Why does IF loop behave differently in different shells


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why does IF loop behave differently in different shells
# 1  
Old 04-11-2008
Why does IF loop behave differently in different shells

Hi , I have a script that compares two string and prints the larger string , This is an extract of a biggers script that i have.
Code:
#! /bin/ksh
DT_STRING_CMP=20081221223440
DT_STRING=20071221223440


 if [ $DT_STRING -ge $DT_STRING_CMP ]; then
                        echo "20081221223440"
                        fi
echo "Hello"

If i run it in Kshell ( my users default shell) i get output as Hello
if i run it in C hsell (my users default shell) i get output as 20081221223440 and Hello
Why is it so ?
# 2  
Old 04-11-2008
The C shell has a completely different syntax. It's weird if this works in the C shell at all.

Using csh for scripting is discouraged. If your users don't have ksh then make it run under /bin/sh (probably a good idea in any case). It has slightly more constrained syntax than ksh but this particular script should work without modification.
# 3  
Old 04-11-2008
The values are too large for ksh to be able to do a numberic comparison which is what gt -ge is doing.
# 4  
Old 04-16-2008
Is there a way to then compare such big numbers ?
# 5  
Old 04-16-2008
You can try a text compare:

Code:
 if [wiki] "$DT_STRING" > "$DT_STRING_CMP" || "$DT_STRING" == "$DT_STRING_CMP" [/wiki]; then

# 6  
Old 04-16-2008
Ksh93 has better support for large numbers than pdksh or ksh88
Code:
#!/bin/ksh93

typeset -F STR1=20081221223440
typeset -F STR2=20071221223440

if [ $STR1 -ge $STR2 ]; then
    print "greater than"
fi
if [ $STR2 -ge $STR1 ]; then
    print "less than"
fi

exit

or to use more "modern" ksh syntax
Code:
#!/bin/ksh93

float STR1=20081221223440
float STR2=20071221223440

if (( $STR1 > $STR2 )); then
    print "greater than"
fi
if (( $STR1 < $STR2 )); then
    print "less than"
fi


Last edited by fpmurphy; 04-16-2008 at 10:18 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How will these subshell commands behave?

Hello, I am firing off some scripts from a main script, cd B/ ./EV_B_m0-m200_hex1.sh & ./EV_B_m0-m200_hex2.sh & wait ...more It would be useful to put a bit of time between the two to clean up the output to the terminal. I think this would work, cd B/ ... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

2. UNIX for Advanced & Expert Users

Why do awk command line vars behave the way they do?

This came up a little in another thread. Can someone explain some why awk (I happen to use gawk) behaves as follows: $ cat file aaa $ awk 'BEGIN {print x}' x=1 $ awk x=1 'BEGIN {print x}' awk: fatal: cannot open file `BEGIN {print x}' for reading (No such file or directory) $ awk -v... (6 Replies)
Discussion started by: hanson44
6 Replies

3. Shell Programming and Scripting

su - user -c 'command' behaves differently

I notice that su - user (note with dash) brings in more of the user's environment than does su - user -c 'command'. For example, if root does an su - user, and types "umask" to the prompt, one umask is displayed; yet, if instead the command is su - user -c 'umask', the value is different. I thought... (2 Replies)
Discussion started by: drokerm
2 Replies

4. Shell Programming and Scripting

mail command behave odd

hi, The following mail cmd executed successfully. mailx -s 'subject' user@company.com < testfile.dat However When i include this mail cmd in shell script it behave odd. Getting an error message mailx comand not found. (2 Replies)
Discussion started by: zooby
2 Replies

5. Solaris

swap displays differently

A new x86 server was installed with 16G of memory. The swap space assigned in the prtvtoc is also 16G. But after the installation of the OS and verifying, noticed df -k output for swap shows as 30G. Other systems do not have this characteristic. Whats wrong in here?:eek: (4 Replies)
Discussion started by: incredible
4 Replies

6. UNIX for Dummies Questions & Answers

formatting differently in last loop

hi. i'm trying to create a list users that is formatted for an SQL query. it should take the form: 'user1', 'user2', 'user3'as you can see, there is no comma after the last value. could someone help me revise my code to do this? while read user; do print "'"$userid"'," >>... (3 Replies)
Discussion started by: ankimo
3 Replies

7. Shell Programming and Scripting

Why does IF loop behave differently in different shells

Hi , I have a script that compares two string and prints the larger string , This is an extract of a biggers script that i have. #! /bin/ksh DT_STRING_CMP=20081221223440 DT_STRING=20071221223440 if ; then echo "20081221223440" fi echo... (1 Reply)
Discussion started by: amit1_x
1 Replies

8. UNIX for Dummies Questions & Answers

how to make ssh to behave as rsh

hi frnds, how to make the ssh service to behave as rsh.as we know ssh asks for passwd whereas the rsh doesnt.so how can i perform the followin operation without being asked for passwd. lets say i want to run the command "ls" on "remote_terminal" $ssh remote_terminal ls the above should work... (1 Reply)
Discussion started by: mxms755
1 Replies

9. Shell Programming and Scripting

Why does cron run this differently?

test.ksh =================== #!/usr/bin/ksh APPLICATION=hr_app APPLICATION_UPPER=`echo $APPLICATION | tr ` echo $APPLICATION_UPPER > /tmp/test.txt echo $APPLICATION >> /tmp/test.txt which tr >> /tmp/test.txt =================== When I run this from the shell: /home/natter> more... (6 Replies)
Discussion started by: natter
6 Replies
Login or Register to Ask a Question