Bash 脚本错误 [: !=: 预期的一元运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22179405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:42:58  来源:igfitidea点击:

Bash script error [: !=: unary operator expected

bash

提问by user3380240

In my script I am trying to error check if the first and only argument is equal to -v but it is an optional argument. I use an if statement but I keep getting the unary operator expected error.

在我的脚本中,我试图错误检查第一个也是唯一的参数是否等于 -v 但它是一个可选参数。我使用 if 语句,但我不断收到一元运算符预期的错误。

this is the code:

这是代码:

if [  != -v ]; then
   echo "usage: 
#!/bin/bash

if [ "$#" -gt "1" ]; then
   echo "usage: 
if [ "" != -v ]; then
[-v]" exit fi if [ "" != -v ]; then echo "usage:
[ != -v ]
[-v]" exit fi if [ "" = -v ]; then echo "`ps -ef | grep -v '\['`" else echo "`ps -ef | grep '\[' | grep root`" fi
[-v]" exit fi

Edit:

编辑:

I should be more specific: This part of the script above is checking an optional argument and then after, if the argument is not entered, it should run the rest of the program.

我应该更具体:上面脚本的这一部分正在检查一个可选参数,然后,如果没有输入该参数,它应该运行程序的其余部分。

[ "" != -v ]

回答by Charles Duffy

Quotes!

引号!

#!/bin/sh
case  in
  '-v') if [ "" = -v ]; then
         echo "`ps -ef | grep -v '\['`"
        else
         echo "`ps -ef | grep '\[' | grep root`"
        fi;;
     *) echo "usage: 
#!/bin/sh

usage ()
 {
  echo "usage: 
$ adduser -u:sam -s -f -u:bob -trace -verbose
[-v]" exit 1 } unset arg_match for arg in $* do case $arg in '-v') if [ "$arg" = -v ]; then echo "`ps -ef | grep -v '\['`" else echo "`ps -ef | grep '\[' | grep root`" fi arg_match=1;; # this is set, but could increment. *) ;; esac done if [ ! $arg_match ] then usage fi
[-v]" exit 1;; #It is good practice to throw a code, hence allowing $? check esac

Otherwise, when $1is completely empty, your test becomes:

否则,当$1完全为空时,您的测试将变为:

$ adduser -u sam -s -f -u bob -trace -verbose

instead of

代替

##代码##

...and !=is not a unary operator (that is, one capable of taking only a single argument).

...并且!=不是一元运算符(即只能接受一个参数的运算符)。

回答by SMullaney

Or for what seems like rampant overkill, but is actually simplistic ... Pretty much covers all of your cases, and no empty string or unary concerns.

或者对于看似猖獗的矫枉过正,但实际上很简单......几乎涵盖了您的所有案例,没有空字符串或一元问题。

In the case the first arg is '-v', then do your conditional ps -ef, else in all other cases throw the usage.

在第一个参数是'-v'的情况下,然后做你的条件ps -ef,否则在所有其他情况下抛出用法。

##代码##

If one cares not where the '-v' arg is, then simply drop the case inside a loop. The would allow walking all the args and finding '-v' anywhere (provided it exists). This means command line argument order is not important. Be forewarned, as presented, the variable arg_match is set, thus it is merely a flag. It allows for multiple occurrences of the '-v' arg. One could ignore all other occurrences of '-v' easy enough.

如果人们不关心 '-v' arg 在哪里,那么只需将案例放入循环中即可。这将允许遍历所有参数并在任何地方找到“-v”(如果它存在)。这意味着命令行参数顺序并不重要。预先警告,如上所述,变量 arg_match 已设置,因此它只是一个标志。它允许多次出现“-v”参数。人们可以很容易地忽略所有其他出现的“-v”。

##代码##

But, allow multiple occurrences of an argument is convenient to use in situations such as:

但是,允许参数多次出现在以下情况下使用起来很方便:

##代码##

We care not about the order of the arguments, and even allow multiple -u arguments. Yes, it is a simple matter to also allow:

我们不关心参数的顺序,甚至允许多个 -u 参数。是的,还允许:

##代码##