
問題が何であるかを調べる必要があります。 if / elseステートメントでユーザーにYes / Noの選択を要求するスクリプトを作成する必要があります。通常、次のように実行されるbashスクリプトがあります。
if CONDITION; then
COMMANDS1
else
COMMANDS2
echo "Does this look okay to you? (WARNING: This action will not be recoverable!) :
PS3="Select a number: "
select yn in "Yes" "No"
do
case $yn in
"Yes" )
echo "Ok!" ; break ;;
"No" )
echo "Aborted!" ; break ;;
esac
done
fi
ただし、スクリプトを実行すると、次のように表示されます。
Does this look okay to you? (WARNING: This action will not be recoverable!)
1) Yes
2) No
Select a number: (I have to press anything here)
Select a number: (Only this input does something).
sh -vxを使用してスクリプトを実行すると、selectとcaseがそれぞれ実行されているとマークされます。 1つの入力のみが必要かどうかを確認するには?
+ echo 'Does this look okay to you? (WARNING: This action will not be recoverable!) : '
Does this look okay to you? (WARNING: This action will not be recoverable!) :
+ PS3='Select a number: '
+ select yn in '"Yes"' '"No"'
1) Yes
2) No
Select a number: 1
+ case "$yn" in
Select a number: 2
+ case "$yn" in
+ echo 'Aborted!'
Aborted!
+ break
とても感謝しています!
編集:私はMac OS 14.1でbashを実行しています。
答え1
私のために動作します。stdin
一部の入力を追加する前の入力と、(重要)改行文字に残ったテキストがあるようです。
デフォルト値を追加し*
て、保存した内容を印刷できますREPLY
。保留中のアイテムを識別するために、これを目に見える形式で表示したい場合があります。
( * ) printf '%s' "${REPLY}" | od -A n -t x1ac;;
複数の単語がここで何をするのか"$yn"
よく分からないので引用します。case