2016-06-11 10 views
0

Ich habe Probleme mit einem Bash-Skript, das als Subshell von einer anderen Shell gestartet wird. Das Skript wird in einer Reihe verschiedener Umgebungen installiert, von denen jede eine Vielzahl lokalspezifischer Werte in einer einzelnen Konfigurationsdatei pro Umgebung enthält.Bash-Skript zu CD, um es zu bewerten, liest aus der Konfigurationsdatei

(UPDATE: eigentlichen Code enthalten)

sofort beim Start muss das Skript in ein Verzeichnis, dessen Wert $ scriptpath es aus einer Konfigurationsdatei lesen muss sich ändern, dass viele andere lokale Variable Werte setzt. (Ich weiß, wie man diesen bestimmten Wert aus der richtigen Zeile der Konfigurationsdatei mit sed liest). Der Grund, warum ich es so eingerichtet habe, ist, dass das Skript Konfigurationsvariablen vor dem Lesen der Positionsparameter setzt (so dass die Parameter die Konfigurationsvariablen übertrumpfen).

Da dieses Skript von anderen verwendet wird und einfach zu installieren ist, möchte ich nicht Aliase, Funktionen oder Shell-Prozeduren einrichten, die einen Benutzer erfordern würde, Änderungen an seiner lokalen Umgebung vorzunehmen. Ich möchte, dass alle benötigten Konfigurationswerte, einschließlich $ scriptpath, in einer einzigen Konfigurationsdatei gespeichert werden.

ODER $ scriptpath konnte von der übergeordneten Shell gesetzt werden. ABER, weil ich die Positionsparameter später im Skript verarbeite, hatte ich Probleme, sie sowohl oben als auch später zu verarbeiten.

Das Skript wird von einem Shell-Skript ausgelöst, das von einer Magento-Erweiterung gestartet wird. Die verschnörkelten Klammern sind Werte, die von der Magento-Datenbank durch die Erweiterung gezogen werden.

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" 

Das Ergebnis ist dieses:

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 
+8

Warum posten Sie Ihren eigentlichen Code nicht? Ich bin kein Fan von dieser Meta-Meta-Codierung – andlrc

+1

DO 'export confpath'? – cdarke

+0

Ich habe versucht, confpath zu exportieren, aber es funktioniert nicht wie erwartet –

Antwort

0

Das ist mein Problem behoben:

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 
echo $DIR 
cd $DIR 
. $DIR/../../conf/config.txt 
cd $scriptpath 

Mit dem Programm sich seiner eigenen Standort Es ist in der Lage, die richtige Konfiguration zu finden Ionenwerte.