Linux シェルスクリプトでユーザー入力を取得する
Yahya Irmak
2023年1月30日
Linux
Linux Input
-
Linux で入力を取得するには
read
を使用する -
Linux でオプションを提供するには、
select
を使用する -
Linux で GUI を介して入力を取得するには、
whiptail
を使用する

この記事では、Linux シェルスクリプトでユーザー入力を取得する方法を説明し、いくつかの例を示します。
read
:ユーザーからの入力を受信しますselect
:オプションを提供しますwhiptail
:ユーザーインターフェイスを使用する
Linux で入力を取得するには read
を使用する
read
コマンドは、ユーザーからの入力を受け取り、それを変数に格納します。
read -p "Do you want to continue? (Y/y for Yes, any other key for No) " answer
case $answer in
[Yy]* ) echo "Program continues..."; break;;
* ) echo "Program exits."; exit;;
esac
出力:
Linux でオプションを提供するには、select
を使用する
select
コマンドはユーザーにオプションを提供し、回答を変数に格納します。
echo "Do you want to continue? "
select answer in "Yes" "No"; do
case $answer in
Yes ) echo "Program continues..."; break;;
No ) echo "Program exits."; exit;;
esac
done
Linux で GUI を介して入力を取得するには、whiptail
を使用する
whiptail
コマンドは、ビジュアルインターフェイスに Yes/No
オプションを提供します。
システムに応じて、dialog
、gdialog
、または kdialog
を使用できます。
if whiptail --yesno "Do you want to continue? " 10 40 ;then
echo "Program continues...";
else
echo "Program exits."
fi
チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe
著者: Yahya Irmak
Yahya Irmak has experience in full stack technologies such as Java, Spring Boot, JavaScript, CSS, HTML.
LinkedIn