0

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
Fred Zimmerman
  • 1,178
  • 3
  • 13
  • 29

1 Answers1

0

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.

Fred Zimmerman
  • 1,178
  • 3
  • 13
  • 29