LoginSignup
2
1

More than 3 years have passed since last update.

Linux メモ ユーザー入力を受け付ける

Last updated at Posted at 2021-04-10

サンプルコード1

echo -n 'input something: '
read input

echo "$input"

サンプルコード2

question.sh
read -p 'USER NAME: ' user_name
stty -echo
read -p 'PASSWORD: ' password
stty echo

echo -e '\n'
echo "Hi! $user_name"
echo "$password"

複数入力を受け付ける方法はこちら

パスワードの非表示入力をしたい場合

ユーザー入力部分をパイプラインで渡す

パイプラインでサンプルコード2のスクリプトにユーザー名・パスワードを渡す

$ printf 'hello\npass' | sh qustion.sh
stty: standard input: Inappropriate ioctl for device
stty: standard input: Inappropriate ioctl for device


Hi! hello
Your Password: pass

コメントにて

コメントで教えて頂きました。
ユーザーの入力文字を非表示にしたい場合、read -s -p のように-sオプションをつけると、sttyが不要になります。stty: standard input: Inappropriate ioctl for device のエラーも消えました。

question.sh
read -p 'USER NAME: ' user_name
read -s -p 'PASSWORD: ' password

echo -e '\n'
echo "Hi! $user_name"
echo "Your Password: $password"
$ printf 'hello\npass' | sh qustion.sh


Hi! hello
Your Password: pass

参考記事

2
1
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1