Skip to content

Instantly share code, notes, and snippets.

@gongo
Created July 30, 2015 02:10
Show Gist options
  • Save gongo/284190a40c1fc7b00f71 to your computer and use it in GitHub Desktop.
Save gongo/284190a40c1fc7b00f71 to your computer and use it in GitHub Desktop.
バックグラウンド実行中のプログラムが特定の文字列を出力するのを待って kill する系
#!/bin/bash
PIPEFILE="pipefile"
BKPID=""
BK=`cat << 'EOL'
for i in $(seq 1 30) ; do
echo $i
sleep 1
done
EOL`
[[ -p "$PIPEFILE" ]] || mkfifo "$PIPEFILE"
bash -c "$BK" > "$PIPEFILE" & BKPID="$!"
echo "$BKPID"
while read LINE ; do
echo $LINE
if [[ "$LINE" == "8" ]] ; then
kill -s TERM "$BKPID"
break
fi
done < "$PIPEFILE"
@gongo
Copy link
Author

gongo commented Jul 30, 2015

出力結果

-> % ./loop.sh
69136
1
2
3
4
5
6
7
8

@gongo
Copy link
Author

gongo commented Jul 30, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment