Run command if stdin is not empty on Linux
ifne runs the following command if and only if the standard input is not empty.
Options -n. Reverse operation. Run the command if the standard input is empty.
The ifne command is part of the moreutils package.
$ sudo yum -y install moreutils
Example usage:
Send email if and only if there is content:
$ mysql --login-path=goyun mysql -e "select * from user where user='goyuninfo'\G" | ifne mailx -s "User goyuninfo found in mysql" service@goyun.info
If you don't want to install ifne, the following is the same:
$ output=$(mysql --login-path=goyun mysql -e "select * from user where user='goyuninfo'\G"); [[ -n "$output" ]] && mailx -s "User goyuninfo found in mysql" service@goyun.info
Reverse:
$ mysql --login-path=goyun mysql -e "select * from user where user='goyuninfo'\G" | ifne -n mailx -s "User goyuninfo not found in mysql" service@goyun.info
Null message body; hope that's ok
Comments
Post a Comment