IF statement failure


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting IF statement failure
# 1  
Old 03-31-2010
IF statement failure

Hi Guys,

i have a variable which hold a string value of "in" when i try to compare this value against another value the if statement is not working.

Code:
$a=in
 
if [ "$a" == "=" ]
then
    echo "Not a Walid match"
else 
    echo "Walid Match"
fi

Thank You.

Last edited by pludi; 03-31-2010 at 02:27 AM.. Reason: code tags, please...
# 2  
Old 03-31-2010
try this
if($a eq "=")
# 3  
Old 03-31-2010
tried the way u said ... still its not working error message : binary operator expected

code

Code:
if [ $a eq "=" ]; then echo "Not a walid match"; else echo "walid match"; fi

suppose if i change to

Code:
if [ $a == "=" ]; then echo "Not a walid match"; else echo "walid match"; fi

it works but the output is "Walid Match" where as my ouput should be "Not a walid Match"

Last edited by pludi; 03-31-2010 at 02:39 AM.. Reason: code tags, please...
# 4  
Old 03-31-2010
copy "=" to a variable
$b = "="
# 5  
Old 03-31-2010
Quote:
Originally Posted by nitinrp1
Code:
$a=in
 
if [ "$a" == "=" ]
then
    echo "Not a Walid match"
else 
    echo "Walid Match"
fi

The red '$' is your problem. Remove it, and it should work. To check for further errors, run (depending on your shell)
Code:
ksh -n your.script
bash -n your.script

Also, if you're assigning strings it's always a good idea to put quotes around them.
# 6  
Old 03-31-2010
Moderator,

that was a typo in my script ... the $ is not there in my script ... but still its not working and not giving me the excepted result ... pls help
# 7  
Old 03-31-2010
Your logic is reversed : you try if $a equal '=' then not valid match
Use '!=' (not equal) or reverse the statements
Code:
a=in
if [ "$a" != "=" ]
then
    echo "Not a Walid match"
else
    echo "Walid Match"
fi

That could also be written
Code:
a=in
[ "$a" = "=" ] && echo "Valid Match" || echo "Not a Valid match"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

2. UNIX for Dummies Questions & Answers

boot up failure unix sco after power failure

hi power went out. next day unix sco wont boot up error code 303. any help appreciated as we are clueless. (11 Replies)
Discussion started by: fredthayer
11 Replies

3. Solaris

Boot failure

I have installed Solaris 10 OS in Sun Virtual Box that uses x86 32 bit system. After an abnormal shutdown i'm getting the following message on the console when i try to boot. SunOS Release 5.10 Version Generic_127128_11 32-bit Copyright 1983-200 Sun Microsystems, Inc. All rights reserrved. Use... (3 Replies)
Discussion started by: Sesha
3 Replies

4. Shell Programming and Scripting

getopts case statement failure

Hi, I have a getopts that works fine if I provide an option but I want it to exit if nothing is given, but for some reason it drops out and continues with the script. Any ideas? while getopts d:m:y: o do case $o in d) day=$OPTARG ;; ... (1 Reply)
Discussion started by: nhatch
1 Replies

5. UNIX for Advanced & Expert Users

su failure

Usually when su rejects an attempt to switch user it responds with "Sorry" but with a certain username on some unix servers the response is "Killed". I'm guessing the su accepted the username/password but refused to spawn child shell with the specified username. What's causing this and what has to... (8 Replies)
Discussion started by: twk
8 Replies

6. Shell Programming and Scripting

How is use sselect statement o/p in insert statement.

Hi All, I am using Unix ksh script. I need to insert values to a table using the o/p from a slelect statement. Can anybody Help! My script looks like tihs. ---`sqlplus -s username/password@SID << EOF set heading off set feedback off set pages 0 insert into ${TB_NAME}_D... (2 Replies)
Discussion started by: nkosaraju
2 Replies

7. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies

8. Linux

Boot failure

Hi all I used a dual boot operating system and it works fine for me. Now , i install a Ati radeon 9250 Agp card on my system and this results in boot failure of fedora 6. The graphics card is working fine with windows XP , i.e i have no compatibility issues.The system also refuses to boot when i... (2 Replies)
Discussion started by: joshighanshyam
2 Replies

9. UNIX for Advanced & Expert Users

dtdbcache failure

Hi, I'm using WRQ Reflections 12.0.6 on Windows XP to open a Xsession to HP 11.11 server. After providing password, the CDE window is displayed and then receive a "dtdbcache failure" message "Couldn't recreate the Desktop Actions/Datatypes Database. Check disk space and/or permissions." I... (3 Replies)
Discussion started by: Leese
3 Replies

10. Programming

ld failure

Hi, I am using gmake to compile a c program with a makefile. The make file runs ld. I get the following error jsh1035c:/users/egate453/admegate/kapil/samples $ gmake -e -f GNUmakefile queue_c gmake -f ./GNUmakefile queue_c in_objdir=1 build_root=/users/egate453/admegate/kapil/samples... (2 Replies)
Discussion started by: handak9
2 Replies
Login or Register to Ask a Question