bash script to cd to value it reads from configuration file - bash

I am having trouble with a bash script that is launched as a subshell from another shell. The script is installed in a number of different environments each of which have a variety of local-specific values set in a single config file per environment.
(UPDATE: actual code included)
Immediately on launch, the script needs to change to a directory whose value $scriptpath it must read from a configuration file that sets many other local variable values. (I know how to read this particular value from the correct line of config file using sed). The reason I have it set up this way is that the script sets configuration variables before it reads the positional parameters (so that the parameters will trump the configuration variables).
Because this script is used by others and must be easy to install, I do not want to set up aliases, functions, or shell procedures that would require a user to make changes to his local environment. I want all needed config values, including $scriptpath, to be in a single configuration file.
OR $scriptpath could be set from the parent shell. BUT because I process the positional parameters later in the script, I have had trouble processing them both right at the top and later.
the script is triggered from a shell script that is launched by a Magento extension. The squiggly brackets are values drawn from the Magento database by the extension.
scriptpath={{var base_path}}
echo $scriptpath
export scriptpath
/opt/bitnami/apache2/htdocs/git-pk-production/pagekicker-community/scripts/bin/builder.sh --scriptpath "{{var base_path}}"--seedfile "{{var base_tmp_path}}/seedlist" --booktype "{{var product.booktype}}" --buildtarget "{{var target}}" --jobprofile "{{var product.jobprofile}}.jobprofile" --booktitle "{{var product.name}}" --truncate_seed "yes" --ebook_format "mobi" --sample_tweets "no" --wikilang "{{var product.wikilang}}" --coverfont "{{var product.coverfont}}" --covercolor "{{var product.covercolor}}" --customername "{{var customer.name}}" --yourname "{{var product.yourname}}"
builder.sh:
#!/bin/bash
# accepts book topic and book type definition, then builds book
echo "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
#scriptpath=$(cat scriptpath.var) #works but is not portable
# echo $scriptpath
cd $scriptpath
. ../conf/config.txt
. includes/set-variables.sh
#echo "set variables, now echoing them"
# . includes/echo-variables.sh
echo "revision number is" $SFB_VERSION
echo "sfb_log is" $sfb_log
echo "completed reading config file and beginning logging at" `date +'%m/%d/%y%n %H:%M:%S'`
jobprofile="default"
jobprofilename="default"
singleseed="none"
sample_tweets="no"
todaysdate=`date`
wikilang="en"
summary="false"
truncate_seed="yes"
coverfont="Minion"
covercolor="RosyBrown"
export PERL_SIGNALS="unsafe"
echo "PERL_SIGNALS" is $PERL_SIGNALS
while :
do
case $1 in
--help | -\?)
echo "for help review source code for now"
exit 0 # This is not an error, the user requested help, so do not exit status 1.
;;
--passuuid)
passuuid=$2
shift 2
;;
--passuuid=*)
passuuid=${1#*=}
shift
;;
--seedfile)
seedfile=$2
shift 2
;;
--seedfile=*)
seedfile=${1#*=}
shift
;;
--booktype)
booktype=$2
shift 2
;;
--booktype=*)
booktype=${1#*=}
shift
;;
--booktitle)
booktitle=$2
shift 2
;;
--booktitle=*)
booktitle=${1#*=}
shift
;;
--buildtarget)
buildtarget=$2
shift 2
;;
--buildtarget=*)
buildtarget=${1#*=}
shift
;;
--singleseed)
singleseed=$2
shift 2
;;
--singleseed=*)
singleseed=${1#*=}
shift
;;
--truncate_seed)
truncate_seed=$2
shift 2
;;
--truncate_seed=*)
shift
truncate_seed=${1#*=}
;;
--sample_tweets)
sample_tweets=$2
shift 2
;;
--sample_tweets=*)
shift
sample_tweets=${1#*=}
;;
--ebook_format)
ebook_format=$2
shift 2
;;
--ebook_format=*)
shift
ebook_format=${1#*=}
;;
--jobprofile)
jobprofile=$2
shift 2
;;
--jobprofile=*)
jobprofile=${1#*=}
shift
;;
--jobprofilename)
jobprofilename=$2
shift 2
;;
--jobprofilename=*)
jobprofilename=${1#*=}
shift
;;
--wikilang)
wikilang=$2
shift 2
;;
--wikilang=*)
wikilang=${1#*=}
shift
;;
--summary)
summary=$2
shift 2
;;
--summary=*)
summary=${1#*=}
shift
;;
--safe_product_name)
safe_product_name=$2
shift 2
;;
--safe_product_name=*)
safe_product_name=${1#*=}
shift
;;
--coverfont)
coverfont=$2
shift 2
;;
--coverfont=*)
coverfont=${1#*=}
shift
;;
--covercolor)
covercolor=$2
shift 2
;;
--covercolor=*)
covercolor=${1#*=}
shift
;;
--fromccc)
fromccc=$2
shift 2
;;
--fromccc=*)
fromccc=${1#*=}
shift
;;
--editedby)
editedby=$2
shift 2
;;
--editedby=*)
editedby=${1#*=}
shift
;;
--yourname)
yourname=$2
shift 2
;;
--yourname=*)
yourname=${1#*=}
shift
;;
--customername)
customername=$2
shift 2
;;
--customername=*)
customername=${1#*=}
shift
;;
--storecode)
storecode=$2
shift 2
;;
--storecode=*)
storecode=${1#*=}
shift
;;
--environment)
environment=$2
shift 2
;;
--environment=*)
environment=${1#*=}
shift
;;
--shortform)
shortform=$2
shift 2
;;
--shortform=*)
shortform=${1#*=}
shift
;;
--flickr)
flickr=$2
shift 2
;;
--flickr=*)
flickr=${1#*=}
shift
;;
--dontcleanupseeds)
dontcleanupseeds=$2
shift 2
;;
--dontcleanupseeds=*)
dontcleanupseeds=${1#*=}
shift
;;
--batch_uuid)
batch_uuid=$2
shift 2
;;
--batch_uuid=*)
batch_uuid=${1#*=}
shift
;;
--) # End of all options
shift
break
;;
-*)
echo "WARN: Unknown option (ignored): $1" >&2
shift
;;
*) # no more options. Stop while loop
break
;;
esac
done
# Suppose some options are required. Check that we got them.
if [ ! "$passuuid" ] ; then
echo "creating uuid"
uuid=$("$PYTHON_BIN" -c 'import uuid; print uuid.uuid1()')
echo "uuid is" $uuid | tee --append $xform_log
mkdir -p -m 777 $TMPDIR$uuid
else
uuid=$passuuid
echo "received uuid " $uuid
mkdir -p -m 777 $TMPDIR$uuid
fi
if [ -z "$covercolor" ]; then
covercolor="RosyBrown"
echo "no cover color in command line so I set it to "$covercolor
else
echo "$covercolor"
fi
if [ -z "$coverfont" ]; then
coverfont="Minion"
echo "no cover font in command line so I set it to "$coverfont
else
echo "$coverfont"
fi
if [ -z "$wikilang" ]; then
wikilang="en"
echo "no wikilang in command line so I set it to "$wikilang
else
echo "$wikilang"
fi
echo "debug: booktitle is $booktitle"
echo "debug: scriptpath is $scriptpath"
The result is this:
scriptpath={{var base_path}}
echo $scriptpath
export scriptpath
Executing: /opt/bitnami/apache2/htdocs/git-pk-production/pagekicker-community/scripts/bin/builder.sh --seedfile "/opt/bitnami/apps/magento/htdocs/media/downloadable/tmp/build/8/seedlist" --booktype "Default" --buildtarget "/opt/bitnami/apps/magento/htdocs/media/downloadable/tmp/build/8/Russian_intervention_in_Syria.mobi" --jobprofile "default.jobprofile" --booktitle "Russian intervention in Syria" --truncate_seed "yes" --ebook_format "mobi" --sample_tweets "no" --wikilang "en" --coverfont "Adler" --covercolor "DodgerBlue1" --customername "Fred Zimmerman" --yourname ""
BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB
revision number is
sfb_log is
completed reading config file and beginning logging at 06/12/16 18:43:09
PERL_SIGNALS is unsafe
debug: booktitle is Russian intervention in Syria
creating uuid
uuid is
DodgerBlue1
Adler
en
debug: exiting for test
Exit status returned: 0
Target File is missing: /opt/bitnami/apps/magento/htdocs/media/downloadable/tmp/build/8/Russian_intervention_in_Syria.mobi
Notification Information

