Skip to content

Instantly share code, notes, and snippets.

@koyudoon
Last active August 29, 2015 14:06
Show Gist options
  • Save koyudoon/9ddf71f195ef319a6bc7 to your computer and use it in GitHub Desktop.
Save koyudoon/9ddf71f195ef319a6bc7 to your computer and use it in GitHub Desktop.
ShellScript で標準入力を処理したいとき (間違っている可能性大)
$ sh stdin.sh
端末
$ sh stdin.sh < /dev/null
リダイレクト
stdin:
$ cat /dev/null | sh stdin.sh
名前付きパイプ
stdin:
$ echo 'hoge' | sh stdin.sh
名前付きパイプ
stdin: hoge

$ cat hoge.txt          
hoge
$ sh stdin.sh < hoge.txt
リダイレクト
stdin: hoge
#! /bin/sh
if [ -t 0 ]; then
echo '端末'
else
if [ -p /dev/stdin ]; then
echo '名前付きパイプ'
else
echo 'リダイレクト'
fi
STDIN=$(cat -)
echo "stdin: $STDIN"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment