Help! I get an error A1.sh: line 205: 0: command not found after running my script.
的帮助!得到一个误差A1。sh:第205行:0:运行脚本后没有找到命令。
What baffles me is that the line 205 is a comment! I have tried editing it but still get an error on the same line. All my line endings are LF.
令我困惑的是,第205行居然是评论!我试过编辑它,但还是在同一条线上有错误。所有的线尾都是LF。
Below is the block of code after editing, line 205 is now:
下面是编辑后的代码块,第205行现在是:
if `doesBookExist "$title" "$author"`
Full block of code:
完整的代码块:
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
Any help is appreciated!
任何帮助都是赞赏!
2 个解决方案
#1
3
In bash, if
expects a command as second argument. What you gave it is the returncode of your command substitution, which is 0 - not a legal command.
在bash中,if将命令作为第二个参数。您所给出的是命令替换的returncode,它是0,而不是一个法律命令。
So you need not do this:
所以你不需要这样做:
if `doesBookExist "$title" "$author"`
but just this:
但就在这个:
if doesBookExist "$title" "$author"
By the way: $(somecommand someargs)
is the preferred way for command substitution - when you need it - as it can be arbitrarily nested.
顺便说一句:$(somecommand someargs)是命令替换的首选方式(当您需要时),因为它可以被任意嵌套。
#2
0
The backticks perform command substitution. It looks as if after command substitution you are trying to run
回签执行命令替换。看起来好像在命令替换之后,您正在尝试运行。
if 0
then
...
fi
which explains the error message. But you want to compare the output to a string. This is most flexibly done with a case statement:
这解释了错误消息。但是您需要将输出与字符串进行比较。最灵活的做法是使用案例说明:
case $(doesBookExist "$title" "$author") in
(0)
# if it is 0
;;
(1)
# if it is 1
;;
esac
#1
3
In bash, if
expects a command as second argument. What you gave it is the returncode of your command substitution, which is 0 - not a legal command.
在bash中,if将命令作为第二个参数。您所给出的是命令替换的returncode,它是0,而不是一个法律命令。
So you need not do this:
所以你不需要这样做:
if `doesBookExist "$title" "$author"`
but just this:
但就在这个:
if doesBookExist "$title" "$author"
By the way: $(somecommand someargs)
is the preferred way for command substitution - when you need it - as it can be arbitrarily nested.
顺便说一句:$(somecommand someargs)是命令替换的首选方式(当您需要时),因为它可以被任意嵌套。
#2
0
The backticks perform command substitution. It looks as if after command substitution you are trying to run
回签执行命令替换。看起来好像在命令替换之后,您正在尝试运行。
if 0
then
...
fi
which explains the error message. But you want to compare the output to a string. This is most flexibly done with a case statement:
这解释了错误消息。但是您需要将输出与字符串进行比较。最灵活的做法是使用案例说明:
case $(doesBookExist "$title" "$author") in
(0)
# if it is 0
;;
(1)
# if it is 1
;;
esac