This fixed my problem:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $DIR
cd $DIR
. $DIR/../../conf/config.txt
cd $scriptpath
With the program aware of its own location, it is able to find the right configuration values.

Related

Bash script has unexpected token `;;'

I have a Bash command that is generated by a buck Python script:
platform=$(case $(uname) in
"Linux") echo "linux-x86_64" ;;
"Darwin") echo "darwin64-x86_64-cc" ;;
*) echo "Unknown" esac);
cp -r $SRCDIR $OUT && cd $OUT && ./Configure shared $platform --prefix=$OUT/build --openssldir=$OUT/build/openssl && make && make install
When I try to execute it, I get the following error:
unexpected token `;;'
What have I done wrong here?
I am on the latest macOS.
A much clearer and more efficient way to write the code would be:
case $(uname) in
"Linux") platform="linux-x86_64" ;;
"Darwin") platform="darwin64-x86_64-cc" ;;
*) platform="Unknown" ;;
esac
Having the result of echo captured by $() is something of a shell anti-pattern. Why write to stdout and read from stdin when you can simply use the text directly?
The closing ) on the "Linux" case is terminating the process substitution. Since it's bash, just use matching parens in the case:
platform=$(case $(uname) in
("Linux") echo "linux-x86_64" ;;
("Darwin") echo "darwin64-x86_64-cc" ;;
(*) echo "Unknown";;
esac
)
(Also, note that I had to add ;; after the default case.)
I would use something like:
get_platform() {
case "${1,,}" in
darwin*) echo darwin64... ;;
freebsd*) echo freebsd... ;;
linux*) echo linux-x86_64.. ;;
cygwin*) echo cyg... ;;
*) return 1;;
esac
return 0
}
platform=$(get_platform "$OSTYPE" || get_platform $(uname))
echo "${platform:-Unknown}"
Double check - bash stores the operating system in the $OSTYPE variable. In case of fail (other shells), it still trying to use the uname.
In the case of undetected OS, the final decision, is populated to the main script (return 1) so you can check and adjust it as you want, like echo "${platform:-Unknown}".

