Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash user input if

I am tring to do simple

Do you want to do that? [Y,n] _ 

question in bash.

i tried

echo "Do that? [Y,n]" read DO_THAT if ["DO_THAT"="y"]; then   do_that fi 

but it fails: bash: [y=y]: command not found

what am I doing wrong??!

like image 640
frnhr Avatar asked Apr 04 '11 17:04

frnhr


People also ask

How do you prompt for input from user in Linux shell script?

You can use the built-in read command ; Use the -p option to prompt the user with a question.

What is in Bash if?

What is the Bash if Statement? Bash scripts help automate tasks on your machine. The if elif else statement in bash scripts allows creating conditional cases and responses to specific code results. The if conditional helps automate a decision-making process during a program.

What is Z in Bash if?

Use the -z Flag in Bash The -z flag is a parameter that checks if the length of a variable is zero and returns true if it is zero. In the example below, the -z flag is used with the test command, and it is tested whether the given string is empty. Bash.


1 Answers

You might consider explicit prompting: -p and specifying 1-character-input -n1 which allows to insert y without ENTER.

read -n1 -p "Do that? [y,n]" doit  case $doit in     y|Y) echo yes ;;    n|N) echo no ;;    *) echo dont know ;;  esac 
like image 127
user unknown Avatar answered Sep 28 '22 06:09

user unknown