I want to optimize a bash code using 1 line instead of two this is the lines i want to optimize:
grep -E "$name" /etc/passwd
if [ $? -eq 0 ]
#...
so the if will test the exit of the last command (grep), i want to merge "grep -E "$name" /etc/passwd" in the if statement to have something like:
if [ ##### -eq 0 ]
thanks for your help :)
没有如果 :
grep "name" file && do_something
You can use the exit code of the command directly in the if statement:
if grep ....
then
echo "found"
else
echo "not found"
fi
If you want to silence the output of the grep command you can add the -q
option, and if you want to quit after the first match (saves time on large files) you can use -m 1
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.