LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-08-2007, 08:19 PM   #1
sjcorp
LQ Newbie
 
Registered: Oct 2007
Posts: 8

Rep: Reputation: 0
Question I need help with "if -else" statement


I am trying to do a simple file, but I cannot get it to work. Can you please let me know what I may be doing wrong. I am typing this using pico.

#!/bin/bash

echo -n "What is your last name?>"
read text
echo -n "Are you male or female?>"
read sex
if ($sex = "male"); then
echo "Hello Mr. $text"
else
echo "Hello Ms. $text"
fi

This is the output I receive:

What is your last name?>mmmmmmmm
Are you male or female?>male
./morf.test: line 7: male: command not found
Hello Ms. mmmmmmmm

Last edited by sjcorp; 10-08-2007 at 08:45 PM.
 
Old 10-08-2007, 08:25 PM   #2
pljvaldez
LQ Guru
 
Registered: Dec 2005
Location: Somewhere on the String
Distribution: Debian Wheezy (x86)
Posts: 6,094

Rep: Reputation: 271Reputation: 271Reputation: 271
Don't you want a single = instead of -eq? I think "-eq" is for arithmetic only. You're comparing strings...
 
Old 10-08-2007, 08:27 PM   #3
ehawk
Senior Member
 
Registered: Jul 2003
Posts: 1,257

Rep: Reputation: 48
I don't program in the language you are using, but it may have something to do with the variable type of "text". Make sure you are making it a string variable and that the -eq operator can act upon string variables. It may be that the variable "male" is being converted to its ascii numeric value and then compared with your entry, or that that -eq cannot act upon the type of variable or the entry you supplied to the read command.

Hope this helps.
 
Old 10-08-2007, 08:28 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
In shell, use the 'wordy' operators eg -eq for numeric comaprisons, and the symbolic eg '=' operators for strings.
http://www.tldp.org/LDP/abs/html/comparison-ops.html
 
Old 10-09-2007, 10:41 AM   #5
jaseka
LQ Newbie
 
Registered: Oct 2007
Posts: 15

Rep: Reputation: 0
This code may serve the purpose


Quote:
echo -n "What is your last name?>"
read text
echo -n "Are you male or female?>"
read sex
o -n "What is your last name?>"
read text
echo -n "Are you male or female?>"
read sex
if [ $sex = "male" ]; then
echo "Hello Mr. $text"
else
echo "Hello Ms. $text"
fi
 
Old 10-09-2007, 10:53 AM   #6
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Unless I am mistaken, you want a == in your if statement, not a single =.
Code:
#!/bin/bash

echo -n "What is your last name?>"
read text
echo -n "Are you male or female?>"
read sex
if [ $sex == "male" ]; then
   echo "Hello Mr. $text"
else
   echo "Hello Ms. $text"
fi
edit: and you need square brackets around an if statement.
edit2: it seems I am mistaken, a single = works fine. Every day's a school day

Last edited by pwc101; 10-09-2007 at 10:58 AM.
 
Old 10-09-2007, 04:55 PM   #7
student04
Member
 
Registered: Jan 2004
Location: Georgia
Distribution: OS X, CentOS
Posts: 669

Rep: Reputation: 34
Here are some websites to use as reference and as tutorials for scripting in bash:

http://www.panix.com/~elflord/unix/bash-tute.html
http://www.tldp.org/LDP/abs/html/
http://www.telematica.polito.it/mell...in20pages.html

I found these to be quite useful.

Enjoy.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Linux Foundation Releases Statement Calling for National Bodies to Vote "No" on LXer Syndicated Linux News 0 08-30-2007 08:00 AM
"[: too many arguments" Error In An If Statement Mr_Safe Programming 4 07-02-2007 09:46 PM
LXer: Position Statement to the Dunc-Tanc "experiment" LXer Syndicated Linux News 0 10-27-2006 02:03 AM
Pattern matching with a "case" statement in BASH King V Programming 8 04-27-2006 05:19 PM
Bash script problems with "if" statement adz Programming 2 05-22-2004 04:34 AM


All times are GMT -5. The time now is 02:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration