Skip to content

Instantly share code, notes, and snippets.

@i97506051502
Created February 15, 2017 07:39
Show Gist options
  • Save i97506051502/4b5f686885a25f62739c861ee8ceb260 to your computer and use it in GitHub Desktop.
Save i97506051502/4b5f686885a25f62739c861ee8ceb260 to your computer and use it in GitHub Desktop.
wait user input by using while loop and read command in while loop sample.
#!/bin/bash
# wait_by_read_with_while_loop.txt
# one
# two
# three
# four
# five
set -ue
exec 3<&0 < $(dirname $0)/wait_by_read_with_while_loop.txt
# exec 3< 新たなファイルディスクリプタ fd3 を開く.
# 3<&0 標準入力 &0 を fd3 に向ける(キーボード入力待ち用のファイルディスクリプタとして fd3 を使う).
# < file file の内容を標準入力として受け取る.
# 以下の説明はかなり怪しい(最初に書いたバージョン).
# foo < file だと,file の内容を fd0 を経由して foo の標準入力として渡す.
# foo 3<&0 < file だと,file の内容を fd3 を経由して foo の標準入力として渡す.(この説明はかなり怪しい)
while read line; do
echo "${line}"
if [[ "${line}" = 'three' ]]; then
echo -n 'Are you sure to do this ? [y/n] > '
while true; do
#exec 3<&0
#read user_answer
#read user_answer <&3
read -u 3 user_answer
echo "${user_answer}"
case "${user_answer}" in
"y" )
echo 'Your input is 'y'.'
break
;;
"n" )
echo 'Your input is 'n'.'
break
;;
* )
echo 'Input word, y or n.'
exit 1
;;
esac
done
fi
#done < $(dirname $0)/wait_by_read_with_while_loop.txt
done
exec 0<&3 3<&-
# exec 0<&3 `exec 3<&0` で,標準入力がファイルディスクリプタ fd3 になっているのを,fd0 に戻す.
# 3<&- ファイルディスクリプタ fd3 を削除する.
# 以下の説明はかなり怪しい(最初に書いたバージョン).
# exec 3<&0 < /var/tmp/wait_by_read_with_while_loop.txt
# 上記で標準入力のファイルディスクリプタを fd3 にしているので,以下で標準入力を fd0 に戻し,fd3 (の内容を) 削除する.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment