Answers:
这是我对这个问题的快速解决方案:
用法示例:
$ watch_and_kill_if.sh ERROR my_program
#!/usr/bin/env bash
function show_help()
{
IT=$(CAT <<EOF
usage: ERROR_STR YOUR_PROGRAM
e.g.
this will watch for the word ERROR coming from your long running program
ERROR my_long_running_program
EOF
)
echo "$IT"
exit
}
if [ "$1" == "help" ]
then
show_help
fi
if [ -z "$2" ]
then
show_help
fi
ERR=$1
shift;
$* |
while IFS= read -r line
do
echo $line
if [[ $line == *"$ERR"* ]]
then
exit;
fi
done
if [ "$1" == "help" ]
then
show_help
fi
if [ -z "$2" ]
then
show_help
fi
ERR=$1
shift;
$* |
while IFS= read -r line
do
echo $line
if [[ $line == *"$ERR"* ]]
then
exit;
fi
done
/q
选项...太酷了!