LinuxQuestions.org
Register a domain and help support LQ
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 04-22-2011, 05:17 AM   #1
hayloiuy
LQ Newbie
 
Registered: Apr 2011
Location: Malaysia
Distribution: Ubuntu
Posts: 6

Rep: Reputation: 0
My If else statement only enters the first one?


Code:
#!/bin/bash


echo $1

if [ $1=="A" ];
	
	then echo 0 > Type.txt
else
	
	if [ $1=="B" ];
		then echo 1 > Type.txt
	else
		if [ $1=="C" ];
			then echo 2 > Type.txt
		else
			if [ $1=="D" ];
				
				then echo 3 > Type.txt
					
			else
				echo "Fail"
			fi
		fi
	fi
fi
My code seems to always think the first if statement is true. What is wrong?
 
Old 04-22-2011, 05:30 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529Reputation: 3529
If you run the script as 'bash -vx /path/to/script.sh' you can see all variables filled in etc, etc. Also note there's no need for nesting so:
Code:
if [ "$1" = "A" ]; then
        echo 0 > Type.txt
elif [ "$1" = "B" ]; then
        echo 1 > Type.txt
elif [ "$1" = "C" ]; then
        echo 2 > Type.txt
elif [ "$1" = "D" ]; then
        echo 3 > Type.txt
else
        echo "Fail"
fi
should do and you could alternatively use a case statement:
Code:
case "$1" in
A) echo 0 > Type.txt;;
B) echo 1 > Type.txt;;
C) echo 2 > Type.txt;;
D) echo 3 > Type.txt;;
*) echo "Fail";;
esac


Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/ 
http://wooledge.org/mywiki/BashFAQ?action=show&redirect=BashFaq 
http://wooledge.org/mywiki/BashPitfalls"; }
 
1 members found this post helpful.
Old 04-22-2011, 09:52 AM   #3
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Quote:
Originally Posted by hayloiuy View Post
My code seems to always think the first if statement is true. What is wrong?
You didn't put spaces around the "=" sign.

It's not some kind of C-like expression, "[" is a command. You can put any other command after it, too.

The "[" command is basically the test command, but it requires the last argument to be "]". Read man test for more info.

http://mywiki.wooledge.org/BashPitfa..._.3D_d_.5D_.5D

Last edited by MTK358; 04-22-2011 at 09:54 AM.
 
1 members found this post helpful.
  


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
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 05:50 AM
[SOLVED] Shell script for adding a statement in a file after a particular statement TheIndependentAquarius Programming 4 06-28-2010 04:07 AM
Problem with if statement in a find -exec statement romsieze Programming 2 10-02-2008 01:38 AM
Case statement with If statement cbo0485 Linux - Newbie 4 11-07-2007 09:05 PM
window re-enters ... half-steper Red Hat 1 02-12-2006 08:22 AM


All times are GMT -5. The time now is 08:01 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