the first command in my bash script is a grep from an existing file for errors. EX:
cat /usr/local/avamar/var/ddrmaintlogs/ddrmaint.log | grep "hfscheck-finish Backup directory missing for backup" > PartialBackups
cat PartialBackups | sed -n -e 's#^.*cur/ ##p' >P2
cat P2 | sed 's#/# / /#g' > P3
cat P3 | awk {'print $1'} >> S1
cat P3 | awk {'print $3'} >> S2
if newfile is empty (PartialBackups File) i want to exit the script, if not i want the script to continue
how can i do that?
Put the grep
in an if
statement.
if ! grep -err /file >> newfile; then
exit
fi
newfile
may or may not be empty--you are appending to a possibly non-empty file to begin with--but grep
will have a non-zero exit status if it doesn't add anything to the file.
You can use
if ! [ -s "/file" ];then
exit
fi
or shorter with less obvious syntax
test -s "/file" && exit
test -s
tests if a file exists and has non-zero size. The second syntax is exploiting, that binary operators only evaluate as much as needed, so a positive return value from test
prevents exit
from running, as the expression would be false anyway.
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.