繁体   English   中英

脚本外壳:在之前传递少量参数之后,获取可变数量的参数

[英]Script shell : Take variable number of arguments after few arguments passed before

嗨,我目前正在尝试制作一个cli命令,该命令会将参数传递给python脚本。

例如,我的python脚本将根据给定的参数执行,

./run.py <START-DATE> <END-DATE> -site <SITE-ID>(ranging from 1 to n)

这里的问题是,如何将变量从bash脚本传递到python脚本?

我研究的是,如果我们不知道将传递多少个参数,则可以使用变量$ 1,$ 2,$ 3等和$ @接受参数。

这里的问题是

我的<START-DATE> <END-DATE>-site仅固定为1个变量,分别是$ 1,$ 2和$ 3,那么如何使用$ @但只能在$ 3之后使用?

我的代码test.sh现在:

#!/usr/bin/bash

if [ $1 -ne "" ] && [ $2 -ne "" ] && [ $3 -ne "" ] && [ $4 -ne "" ]; //minimum 1 argument for site// then
    /usr/bin/python2.6 run.py $1 $2 $3
else
    echo "Please insert the correct number of arguments"

谢谢。

您可以使用$#查找传递给bash脚本的参数数量。 如果一切顺利,请使用$@将所有参数传递给Python脚本。 就像是:

# At least 4 arguments should be passed
if [ $# -gt 3 ]
then
    /usr/bin/python2.6 run.py $@
else
    echo "Please insert the correct number of arguments"
fi

暂无
暂无

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

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