a simple doubt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting a simple doubt
# 1  
Old 01-23-2006
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  
Old 01-23-2006
(( )) 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  
Old 01-23-2006
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  
Old 01-23-2006
Data 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  
Old 01-23-2006
Lightbulb pleae clear

Should I use '!=' instead of '-ne' when I use (( )). Is it like C coding.
Regards,
Ranjith
# 6  
Old 01-23-2006
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  
Old 01-23-2006
Data 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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Doubt

in my shell script requirement is to search and replace the file with variable so i use the following command sed -i "s/abc/$SCHEMA/g" table.sql later when the script runs sqlplus username/pwd@Table& this & is not letting the variable to replace the value inside the file .please let me know... (1 Reply)
Discussion started by: bhuvan1
1 Replies

2. UNIX for Dummies Questions & Answers

A Simple Clock, Well Maybe Not That Simple...

The attachment says it all really... It is a DEMO at a glance digital readout using the "date" command to make it useful... For a Mocbook Pro 13", OSX 10.7.5, but may well work on Linux variants too. Enjoy... #!/bin/bash # # Clock.sh # A bash DEMO to create a 6 x 7 character set... (4 Replies)
Discussion started by: wisecracker
4 Replies

3. Red Hat

Syslog.conf: looking for a simple answer on a simple question

Cheers! In /etc/syslog.conf, if an error type is not specified, is it logged anywhere (most preferable is it logged to /var/log/messages) or not? To be more precise I am interested in error and critical level messages. At default these errors are not specified in syslog.conf, and I need to... (6 Replies)
Discussion started by: dr1zzt3r
6 Replies

4. Shell Programming and Scripting

Help making simple perl or bash script to create a simple matrix

Hello all! This is my first post and I'm very new to programming. I would like help creating a simple perl or bash script that I will be using in my work as a junior bioinformatician. Essentially, I would like to take a tab-delimted or .csv text with 3 columns and write them to a "3D" matrix: ... (16 Replies)
Discussion started by: torchij
16 Replies

5. UNIX for Dummies Questions & Answers

Doubt

Hi , Struck with one basic question. Iam expecting word count of 4 where "wc" is showing as 5 . # echo "abcd" | wc 1 1 5 # echo abcd | wc 1 1 5 (5 Replies)
Discussion started by: penchal_boddu
5 Replies

6. Shell Programming and Scripting

simple doubt..

can we use variables containing file names as filenames to commands like tar? please give an example..:confused: (1 Reply)
Discussion started by: Amruthesh C
1 Replies

7. Shell Programming and Scripting

Doubt??

I jus want to know the meaning of the below command line(exclamation following that re-direction) sqlplus -s `cat /home/sample.txt` <<! Thanks!! (1 Reply)
Discussion started by: nohup
1 Replies

8. Shell Programming and Scripting

Simple to you not simple to me pattern matchin help

hey all, im new and my first question is: say i have a word "blahblah" how do i get and replace the last letter of the word with say k, so replace the h with a k. However you cant just replace the h it has to change the LAST LETTER of the word. Cheers In advance. :b: (0 Replies)
Discussion started by: aleks001
0 Replies

9. Programming

Simple C question... Hopefully it's simple

Hello. I'm a complete newbie to C programming. I have a C program that wasn't written by me where I need to write some wrappers around it to automate and make it easier for a client to use. The problem is that the program accepts standard input to control the program... I'm hoping to find a simple... (6 Replies)
Discussion started by: Xeed
6 Replies

10. UNIX for Dummies Questions & Answers

Ok simple question for simple knowledge...

Ok what is BSD exactly? I know its a type of open source but what is it exactly? (1 Reply)
Discussion started by: Corrail
1 Replies
Login or Register to Ask a Question