Bash long options/flags - how to do it?

I am trying to change my working script with getopts to getopt ( long flags ).
Below i present my code which is working.
getopts 'm:' mode
modeValue=$OPTARG
getopts 'p:' parameter
parameterValue=$OPTARG
getopts 'u:' parameter
parameterValue2=$OPTARG
getopts 'l:' parameter
parameterValue3=$OPTARG
getopts 'n:' parameter
parameterValue4=$OPTARG
getopts 'e:' parameter
parameterValue5=$OPTARG
getopts 'w:' parameter
parameterValue6=$OPTARG
getopts 'r:' parameter
parameterValue7=$OPTARG
case $modeValue in
addRepository)
doAddRepository "$parameterValue" "$parameterValue7"
exit $?
;;
addProject)
doAddProject "$parameterValue"
exit $?
;;
addUser)
doAddUser "$parameterValue2" "$parameterValue4" "$parameterValue5" "$parameterValue6"
exit $?
;;
assignProject)
doAssignProject "$parameterValue" "$parameterValue2" "$parameterValue3"
exit $?
;;
*)
#echo "$doShowUsage"
exit 1
;;
esac
Now my script is working like example below:
For add repository: ./script.sh -m addRepository -p NameOfTheProject -r NameOfTheRepository
I want to edit this for something like this:
./script.sh --mode addRepository --project NameOfTheProject --repo NameOfTheRepository
I started to modify code and added something what i present below:
TEMP=`getopt -o m:p:u:l:n:e:c:r: --long mode:,project:,username:,level:,name:,email:,pass:,repo: -n 'test.sh'
-- "$#"` eval set -- "$TEMP"
while true ; do
case "$1" in
-m|--mode)
case "$2" in
addRepository)
doAddRepository=$2 ; shift 2 ;;
addProject)
doAddProject=$2 ; shift 2 ;;
addUser)
doAddUser=$2 ; shift 2 ;;
assignProject)
doAssignProject=$2 ; shift 2 ;;
esac ;;
-h|--help)
case "$2" in
*) echo "$doShowUsage"
exit 1
esac ;;
esac done
My question is : Am I doing it in the right way ? How can I add parameters to the functions "doAddProject/Repository/User...?" Can someone give me some advices? Above functions got different amount of parameters so take a look at it.
Thank you!
Stephane Chazelas wrote a very fine getops-long shell script that I use in my bash debugger. You can copy that script and use it.
If you run that program setting variable test_getopts_long, e.g.
test_getopts_long=1 bash getopts_long.sh
you'll see extensive examples for how to use, and it tests itself.

