Register
It is currently Sun Nov 23, 2014 8:27 am

Checking a users respone is bash. FIXED


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Mon May 01, 2006 6:00 am   

Joined: Fri Oct 14, 2005 9:30 am
Posts: 61
Firstly yes I have posted this on usalug, but wasnt getting a reply.
Ok here is the script:
Code:
#!/bin/bash
function beans ()
{
#check they answered Yes, yes, y, Y, No, no, n, N
while [ $answer != "No" ] && [ $answer != "no" ] && [ $answer != "Yes" ] && [ $answer != "yes" ] && [ $answer != "y" ] && [ $answer != "n" ] && [ $answer != "Y" ] && [ $answer != "N" ]
do
#if not run function "potatoe"
potatoe
done
#If they have assign a action to their answer.
if [ $answer != "No" ] && [ $answer != "no" ] && [ $answer != "n" ] && [ $answer != "N" ]
then
echo "You said yes"
elif [ $answer != "Yes" ] && [ $answer != "yes" ] && [ $answer != "Y" ]&& [ $answer != "y" ]
then
echo "You said no."
fi
}
function potatoe ()
{
#they have not entered a valid answer.
echo "That is not a valid answer"
echo ""
#call function "sausage"
sausage
}
function sausage ()
{
#ask question again.
read -p "Do you like me? Enter Y/N : " answer
#If they asnwer no then...
if [[ $answer != "Y" ]] && [[ $answer != "y" ]] && [[ $answer != "yes" ]] && [[ $answer != "Yes" ]]
then
echo "You said no"
#If they answered yes then...
elif [[ $answer != "N" ]] && [[ $answer != "n" ]] && [[ $answer != "no" ]] && [[ $answer != "No" ]]
then
echo "You said yes"
fi
}
#script starts here
#Ask the user a simple question
read -p "Do you like me? Enter Y/N : " answer
#call the function "beans"
beans

What I want to do is check to see if the user has not entered anything - just pressed enter, because otherwise the script quite after an error:
Code:
[rob@/home/rob 2.6.15-1.2054_FC5]: ./test2
Do you like me? Enter Y/N :
./test2: line 5: [: !=: unary operator expected
./test2: line 9: [: !=: unary operator expected
./test2: line 12: [: !=: unary operator expected

What to do?
Thanks,
Rob.


Last edited by crudball on Mon May 01, 2006 7:09 am, edited 1 time in total.

Top
 Profile WWW  
 PostPosted: Mon May 01, 2006 6:59 am   
Site Admin

Joined: Tue May 17, 2005 7:31 pm
Posts: 251
Location: Georgia
i was finally able to get around to answering the post on usalug... but here's basically what i put there...

to check for an empty variable (someone just hitting enter on a read)...
check the length of the variable...
Code:
if [[ ${#answer} = 0 ]]
then
      echo "You can not use an empty variable!!"
      answer="something to make the loop start again"
fi

hope that helps
:)


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP