意外的操作员错误

时间:2010-01-06 05:39:49

标签: bash jar find

我的代码有什么问题?

#!/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

输出

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 ...

4 个答案:

答案 0 :(得分:70)

您需要在=行中使用==代替[ $? == 0 ]

答案 1 :(得分:8)

您应该将其更改为:

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

-eq进行数字比较。

您还可以利用shell中返回值0被视为成功的事实,并编写如下代码:

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

答案 2 :(得分:2)

#!/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

答案 3 :(得分:-1)

尝试:

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