使用路径作为参数通过bash调用shell脚本

时间:2016-06-27 14:56:14

标签: linux 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

我怀疑将路径(/ opt / subfolder / 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

2 个答案:

答案 0 :(得分:2)

这是无效的:

if $1=""

您需要使用test命令来比较变量。密切注意空白:

if test "$1" = "" ;

这也可以简写为

if test -z "$1" ;

答案 1 :(得分:0)

您可能需要提供该行代码中发生的事情,但是我可以告诉您在脚本中尝试执行此类操作:

some/path=$1
你不能这样做。 " /"是一个特殊字符,不能成为bash var名称的一部分。