Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

How to use "not equal to " in IF statement

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 10-01-2008
manmeet manmeet is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 1 April 2009, 5:50 PM EDT
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
How to use "not equal to " in IF statement

Hi,
i need to compare two variables by using if statement
if [ $var1 -ne $var2 ]
then
echo "$var1"
else
echo "$var2"
fi

where -ne is used for not equal to but this is not working and giving me a syntax error. can anybody tel me how use not equal to with 'if' in ksh

thanks
Sponsored Links
    #2  
Old Unix and Linux 10-01-2008
Ikon's Unix or Linux Image
Ikon Ikon is offline Forum Advisor  
Computer Geek
 
Join Date: Jul 2008
Last Activity: 15 January 2015, 10:57 AM EST
Location: Frederick, MD
Posts: 748
Thanks: 4
Thanked 11 Times in 10 Posts

Code:
if [ $var1 != $var2 ] 
then 
echo "$var1"
else 
echo "$var2"
fi

Sponsored Links
    #3  
Old Unix and Linux 10-01-2008
manmeet manmeet is offline
Registered User
 
Join Date: Sep 2008
Last Activity: 1 April 2009, 5:50 PM EDT
Posts: 51
Thanks: 0
Thanked 0 Times in 0 Posts
thanks Ikon.... it's working fine now.. Linux
    #4  
Old Unix and Linux 10-01-2008
yahyaaa's Unix or Linux Image
yahyaaa yahyaaa is offline
Registered User
 
Join Date: May 2008
Last Activity: 11 September 2012, 9:54 AM EDT
Location: Jordan
Posts: 107
Thanks: 2
Thanked 0 Times in 0 Posts
try to do it in KSH....

#!/bin/ksh
if [[ "$var1" -ne "$var2" ]] ; then

echo "$var1"

else

echo "$var2"

fi
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
What is the meaning of "-s" option in "if" statement? rymnd_12345 UNIX for Dummies Questions & Answers 7 05-03-2012 12:22 PM
awk command to replace ";" with "|" and ""|" at diferent places in line of file shis100 Shell Programming and Scripting 7 03-16-2011 08:59 AM
What "-a" operator means in "if" statement aoussenko Shell Programming and Scripting 1 03-02-2011 10:30 AM
"if" and "then" statement is not working in RedHat Afi_Linux Red Hat 10 01-28-2011 03:26 AM



All times are GMT -4. The time now is 01:45 PM.