Unexpected operator error [duplicate]

早过忘川 提交于 2019-12-17 04:05:07

问题


What is wrong in my code?

#!/bin/sh

LOOK_FOR="$1"

for i in `find $2 -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 ...

回答1:


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




回答2:


You should change that to:

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

-eq does a numeric comparison.

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:

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



回答3:


#!/bin/sh
LOOK_FOR="$1"    
find $2 -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



回答4:


Try:

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


来源:https://stackoverflow.com/questions/2011160/unexpected-operator-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!