Register
It is currently Wed Apr 23, 2014 5:37 pm

If Statement Trouble


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Wed Jul 21, 2010 1:42 pm   

Joined: Wed Jul 21, 2010 1:34 pm
Posts: 1
I have been trying to get this to work for way too long and I havent found an answer online that fixed my problem yet

I want to give the user the option of 2 commands, backup or restore
the if statement will not work for me
I am used to programming, but I havent programmed a bash script before so I am still learning all of the syntax

PLEASE do me a favor and if you are going to post code you suggest I try, DO NOT type it in sudo-code, that is not helpful for someone who is learning
please take the extra 1-2 minutes to type out the code as you would really place it into the bash file

(rough in code of basicly what I am doing below)

read -p "enter b or r" ask
if [ $ask == 'b' ] (NOTE: I have tried almost every variation of this I can think of with ", ' $ or not = or == no spaces with spaces, I am lost at this point, please help)
then
echo "woo b"
elif [ $ask == 'r' ]
then
echo "woo r"
else
echo "you screwed up"


Thanks for the help


Top
 Profile  
 PostPosted: Wed Jul 21, 2010 2:47 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 512
hi,

you need to close an if statement using fi
you don't need two = in single square brackets.
you should also check $ask is a single char.

see you soon :)


Top
 Profile  
 PostPosted: Thu Jul 22, 2010 3:32 am   
Moderator
User avatar

Joined: Thu Oct 11, 2007 7:12 am
Posts: 224
Location: London - UK
Code:

read -p "enter b or r: " ask
if [ "$ask" == 'b' ]
then
  echo "woo b"
elif [ "$ask" == 'r' ]
then
  echo "woo r"
else
  echo "you screwed up"
fi


Top
 Profile  
 PostPosted: Thu Jul 22, 2010 10:17 pm   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 657
Location: Des Moines, Iowa
A more elegant solution might be to use "case"

Code:
#!/bin/bash
read -p "enter b or r: " ask
   case $ask in
      b)   echo "woo b";
         ;;
      r|R)   echo "woo r";
         ;;
      *)     echo "you screwed up";
         ;;
   esac
exit 0


Notice the r|R ............ this will allow a "R" or "r" to successfully work for that option.
Hope that is helpful.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 3 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