Computing.Net > Forums > Unix > ksh questions

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

ksh questions

Reply to Message Icon

Name: jigujigu
Date: April 1, 2003 at 11:44:02 Pacific
OS: HPUX 11.00
CPU/Ram: HP 9000, 1GB ram
Comment:

I have the following questions.

1. What is the difference between [[..]] and [..]? Why does first statement below works and second does not?

[[ 2-lt 3 && 3 -lt 4 ]] && echo yes
[ 2-lt 3 && 3 -lt 4 ] && echo yes

2. What is the difference between the 2 kinds of statement below? Why would someone prefer one over the other?

if [[ 3 -gt 2 ]]; then
echo yes
fi

if [[ 3 -gt 2 ]]
then
echo yes
fi

3. What is the difference between the 2 kinds of statement below? Why would someone prefer one over the other?

val=$(whoami)
(vs)
val=`whoami`




Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: April 1, 2003 at 12:53:08 Pacific
Reply:

1) double brackets [[...]] evaluates the expression between them and sets the unix exit code - either to 0 for true of nonzero for false. This is why 1 works and 2 doesn't.

The single brackets [] are used in such statements as:

while [ $x -gt 0 ]
do
.
.
done

and ksh array declarations and use.

2) Essentially, there's no difference betweent the two if statements. The semicolon before the 'then' allows placing the then on the same line as the rest of the if. Pick one method and stick with it.

3) Below are two examples of command substitution:

val=$(whoami)
(vs)
val=`whoami`

The first is ksh while the other is bourne shell, sh. The ksh method doesn't work with bourne, but the sh works on ksh.

The ksh purests claim the sh method is obsolete, and could disappear in the future (according to Rosenblatt's "Learning the Korn Shell).

Personally, if portability is an issue, use the sh method.

Regards,

Nails


0

Response Number 2
Name: James Boothe
Date: April 1, 2003 at 14:03:45 Pacific
Reply:

Regarding question #1, the single brackets also represent an expression to be evaluated as true or false. You can use either of two forms (see man test):

if test 2 -lt 3   or you could use:
if [ 2 -lt 3 ]

and test will also handle multiple tests, but as you discovered, shell operators like && and || cannot be used here. Instead, use -a or -o for and and or :

[ 2 -lt 3 -a 3 -lt 4 ] && echo yes


0

Response Number 3
Name: WilliamRobertson
Date: April 2, 2003 at 08:35:23 Pacific
Reply:

Speaking as a ksh purist myself, I always use the ksh [[ ... ]] and $() instead of the sh [ ... ] and `...`. I also never use -gt, since ksh has integer expressions such as (( x > y )).

One great advantage of $() is that it nests better, e.g:

grep -l three $(grep -l two $(grep -l one *))


0

Response Number 4
Name: James Boothe
Date: April 2, 2003 at 11:35:28 Pacific
Reply:

Great input - my preferences also. I just wanted to show jigujigu the proper syntax for his posted question.


0

Response Number 5
Name: jigujigu
Date: April 4, 2003 at 12:59:14 Pacific
Reply:

Thanks nails, James and WilliamRobertson!


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: ksh questions

ksh scripting - numeric vs. alpha www.computing.net/answers/unix/ksh-scripting-numeric-vs-alpha/4549.html

IF command behaving strangely www.computing.net/answers/unix/if-command-behaving-strangely/6754.html

Ask questions, append answr to file www.computing.net/answers/unix/ask-questions-append-answr-to-file/5968.html