注释行中出现错误“0:command not found”

时间:2015-01-27 13:52:20

标签: linux bash shell

帮助!我收到错误A1.sh:第205行:0:运行我的脚本后找不到命令。

令我感到困惑的是205行是评论!我已经尝试过编辑它但仍然在同一行上出错。我的所有行结尾都是LF。

下面是编辑后的代码块,第205行现在是:

if `doesBookExist "$title" "$author"`

完整的代码块:

if [[ bookExist -gt 0 ]]
then
    # get title from actual book (in case title input has bad case)
    actualTitle=`cut -d ':' -f 1 BookDB.txt | grep -i "^$title$"`
    sed -i "/^$title:$author.*$/Id" $FILENAME
    if `doesBookExist "$title" "$author"`
    then
        title=`echo "$title" | tr "@" ":"`
        echo
        echo "Book title '$actualTitle' could not be removed!"
    else
        title=`echo "$title" | tr "@" ":"`
        echo
        echo "Book title '$actualTitle' removed successfully!"
    fi
else
    echo
    echo 'Book does not exist!'
fi

感谢任何帮助!

2 个答案:

答案 0 :(得分:3)

在bash中,if期望命令作为第二个参数。你给它的是命令替换的返回码,它是0 - 不是合法的命令。

所以你不需要这样做:

if `doesBookExist "$title" "$author"`

但就是这样:

if doesBookExist "$title" "$author"

顺便说一句:$(somecommand someargs)是命令替换的首选方式 - 当你需要它时 - 因为它可以任意嵌套。

答案 1 :(得分:0)

反引号执行命令替换。看起来好像在命令替换之后你正在尝试运行

 if 0
 then
   ...
 fi

解释了错误消息。但是您想将输出与字符串进行比较。这是通过案例陈述最灵活地完成的:

 case $(doesBookExist "$title" "$author") in
 (0)
   # if it is 0
   ;;
 (1)
   # if it is 1
   ;;
 esac