bash脚本错误[:!=:一元运算符预期 [英] bash script error [: !=: unary operator expected

查看:124
本文介绍了bash脚本错误[:!=:一元运算符预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是code:

 如果[$ 1 = -v!]然后
   回声用法:$ 0 [-v]
   出口
科幻

编辑:

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

 #!/斌/庆典如果[$#-gt1];然后
   回声用法:$ 0 [-v]
   出口
科幻如果[$ 1= -v!]然后
   回声用法:$ 0 [-v]
   出口
科幻如果[$ 1= -v];然后
   回声的`ps -ef | grep的-v'\\ ['`
其他
   回声的`ps -ef | grep的'\\ ['| grep的root`
科幻


解决方案

行情!

 如果[$ 1= -v!]然后

否则,当 $ 1 完全是空的,你的测试就变成了:

  [!= -v]

而不是

  [!= -v]

...和!= 不是一元运算符(即一个能够拍摄只有一个参数)。

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.

this is the code:

if [ $1 != -v ]; then
   echo "usage: $0 [-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.

#!/bin/bash

if [ "$#" -gt "1" ]; then
   echo "usage: $0 [-v]"
   exit
fi

if [ "$1" != -v ]; then
   echo "usage: $0 [-v]"
   exit
fi

if [ "$1" = -v ]; then
   echo "`ps -ef | grep -v '\['`"
else
   echo "`ps -ef | grep '\[' | grep root`"
fi

解决方案

Quotes!

if [ "$1" != -v ]; then

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

[ != -v ]

instead of

[ "" != -v ]

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

这篇关于bash脚本错误[:!=:一元运算符预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