Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get user confirmation in fish shell?

Tags:

I'm trying to gather user input in a fish shellscript, particularly of the following oft-seen form:

This command will delete some files. Proceed (y/N)? 

After some searching around, I am still not sure how to do this cleanly.

Is these a special way of doing this in fish?

like image 795
user456584 Avatar asked May 06 '13 21:05

user456584


2 Answers

The best way I know of is to use the builtin read. If you are using this in multiple places you could create this helper function:

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 

and use it like this in your scripts/functions:

if read_confirm   echo 'Do stuff' end 

See documentation for more options: https://fishshell.com/docs/current/commands.html#read

like image 76
terje Avatar answered Sep 23 '22 00:09

terje


This does the same as the chosen answer but with only one function, seems cleaner to me:

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 

The prompt function can be inlined as such.

like image 28
Fyrn Avatar answered Sep 26 '22 00:09

Fyrn