I am trying to update the values of variables that I have sourced from a .conf file in a bash script.
. /home/pi/OffTheDome/config/timeline.conf
yes=YES
no=NO
if [ $1 == "-B" ]; then
BREAKFAST=$yes
LUNCH=$no
DINNER=$no
NIGHT=$no
elif [ $1 == "-L" ]; then
BREAKFAST=$no
LUNCH=$yes
DINNER=$no
NIGHT=$no
elif [ $1 == "-D" ]; then
BREAKFAST=$no
LUNCH=$no
DINNER=$yes
NIGHT=$no
elif [ $1 == "-N" ]; then
BREAKFAST=$no
LUNCH=$no
DINNER=$no
NIGHT=$yes
fi
The variables BREAKFAST, LUNCH, DINNNER, and NIGHT are all initiallized to NO in a .conf file
BREAKFAST=NO
LUNCH=NO
DINNER=NO
NIGHT=NO
The script shown above is meant to overwrite these values depending on the argument passed with it.
Additional information: There exists a start.sh script that calls a python file with the values of the 4 variables passed to it. The python file calls the script above using subprocess.check_call()
When I run this script to test it, I see nothing change. Any ideas what I am doing wrong?
CodePudding user response:
At the end of the script, rewrite the timeline.conf
file using the updated variables.
cat <<EOF >/home/pi/OffTheDome/config/timeline.conf
BREAKFAST=$BREAKFAST
LUNCH=$LUNCH
DINNER=$DINNER
NIGHT=$NIGHT
EOF
CodePudding user response:
Sample usage:
$ ./start.sh
======== cat /home/pi/OffTheDome/config/timeline.conf ========
BREAKFAST=NO
LUNCH=NO
DINNER=NO
NIGHT=NO
=====================================================================
Usage:
To change BREAKFAST to YES
./start.sh -B
To change DINNER to YES
./start.sh -D
To change NIGHT to YES
./start.sh -N
To change LUNCH to YES
./start.sh -L
$ ./start.sh invalid parameter
======== cat /home/pi/OffTheDome/config/timeline.conf ========
BREAKFAST=NO
LUNCH=NO
DINNER=NO
NIGHT=NO
=====================================================================
Usage:
To change BREAKFAST to YES
./start.sh -B
To change DINNER to YES
./start.sh -D
To change NIGHT to YES
./start.sh -N
To change LUNCH to YES
./start.sh -L
$ ./start.sh -B
$ cat /home/pi/OffTheDome/config/timeline.conf
BREAKFAST=YES
LUNCH=NO
DINNER=NO
NIGHT=NO
$ ./start.sh -L
$ cat "/home/pi/OffTheDome/config/timeline.conf"
BREAKFAST=NO
LUNCH=YES
DINNER=NO
NIGHT=NO
$ ./start.sh -D
$ cat "/home/pi/OffTheDome/config/timeline.conf"
BREAKFAST=NO
LUNCH=NO
DINNER=YES
NIGHT=NO
$ ./start.sh -N
$ cat /home/pi/OffTheDome/config/timeline.conf
BREAKFAST=NO
LUNCH=NO
DINNER=NO
NIGHT=YES
Related coding:
$ cat ./start.sh
#!/bin/bash
#Your wish to use bash/bash.exe/mksh.exe/mksh/ksh/... based on your OS/shell.
python ./start.py $@
$ cat start.py
import os
import sys
if ( 1 == len( sys.argv ) ):
os.system( "./73535990.sh")
else:
os.system( "./73535990.sh " sys.argv[1])
$ cat 73535990.sh
#!/bin/bash
if [[ ! -f /home/pi/OffTheDome/config/timeline.conf ]]
then
echo "BREAKFAST=NO
LUNCH=NO
DINNER=NO
NIGHT=NO">/home/pi/OffTheDome/config/timeline.conf
if [ 0 -ne $? ]
then
echo "Unable to create/update file /home/pi/OffTheDome/config/timeline.conf"
exit 1
fi
fi
. /home/pi/OffTheDome/config/timeline.conf
yes=YES
no=NO
if [[ 0 -eq $# || "-B" != "$1" && "-L" != "$1" && "-D" != "$1" && "-N" != "$1" ]]
then
echo "======== cat /home/pi/OffTheDome/config/timeline.conf ========"
cat /home/pi/OffTheDome/config/timeline.conf
echo "====================================================================="
echo "Usage:"
echo "To change BREAKFAST to $yes"
echo "./start.sh -B"
echo "To change DINNER to $yes"
echo "./start.sh -D"
echo "To change NIGHT to $yes"
echo "./start.sh -N"
echo "To change LUNCH to $yes"
echo "./start.sh -L"
exit 1
elif [ "$1" == "-B" ]
then
sed -i "s/BREAKFAST=NO/BREAKFAST=YES/;s/LUNCH=YES/LUNCH=NO/;s/DINNER=YES/DINNER=NO/;s/NIGHT=YES/NIGHT=NO/;" /home/pi/OffTheDome/config/timeline.conf
elif [ "$1" == "-L" ]
then
sed -i "s/BREAKFAST=YES/BREAKFAST=NO/;s/LUNCH=NO/LUNCH=YES/;s/DINNER=YES/DINNER=NO/;s/NIGHT=YES/NIGHT=NO/;" /home/pi/OffTheDome/config/timeline.conf
elif [ "$1" == "-D" ]
then
sed -i "s/BREAKFAST=YES/BREAKFAST=NO/;s/LUNCH=YES/LUNCH=NO/;s/DINNER=NO/DINNER=YES/;s/NIGHT=YES/NIGHT=NO/;" /home/pi/OffTheDome/config/timeline.conf
elif [ "$1" == "-N" ]
then
sed -i "s/BREAKFAST=YES/BREAKFAST=NO/;s/LUNCH=YES/LUNCH=NO/;s/DINNER=YES/DINNER=NO/;s/NIGHT=NO/NIGHT=YES/;" /home/pi/OffTheDome/config/timeline.conf
fi