The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
if statement ramky79 Shell Programming and Scripting 6 05-29-2008 12:17 AM
IF Statement koti_rama Shell Programming and Scripting 3 04-29-2008 01:48 AM
If statement - How to write a null statement april Shell Programming and Scripting 3 04-16-2008 10:14 AM
IF statement on a df -g hassanj UNIX for Advanced & Expert Users 1 12-18-2007 03:41 AM
If statement mariner UNIX for Advanced & Expert Users 4 12-16-2004 03:21 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-01-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 128
Arrow using && in if statement ..

Hi All,

Can some one tell me how to get run the following:

data1="hello"
data2="world"
if [ "$data2" != "world" && "$data1" != "hello"]
then
{
echo "good afternnon"
}
else
{
echo " good morning"
}
fi


The above code gives me an error ad below :
./if.h: line 3: [: missing `]'

Thanks in advance
Js
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-01-2008
Registered User
 

Join Date: Apr 2007
Location: Kolkata
Posts: 81
Thumbs up

Dear jisha,

try this

data1="hello"
data2="world"
if [ "$data2" != "world" ]
then
if [ "$data1" != "hello" ]
then
echo "good afternnon"
fi
else

echo " good morning"
fi

Regards,
Pankaj
Reply With Quote
  #3 (permalink)  
Old 02-01-2008
Tytalus's Avatar
Registered User
 

Join Date: Jun 2003
Location: Scotland
Posts: 217
try:

Code:
if [ "$data2" != "world" ] && [ "$data1" != "hello" ]
Reply With Quote
  #4 (permalink)  
Old 02-01-2008
Registered User
 

Join Date: Nov 2007
Location: Belgium & France
Posts: 70
Don't forget to let a space before ] and after [ !
Here is the code :
Code:
data1="hello"
data2="world"
if [ "$data2" != "world" ] && [ "$data1" != "hello" ]
then
{
        echo "good afternnon"
}
else
{
        echo " good morning"
}
fi
Reply With Quote
  #5 (permalink)  
Old 02-01-2008
manas_ranjan's Avatar
Registered User
 

Join Date: Jul 2007
Location: PUNE
Posts: 157
which code is this ???? shell script ?????
Reply With Quote
  #6 (permalink)  
Old 02-01-2008
Registered User
 

Join Date: Jan 2008
Location: Bangalore,India
Posts: 128
Thanks to all !!

Its working now.. I did a lot of debugging still i didnot get ...I think the problem was with the spacing

Thank you so much

Js
Reply With Quote
  #7 (permalink)  
Old 02-01-2008
Moderator
 

Join Date: Dec 2003
Location: /ksh93
Posts: 863
If you want to make your schell script more efficient you should use the shell builtins

Code:
#!/usr/bin/ksh

data1="hello"
data2="world"

if [[ "$data2" != "world" && "$data1" != "hello" ]]
then
    print "good afternnon"
else
    print "good morning"
fi
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0