bash 意外的操作员错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2011160/
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-09 18:50:11  来源:igfitidea点击:

Unexpected operator error

bashjarfind

提问by wearetherock

What is wrong in my code?

我的代码有什么问题?

#!/bin/sh

LOOK_FOR=""

for i in `find  -name "*jar"`; do
  echo "Looking in $i ..."
  #jar tvf $i | grep $LOOK_FOR > /dev/null
  jar tvf "$i" | grep "$LOOK_FOR" 

  if [ $? == 0 ] ; then
    echo "==> Found \"$LOOK_FOR\" in $i"
  fi  
done #line 13

Output

输出

wk@wk-laptop:$ sh lookjar.sh org/apache/axis/message/addressing/EndpointReference  /media/0C06E20B06E1F61C/uengine/uengine
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/build/uengine_settings.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/WebContent/uengine-web/lib/FCKeditor/WEB-INF/lib/commons-fileupload.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/WebContent/uengine-web/lib/FCKeditor/WEB-INF/lib/FCKeditor-2.3.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/defaultcompany/WebContent/uengine-web/processmanager/signedmetaworks.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/hsqldb/lib/hsqldb.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/hsqldb/lib/servlet.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/src/lib/commons-discovery.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/src/lib/google.jar ...
[: 13: 1: unexpected operator
Looking in /media/0C06E20B06E1F61C/uengine/uengine/src/lib/jxl.jar ...

回答by Chris Jester-Young

You need to use =instead of ==in the [ $? == 0 ]line.

你需要使用=,而不是==[ $? == 0 ]行。

回答by R Samuel Klatchko

You should change that to:

您应该将其更改为:

if [ $? -eq 0 ]; then
    ...

-eqdoes a numeric comparison.

-eq进行数字比较。

You can also take advantage of the fact that in shell a return value of 0 is considered success and write your code like this:

您还可以利用在 shell 中返回值 0 被认为是成功的事实,并像这样编写代码:

if jar tvf "$i" | grep "$LOOK_FOR"; then
    ...

回答by ghostdog74

#!/bin/sh
LOOK_FOR=""    
find  -name "*jar"`| while read -r file
  echo "Looking in $file ..."
  jar tvf "$file" | grep "$LOOK_FOR" 
  if [ $? -eq 0 ] ; then
    echo "==> Found \"$LOOK_FOR\" in $file"
  fi  
done

回答by slebetman

Try:

尝试:

if [[ $? == 0 ]]; then
    echo "==> Found \"$LOOK_FOR\" in $i"
fi