特に次のよく見られる形式の魚のシェルスクリプトでユーザー入力を収集しようとしています:
This command will delete some files. Proceed (y/N)?
いくつか検索した後、これをきれいに行う方法がまだわかりません。
これらは魚でこれを行う特別な方法ですか?
特に次のよく見られる形式の魚のシェルスクリプトでユーザー入力を収集しようとしています:
This command will delete some files. Proceed (y/N)?
いくつか検索した後、これをきれいに行う方法がまだわかりません。
これらは魚でこれを行う特別な方法ですか?
私が知っている最善の方法は、ビルトインを使用することread
です。これを複数の場所で使用している場合は、次のヘルパー関数を作成できます。
function read_confirm
while true
read -l -P 'Do you want to continue? [y/N] ' confirm
switch $confirm
case Y y
return 0
case '' N n
return 1
end
end
end
スクリプト/関数で次のように使用します。
if read_confirm
echo 'Do stuff'
end
その他のオプションについては、ドキュメントを参照してください: https://fishshell.com/docs/current/commands.html#read
これは選択した答えと同じですが、機能が1つしかないため、私にはきれいに見えます:
function read_confirm
while true
read -p 'echo "Confirm? (y/n):"' -l confirm
switch $confirm
case Y y
return 0
case '' N n
return 1
end
end
end
プロンプト関数はそのままインライン化できます。
オプションのデフォルトのプロンプトを使用したバージョンを次に示します。
function read_confirm --description 'Ask the user for confirmation' --argument prompt
if test -z "$prompt"
set prompt "Continue?"
end
while true
read -p 'set_color green; echo -n "$prompt [y/N]: "; set_color normal' -l confirm
switch $confirm
case Y y
return 0
case '' N n
return 1
end
end
end