Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux Bash ARGS帮助-获取随机错误_Linux_Bash_Args - Fatal编程技术网

Linux Bash ARGS帮助-获取随机错误

Linux Bash ARGS帮助-获取随机错误,linux,bash,args,Linux,Bash,Args,如果某个变量设置到位,试图设置规则,有人能识别我这里缺少的wtf吗 test.sh: #!/bin/bash args=($@) if [ "$@" = "--cron" ]; then echo "WORKS!"; else echo "FAILS" fi /test.sh的输出: ./test.sh: line 3: [: =: unary operator expected FAILS 但是,当我运行/test.sh--cron时,它可以工作,并且可以工作被输出。这应该可以

如果某个变量设置到位,试图设置规则,有人能识别我这里缺少的wtf吗

test.sh:

#!/bin/bash
args=($@)
if [ "$@" = "--cron" ]; then 
 echo "WORKS!";
    else echo "FAILS"
fi
/test.sh的输出:

./test.sh: line 3: [: =: unary operator expected
FAILS

但是,当我运行
/test.sh--cron
时,它可以工作,并且
可以工作
被输出。

这应该可以工作,但只适用于第一个元素,如果您想要更多,您可能需要对
执行
,同时
循环遍历参数

#!/bin/bash
args=($1)
if [ $args ] && [ $args = "--cron" ]; then
  echo "WORKS!";
    else echo "FAILS"
fi

这应该是可行的,但只适用于第一个元素,如果您想要更多,您可能需要对
执行
,或者在
循环时对参数进行迭代

#!/bin/bash
args=($1)
if [ $args ] && [ $args = "--cron" ]; then
  echo "WORKS!";
    else echo "FAILS"
fi

在这种情况下,我认为
“$@”
引用得太好了

尝试与
“$1”
进行比较,或使用:

#!/bin/bash
args=($@)
if [ "$*" = "--cron" ]; then 
    echo "WORKS!";
else
    echo "FAILS"
fi

在这种情况下,我认为
“$@”
引用得太好了

尝试与
“$1”
进行比较,或使用:

#!/bin/bash
args=($@)
if [ "$*" = "--cron" ]; then 
    echo "WORKS!";
else
    echo "FAILS"
fi

正确的方法会有所不同,具体取决于你想做什么。如果要检查第一个参数是否为--cron,请使用以下命令:

if [ "$1" = "--cron" ]; then
if [ "$*" = "--cron" ]; then
cronopt=false
for argument; do
    if [ "$argument" = "--cron" ]; then
        cronopt=true
        break    # note: if you are scanning the arguments for other things too, remove this
    fi
done
if $cronopt; then
    ...
如果要检查唯一的参数是否为--cron,请使用以下命令:

if [ "$1" = "--cron" ]; then
if [ "$*" = "--cron" ]; then
cronopt=false
for argument; do
    if [ "$argument" = "--cron" ]; then
        cronopt=true
        break    # note: if you are scanning the arguments for other things too, remove this
    fi
done
if $cronopt; then
    ...
(注意,
“$*”
是执行某些操作的正确方法,这是极少数情况之一——它扩展到所有由空格分隔的参数,但出于解析目的被视为单个单词。)

如果要检查任何参数是否为--cron,请使用以下命令:

if [ "$1" = "--cron" ]; then
if [ "$*" = "--cron" ]; then
cronopt=false
for argument; do
    if [ "$argument" = "--cron" ]; then
        cronopt=true
        break    # note: if you are scanning the arguments for other things too, remove this
    fi
done
if $cronopt; then
    ...

顺便说一句,我不确定您使用的是
args=($@)
行,但是如果您想将参数存储在数组中,正确的方法是
args=(“$@”)
——引号防止它进行分词、文件名扩展,在将arg放入数组之前,请先执行etc操作。

执行此操作的正确方法会有所不同,具体取决于您要执行的操作。如果要检查第一个参数是否为--cron,请使用以下命令:

if [ "$1" = "--cron" ]; then
if [ "$*" = "--cron" ]; then
cronopt=false
for argument; do
    if [ "$argument" = "--cron" ]; then
        cronopt=true
        break    # note: if you are scanning the arguments for other things too, remove this
    fi
done
if $cronopt; then
    ...
如果要检查唯一的参数是否为--cron,请使用以下命令:

if [ "$1" = "--cron" ]; then
if [ "$*" = "--cron" ]; then
cronopt=false
for argument; do
    if [ "$argument" = "--cron" ]; then
        cronopt=true
        break    # note: if you are scanning the arguments for other things too, remove this
    fi
done
if $cronopt; then
    ...
(注意,
“$*”
是执行某些操作的正确方法,这是极少数情况之一——它扩展到所有由空格分隔的参数,但出于解析目的被视为单个单词。)

如果要检查任何参数是否为--cron,请使用以下命令:

if [ "$1" = "--cron" ]; then
if [ "$*" = "--cron" ]; then
cronopt=false
for argument; do
    if [ "$argument" = "--cron" ]; then
        cronopt=true
        break    # note: if you are scanning the arguments for other things too, remove this
    fi
done
if $cronopt; then
    ...

顺便说一句,我不确定您使用的是
args=($@)
行,但是如果您想在数组中存储参数,正确的方法是
args=(“$@”)
——引号防止它在将args放入数组之前进行分词、文件名扩展等操作。

我不知道,但可能是这个
=
。。不,不是。。刚刚测试:/@n0oitaf Bash应该同时接受
=
=
。我是noob,但可能是这个
=
。。不,不是。。刚刚测试:/@n0oitaf Bash应该同时接受
=
=
。更新了答案但不理想,它只适用于第一个参数更新了答案但不理想,它只适用于第一个参数argument@muistooshort:否;
“$*”
(单个字符串)和
“$@”
(有多少个字符串就有多少个参数)之间有很大的区别。另请参见和@JonathanLeffler:I错过了切换到
$*
。至少有人提醒了我为什么不再编写shell脚本而改写Perl脚本。@muistooshort:No;
“$*”
(单个字符串)和
“$@”
(有多少个字符串就有多少个参数)之间有很大的区别。另请参见和@JonathanLeffler:I错过了切换到
$*
。至少有人提醒了我为什么不再编写shell脚本而改写Perl脚本。