繁体   English   中英

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

[英]Calling shell script via bash using a path as an argument

我正在尝试通过bash执行shell脚本,同时还尝试传递所述脚本的参数。

到目前为止,我对此从未遇到过任何问题,像/opt/scripts/start_import.sh user pass "Some name with spaces"命令/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 /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

这是无效的:

if $1=""

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

if test "$1" = "" ;

这也可以更简短地写为

if test -z "$1" ;

您可能需要提供该行代码中正在发生的事情,但是据我所知,您正在尝试在脚本中执行以下操作:

some/path=$1

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM