2016-06-27 101 views
0

我试图通过bash执行shell脚本,同时也试图为所述脚本传递参数。通过bash使用路径作为参数调用shell脚本

到目前为止,我从来没有遇到过这个问题,并且像/opt/scripts/start_import.sh user pass "Some name with spaces"这样的命令总是执行得很好。三个参数传递得很好,可以在start_import脚本中使用。

今天我写了一个新的脚本,我们称之为set_properties.sh。我像往常一样执行命令/opt/scripts/set_properties.sh /opt/subfolder/subfolder user pass stringwithoutspaces stringwithoutspaces2

然而,这一次我收到轰炸与错误信息:

/opt/scripts/set_properties.sh: line 7: /opt/subfolder/subfolder=: No such file or directory 
/opt/scripts/set_properties.sh: line 13: user=: command not found 
/opt/scripts/set_properties.sh: line 19: pass=: command not found 
/opt/scripts/set_properties.sh: line 56: syntax error: unexpected end of file 

我怀疑,传递路径(/选择/子文件夹/ subolder)作为参数导致此错误。然而,我还没有找到这个解决方案呢...

我猜shell是试图评估路径,而不是只是作为参数传递它。我尝试用双引号括住路径,它应该停止对封闭字符串的任何评估,但没有运气。

我在做什么错?调用脚本时是否有任何解决方案将路径作为参数传递?提前致谢!


编辑:set_properties.sh的内容:

set +v 

error_txt="" 
ret_code="0" 

#check input parameters 
if $1="" 
then 
     error_txt="Invalid parameter for 'XXXX'." 
     ret_code="1" 
fi 

if $2="" 
then 
     error_txt="Invalid parameter for 'XXXX'." 
     ret_code="1" 
fi 

if $3="" 
then 
     error_txt="Invalid parameter for 'XXXX'." 
     ret_code="1" 
fi 

if $4="" 
then 
     error_txt="Invalid parameter for 'XXXX'." 
     ret_code="1" 

if $5="" 
then 
     error_txt="Invalid parameter for 'XXXX'." 
     ret_code="1" 
fi 

# create new .properties based on input parameters 
echo "################################################################################" > $1/subfolder1/subfolder2/my_property_file.properties 
echo "#xxxxyyyyzzzz" >> $1/subfolder1/subfolder2/my_property_file.properties 
echo "#created automatically by xxxx processing on `date`" >> $1/subfolder1/subfolder2/my_property_file.properties 
echo "#xxxxyyyyzzzz" >> $1/subfolder1/subfolder2/my_property_file.properties 
echo "propertyName=^$4" >> $1/subfolder1/subfolder2/my_property_file.properties 
echo "#xxxxyyyyzzzz" >> $1/subfolder1/subfolder2/my_property_file.properties 
echo "propertyName=$2" >> $1/subfolder1/subfolder2/my_property_file.properties 
echo "#xxxxyyyyzzzz" >> $1/subfolder1/subfolder2/my_property_file.properties 
echo "propertyName=$3" >> $1/subfolder1/subfolder2/my_property_file.properties 
echo "#xxxxyyyyzzzz" >> $1/subfolder1/subfolder2/my_property_file.properties 
echo "propertyName=$5" >> $1/subfolder1/subfolder2/my_property_file.properties 
echo "#xxxxyyyyzzzz" >> $1/subfolder1/subfolder2/my_property_file.properties 
echo "propertyName=N" >> $1/subfolder1/subfolder2/my_property_file.properties 
echo "################################################################################" >> $1/subfolder1/subfolder2/my_property_file.properties 

ret_code=$? 

echo $error_txt 
exit $ret_code 
+0

写你能提供你的脚本内容? – xurei

+0

修复你的if-tests如果[“x {$ 1}”-eq'x'];然后' –

+0

用参数的路径引用换行 – lulyon

回答

2

这是无效的:

if $1="" 

您需要使用test命令来比较变量。狠抓空白,以及:

if test "$1" = "" ; 

这也可以更简单地为

if test -z "$1" ; 
+0

修复它,谢谢。奇怪的是我使用了相同的if-tests在我的其他脚本中运行良好,但我只是用test -z命令替换它 – daZza

0

您可能需要提供什么在该行上的代码发生的事情,但我可以告诉你正在尝试做这样的事情这在你的脚本中:

some/path=$1 

你不能这样做。 “/”是一个特殊字符,不能成为bash var名称的一部分。

+0

我的不好,我认为这完全是一个脚本调用的问题,而不是脚本本身。我会在几分钟内更新OP。谢谢! – daZza