The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
SDL doubt royalibrahim High Level Programming 0 11-22-2007 01:49 AM
awk doubt.. esham Shell Programming and Scripting 11 10-11-2007 02:24 AM
Simple to you not simple to me pattern matchin help aleks001 Shell Programming and Scripting 0 07-22-2007 10:06 PM
Ok simple question for simple knowledge... Corrail UNIX for Dummies Questions & Answers 1 11-28-2005 01:03 PM
doubt it sed esham Shell Programming and Scripting 2 03-08-2005 01:52 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-23-2006
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
a simple doubt

Hi all
I have a small doubt. Can we do an if compare using if (( )). In what conditions will it fail.
Regards,
Ranjith
  #2 (permalink)  
Old 01-23-2006
mona's Avatar
mona mona is offline
Registered User
  
 

Join Date: Nov 2005
Location: Singapore
Posts: 96
(( )) can be used if you are doing numeric comparisons. It will not work well with the String comparison for which you have to use [[ ]].

An Example

Code:
/export/home/test/mons>if (( 10 == 10 ))
> then
> echo "Equal"
> else
> echo "Not equal"
> fi
Equal

Code:
/export/home/test/mons>if (( "hai" == "hai" ))
> then
> echo "Equal"
> else
> echo "Not equal"
> fi
ksh:  hai == hai : bad number

Code:
/export/home/test/mons>if [[ "hai" == "hai" ]] 
> then
> echo "Equal"
> else
> echo "Not equal"
> fi
Equal

Last edited by mona; 01-23-2006 at 08:36 AM.. Reason: Added Example
  #3 (permalink)  
Old 01-23-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by ranj@chn
Hi all
I have a small doubt. Can we do an if compare using if (( )). In what conditions will it fail.
Regards,
Ranjith
If the expression within (( )) comes out to be a 0, then the if fails.

Code:
[/tmp]$ cat test.sh
#! /bin/ksh

L=0
if ((L=L+1)) ; then
echo $L
else
echo "Failed"
fi; 
if ((L=L-1)) ; then
echo $L
else
echo "Failed"
fi; 
[/tmp]$ ./test.sh
1
Failed
[/tmp]$
  #4 (permalink)  
Old 01-23-2006
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
Unhappy some explanation

I have a small script flow like this
#! /bin/ksh
testscpt.sh
pgmcc=$?
if (( $pgmcc -ne 0 && $pgmcc -ne 99 ))
then
echo "Correct condition check"
else
echo "Failed"
fi

FIrst, I am getting syntax error with the if condition. Second, the testscpt.sh always returns 0. What will happen to this check if the testscpt.sh returns value other than 0 or 99.
  #5 (permalink)  
Old 01-23-2006
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
Lightbulb pleae clear

Should I use '!=' instead of '-ne' when I use (( )). Is it like C coding.
Regards,
Ranjith
  #6 (permalink)  
Old 01-23-2006
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
Quote:
Originally Posted by ranj@chn
I have a small script flow like this
#! /bin/ksh
testscpt.sh
pgmcc=$?
if (( $pgmcc -ne 0 && $pgmcc -ne 99 ))
then
echo "Correct condition check"
else
echo "Failed"
fi

FIrst, I am getting syntax error with the if condition. Second, the testscpt.sh always returns 0. What will happen to this check if the testscpt.sh returns value other than 0 or 99.
First, how can you get two exit values from a single running of the script ?
if (( $pgmcc -ne 0 && $pgmcc -ne 99 )) should be
if (( $pgmcc -ne 0 || $pgmcc -ne 99 ))

I think you should change ((expr)) to [[expr]] to satisfy your requirements.
  #7 (permalink)  
Old 01-23-2006
ranj@chn ranj@chn is offline Forum Advisor  
Playing with Ubuntu Now!
  
 

Join Date: Oct 2005
Location: Chennai
Posts: 365
Unhappy script written like that

THe testscpt.sh returns multiple return codes based on the error conditions. But it returns only one code at any particular time. We had a problem with the script a few days back and on analysing found the issue due to this check. THe script shown above is a small representation of the the actual code.
Normally, the testscpt.sh always returns 0, but a few days back, it failed due to DB issues and returned 1. The if condition should have caught that, but it failed and it led to lot of processing issues. We are currently planning to make changes to this part of the code.
Even, we are not sure why the '&&' has been included. Is it due to the way (( )) evaluates its expression.
Thanks for your inputs. We were lost/perplexed from the morning.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:07 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0