0

我在使用从另一个 shell 作为子 shell 启动的 bash 脚本时遇到问题。该脚本安装在许多不同的环境中,每个环境都在每个环境的单个配置文件中设置了各种特定于本地的值。

(更新:包括实际代码)

在启动时,脚本需要立即更改到一个目录,其值 $scriptpath 它必须从设置许多其他局部变量值的配置文件中读取。(我知道如何使用 sed 从正确的配置文件行中读取这个特定值)。我以这种方式设置它的原因是脚本在读取位置参数之前设置了配置变量(这样参数将胜过配置变量)。

因为这个脚本被其他人使用并且必须易于安装,所以我不想设置别名、函数或 shell 过程,这些都需要用户对其本地环境进行更改。我希望所有需要的配置值,包括 $scriptpath,都在一个配置文件中。

或者 $scriptpath 可以从父 shell 中设置。但是因为我稍后在脚本中处理位置参数,所以我在处理它们时遇到了麻烦,无论是在顶部还是在后面。

该脚本是从一个由 Magento 扩展启动的 shell 脚本触发的。波浪形括号是扩展从 Magento 数据库中提取的值。

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}}" 

生成器.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"

结果是这样的:

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
4

1 回答 1

0

这解决了我的问题:

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

程序知道自己的位置后,就能够找到正确的配置值。

于 2016-06-13T15:21:05.190 回答