Zac*_*Zac 4 shell grep shell-script test
我正在使用 if-else 语句搜索关键字并在终端中显示结果,这是我的代码示例。
read finding
if ["$finding" != "" ]; then
grep $finding information.txt
else
echo "No such information in database."
fi
Run Code Online (Sandbox Code Playgroud)
但是如果我输入不存在的信息,终端不会显示任何内容。我大约一周前开始使用 shell,可能需要更多关于某些代码如何工作的解释。
[
(这是一个命令)-n
以测试是否字符串的长度为非零,或-z
测试,如果它是零所以:
read finding
if [ -z "$finding" ]; then
echo "You didn't enter anything"
else
grep "$finding" information.txt
if [ ! "$?" -eq 0 ]; then
echo "No such information in database."
fi
fi
Run Code Online (Sandbox Code Playgroud)