Issue
I need to update the value of a variable in a bash script and write the changes to the file. My attempts have either replace the key without strings or deleted the value.
sed -ri 's|^(VARIABLE_KEY\s*=\s*).*|\new_java-microservice.jar|' path_to_file/bashscript.sh
sed -i "s#\(.*VARIABLE_KEY =\)\( .*\)#\1 "new_java-microservice.jar"#" path_to_file/bashscript.sh
sed -i 's/VARIABLE_KEY =.*/VARIABLE_KEY ="new_java-microservice.jar"/' path_to_file/bashscript.sh
script
#!/bin/bash
COMMAND="$1"
HOME="/root/service/"
BASE="${HOME}/javaservice"
VARIABLE_KEY="existing-java.jar"
case "${COMMAND}" in
"start")
nohup java -jar ${BASE}/${VARIABLE_KEY} start > /dev/null 2>&1 &
;;
"stop")
#....
;;
*)
echo "Usage: $0 {start|stop}"
;;
esac
Solution
sed -i 's/^VARIABLE_KEY=.*/VARIABLE_KEY="new_java-microservice.jar"/' path_to_file/bashscript.sh
Answered By - dimich Answer Checked By - Cary Denson (WPSolving Admin)