Execution sequence in shell script and passing the right arguments to functions

I have a shell script that I use to launch some ROS launcher file (not that important for my question). My requirement is to use some arguments and therefore I am using getopts.
Basically, I am getting one file or a few, playing them with ROS and simultaneously recording all the files back to a single file. After that, if I have provided an argument -r or -c, I would like to run two additional operations (reindex and compress) on the recorded file. But it is required that the other process are done, before I can run the -r and -c. I am using the wait keyword, but I am not sure I really understand the flow. In other words, the playing and recording should be done and only then -r and -c should be run if provided as arguments.
Second question is related to how do I get or pass the same file that was outputted to these two functions (reindex and compress)?
So my desired format is:
./myscript -i file1 -i file2 -o /home/output -r -c
#!/bin/bash
OUTPUT_FILE_NAME="output_$(date +%Y.%m.%d--%H_%M_%S)"
usage="
$(basename "$0") [-i] [-o] [-r] [-c] [-h] -- to be done"
# Reset is necessary if getopts was used previously in the script.
OPTIND=1
while getopts ":i:orch:" opt; do
case $opt in
i ) echo "INPUT file - argument = $OPTARG"; input_files+=($OPTARG) ;;
o ) echo "OUTPUT dir - argument = $OPTARG"; output_dir=($OPTARG) ;;
r ) echo "REINDEX"; reindex ;;
c ) echo "COMPRESS"; compress ;;
h ) echo "$usage"
graceful_exit ;;
* ) echo "$usage"
exit 1
esac
done
# Shift off the options
shift $((OPTIND-1))
roslaunch myLauncher.launch &
echo "Number of loaded files: ${#input_files[#]}"
echo -n "FILES are:"
rosbag play ${input_files[#]} &
rosbag record -o $output_dir/$OUTPUT_FILE_NAME -a &
wait
function reindex{
rosbag reindex $output_dir/$OUTPUT_FILE_NAME
}
function compress{
rosbag reindex $output_dir/$OUTPUT_FILE_NAME
}
Thank you in advance!
You're very close to where you need to be — and using getopts puts you firmly on the correct track, too. Note whether or not you need to reindex or compress in the option parsing loop. Then, after the music has been played and the output file written, run the code from the functions if you need to:
#!/bin/bash
OUTPUT_FILE_NAME="output_$(date +%Y.%m.%d--%H_%M_%S)"
usage="$(basename "$0") [-i file] [-o file] [-r] [-c] [-h]"
input_files=() # Empty list of files/tracks
output_dir="." # Default output directory
r_flag="no" # No reindex by default
c_flag="no" # No compress by default
while getopts ":i:orch:" opt; do
case $opt in
(i) echo "INPUT file - argument = $OPTARG"; input_files+=("$OPTARG");;
(o) echo "OUTPUT dir - argument = $OPTARG"; output_dir="$OPTARG";;
(r) echo "REINDEX"; r_flag="yes";;
(c) echo "COMPRESS"; c_flag="yes";;
(h) echo "$usage" >&2; exit 0;;
(*) echo "$usage" >&2; exit 1;;
esac
done
# Shift off the options
shift $((OPTIND-1))
if [ $# != 0 ] || [ "${#input_files[#]}" = 0 ] ||
then
echo "$usage" >&2
exit 1
fi
roslaunch myLauncher.launch &
echo "Number of loaded files: ${#input_files[#]}"
echo -n "FILES are:"
rosbag play "${input_files[#]}" &
rosbag record -o "$output_dir/$OUTPUT_FILE_NAME" -a &
wait
if [ "$r_flag" = "yes" ]
then
rosbag reindex "$output_dir/$OUTPUT_FILE_NAME"
fi
if [ "$c_flag" = "yes" ]
then
rosbag compress "$output_dir/$OUTPUT_FILE_NAME"
fi
I didn't keep the functions since they didn't provide any value in the rewritten code.

BASH - getopts not working properly

I'm currently having problems with my script. Basically, what I want to happen is when I execute ./apache_new_vhost.sh -a -d google.com, it will create a file and directories and if I use the -r option, it should delete.
The script was able to use the functions like add_vhost. It could create a configuration and folder however the filename is empty because it could not read the value I passed to $domain.
while getopts ":a:r:d:h" opt; do
case $opt in
a) action=add_vhost
;;
r) action=remove_vhost
;;
d) domain=$OPTARG
;;
h) usage
exit 1
;;
\?) echo "Invalid option: -$OPTARG"
usage
exit 1
;;
:) echo "Error: option -$OPTARG requires an argument."
usage
exit 1
;;
esac
done
#if [ -z $domain ]; then
# usage
# exit 1
if [ $action == "add_vhost" ]; then
echo $action $domain
elif [ $action == "remove_vhost" ]; then
echo $action $domain
fi
The options are processed in the order you specify them on the command line. So in your example, case a) is processed first, and calls your add_vhost function right then.
But the d) case hasn't been processed yet, so you haven't set domain.
You need to change your logic a bit. Rather than calling your functions directly from the case statement, save what action was selected. i.e.:
a) action="add_vhost"
;;
Then after the case, check that you do have an action selected, and call that function.
As per your script you expect argument after option -a. So when you execute your script by
./apache_new_vhost.sh -a -d google.com
then -d will consider as argument given to -a option. So your second argument discarded.To solve it just give any argument after -a (ex: ./apache_new_vhost.sh -a 1 -d google.com )option or make changes in your getopt
while getopts ":ar:d:h" opt; do

bash case statement with unmatched patterns

Ok, so i have tried searching for this on google and i cant seem to find an answer. What i'm trying to do is create a case statement in bash but if the user enters a different number than listed it just exits the script. How do i make it give an error and then ask for the user to select one of the options?
for example, my case statement
case $ans in
1) echo "Running Project 1..."
sleep 2
./project1.sh
;;
2) echo "Running Project 2..."
sleep 2
./project2.sh
;;
Qq) echo "Exiting"
exit
;;
esac
so any options other than 1, 2, Qq it will give an error saying invalid selection, try again.
You need a while loop and a boolean variable like that:
flag = true
while [ $flag ]; do
case $ans in
1) echo "Running Project 1..."
sleep 2
./project1.sh
;;
2) echo "Running Project 2..."
sleep 2
./project2.sh
;;
Qq) echo "Exiting"
flag = false
;;
esac
done

Resources