korn shell script puzzler


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting korn shell script puzzler
# 1  
Old 02-14-2012
korn shell script puzzler

Here is a very simple Korn shell script running on an AIX 5.3 box.

Why does this work without the $ prepended to RET_CD?
Code:
#!/bin/ksh
RET_CD=0
if [ RET_CD -ne 0 ] && [ RET_CD -ne 2 ] 
then
  echo "RET_CD is not 0 and not 2"
else
  echo "RET_CD is a 0 or a 2" 
fi


Last edited by Franklin52; 02-15-2012 at 03:33 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-14-2012
In Korn shell conditional expressions are evaluated in double brackets ( [[..]] ). Integer variables do not need $ signs in these. The test command (or its synonym [..] ) is also supported as an external command, but ksh also has a built-in that is used instead of this external test command to speed up execution.

My guess is that this built-in is using some or all of the code of the double bracket version and in the process also evaluates integer variables without the $-signs. It is still compliant because it evaluates integer variables with $-signs correctly.

If you want your code to be compatible with other shells, it is best to use $-signs with integer variables anyway if you are using single brackets.
# 3  
Old 02-22-2012
Scrutinizer,

I thank you for the reponse.

Smilie
# 4  
Old 02-22-2012
Interesting post.

This variation of old Bourne works by design for compatibility with old shells.
Code:
#!/bin/ksh
RET_CD=0
if [[ RET_CD -ne 0 && RET_CD -ne 2 ]]
then
  echo "RET_CD is not 0 and not 2"
else
  echo "RET_CD is a 0 or a 2"
fi

./scriptname
RET_CD is a 0 or a 2

As Scrutinizer advises, stick to the modern syntax with the $ prefix.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

pass null value to sql script from korn shell script

There are 4 parameters that I have to pass from korn shell to sql script. 1) I have to check if $1 , $2 , $3 and $4 are null values or not . How can I do that ? 2) Once its determined that these values are null (in the sense they are empty) how can I pass null values to sql script... (11 Replies)
Discussion started by: megha2525
11 Replies

2. Homework & Coursework Questions

Korn Shell Script

1. The problem statement, all variables and given/known data: Write a korn shell script with an alfanumeric string as argument. The script lists the file's names in the current directory that contain the given string as substring and that can be read and written. 2. Relevant commands, code,... (3 Replies)
Discussion started by: burm
3 Replies

3. Shell Programming and Scripting

Korn Shell Script

I have to solve some exercises in Korn Shell, but i'm having some problems. For example: Write a korn shell script with an alfanumeric string as argument. The script lists the file's names in the current directory that contain the given string as substring and that can be read and written. I... (3 Replies)
Discussion started by: burm
3 Replies

4. UNIX for Dummies Questions & Answers

running script with korn shell

How would i instruct the current shell to run the current script using the korn shell? (1 Reply)
Discussion started by: JamieMurry
1 Replies

5. Solaris

Problems with korn shell script

Hey Guys, I'm looking for some advice about a korn shell script I've written. I've spent hours googling for an answer hopefully someone here can help me out. Basically the part of the script I'm having problems with is when I need to SFTP a file from one server to another. The line looks... (6 Replies)
Discussion started by: hilather
6 Replies

6. Shell Programming and Scripting

Korn Shell script not running

I am sorry, this is really trivial, yet I am not able to understand what the problem is! I am using korn shell and running this script #!/bin/ksh keep=3 while ; do echo $keep keep=$(($keep-1)) done I am getting this error: `keep=$' unexpected I am not able to understand it because ... (1 Reply)
Discussion started by: Asty
1 Replies

7. AIX

Help with Korn Shell script

I have this Korn shell script that runs via a cron entry. It runs in a loop "watching" a specific file system for files with a certain name. The file system that it is watching is an upload file system for an FTP server. When files that are the correct name come in, it takes the extension of the... (1 Reply)
Discussion started by: heprox
1 Replies

8. UNIX Desktop Questions & Answers

korn shell script

hi all i am writing the korn shell script. i have a SQL script which gives me the folowing output DSA.WLG.20050713211544.20051025.20050713211544 28991 1130198400 DSA.WLG.20050713211544.20051025.20050713211544 25881 1130198400 DSA.WLG.20050711210100.20051025.20050711210100 25881 ... (3 Replies)
Discussion started by: pavan_test
3 Replies

9. UNIX for Dummies Questions & Answers

korn shell script

hello., i have 2 files.. 1 file is in this folder /home/test/ssk/DSA.WLG.20050713211544.20050710.20050713211544 (this part) other file is in this folder /home/kk/dev/DSA.WLG.20050711210100.20050710.20050711210100 ... (1 Reply)
Discussion started by: pavan_test
1 Replies

10. UNIX for Dummies Questions & Answers

su and password in a Korn shell script

Hi All, In my shell script I want to use su - userid and provide password for this user, but I don't know the syntax to do this, can anyone tell me what it is? I have tried: su - userid/password and su - userid password but no luck... (2 Replies)
Discussion started by: Melissa
2 Replies
Login or Register to Ask a Question