我试图通过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
写你能提供你的脚本内容? – xurei
修复你的if-tests如果[“x {$ 1}”-eq'x'];然后' –
用参数的路径引用换行 – lulyon