I just wanted to write a small sript for copying some files for my NAS, so I'm not very experienced in Shell-Scripting. I know that many command line tools on Linux use the following sheme for Yes/No inputs
Are you yure [Y/n]
where the capitalized letter indicates the standard action which would also be started by hitting Enter. Which is nice for a quick usage.
I also want to implement something like this, but I have some trouble with caching the Enter key. Here is what I got so far:
read -p "Are you sure? [Y/n] " response
case $response in [yY][eE][sS]|[yY]|[jJ]|[#insert ENTER codition here#])
echo
echo files will be moved
echo
;;
*)
echo
echo canceld
echo
;;
esac
I can add what ever I want but it just won't work with Enter.
Here's a quick solution:
read -p "Are you sure? [Y/n] " response
case $response in [yY][eE][sS]|[yY]|[jJ]|'')
echo
echo files will be moved
echo
;;
*)
echo
echo canceled
echo
;;
esac
If you are using bash
4, you can "pre-seed" the response with the default answer, so that you don't have to treat ENTER
explicitly. (You can also standardize the case of response
to simplify the case
statement.
read -p "Are you sure? [Y/n] " -ei "y" response
response=${response,,} # convert to lowercase
case $response in
y|ye|yes)
echo
echo files will be moved
echo
;;
*)
echo
echo cancelled
echo
;;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With