I am using if-else statement to search for keywords and displaying the results in the terminal, here's an example of my code.
read finding
if ["$finding" != "" ]; then
grep $finding information.txt
else
echo "No such information in database."
fi
But the terminal does not display anything if i key in information that does not exist. I started shell about a week back, might need more explanation on how certain code works.
[
(it is a command)-n
to test if length of string is nonzero, or -z
to test if it is zeroSo:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句