Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

When user doesnt enter any choice

I need to ask user in unix shell script to enter input to proceed further in a code block. Need to display ‘Hi’ only when user enters Y (case sensitive) otherwise just display ‘Bye’. ‘Bye’ should be displayed when user hits enter key on keyboard or when he wont enter any input.

Also, please note that, I need to show either of echo messages based on user input and finally reach ‘Proceeding to next line of code’ code as well.

I am getting below error for else condition when user hits enter key
[: ==: unary operator expected

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

echo 'Do you want to proceed?'
read i
if [ $i == 'Y' ]
then
   echo 'Hi'
else
   echo 'Bye'
fi

echo 'Proceeding to next line of code'

>Solution :

Just add double quotes in your variable:

echo 'Do you want to proceed?'
read i
if [ "$i" == 'Y' ]
then
    echo 'Hi'
else
    echo 'Bye'
fi
echo 'Proceeding to next line of code'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading