Re: Newbie-ish question about ( [ and [[



Basics

[ ] is a bourne shell syntax and can be used in the Korn shell as
well
[[ ]] is Korn shell syntax and is reserved for string comparison
(( )) is Korn shell syntax and is reserved for numerical comparison

Examples


-- numerical comparison --
--- To test in born --

a=5
b=7

if [ $a -gt $b ] --testing to see if the value of a is greater than
the value of b
if [ $a -le $b ] --testing to see if the value of a is less than or
equal to b
-lt less than
-eq equal
-ge greater than or equal to
-ne not equal to


-- To test in korn --

if (( a > b )) --testing to see if a is greater than b
if (( a <= b)) -- testing to see if a is less than or equal to b
if (( a == b)) -- testing to see if a is exactly equal to b


The ksh can use either syntax. In some cases the script is less bulky
by using ksh syntax , at other times it's easier to use the bourne if
that's what you use for System Admin scripts most of the time.

There is a korn shell reference at http://unixinu.net/korn/quickksh.htm

look under the "if" statements just down from the top of the page.

Scott M. Oldre


Michael wrote:
I guess I'm a little dense, but I just don't get it.

When, and most importantly why do you use ( [ and [[ in shell scripts?

I'm trying to construct a simple:

if ( some-test-goes-here ) or [ some-test-goes-here ] or maybe even [[
some-test-goes-here ]]
then
blah
fi

construct and I am not sure when or why to use one of the three
different kinds of brackets.

Can someone point me to a resource?

Thanx!

.



Relevant Pages

  • Re: Newbie-ish question about ( [ and [[
    ... is a bourne shell syntax and can be used in the Korn shell as ... The ksh can use either syntax. ... There is a korn shell reference at http://unixinu.net/korn/quickksh.htm ... and most importantly why do you use ([and [[in shell scripts? ...
    (comp.unix.shell)
  • Re: combining the result of processing various commands and setting it in a variable
    ... "Contained here are the Korn shell 93 functions that comprise the Shell ... CHAPTER 19 A Smorgasbord of Scripts 381 ... Unfortunately I don't see any reference to ncurses ... I need is a code snippet "simple" is an adjective;-) ...
    (comp.unix.shell)
  • Re: ksh on Linux and echo
    ... > I am using the Korn Shell that is avaible for Red Hat Enterprise Linux ... > This is not pdksh, as pdksh has some limitations that I cannot live ... > We do not want to change these scripts, ... Fix them. ...
    (comp.unix.shell)
  • Re: Converting from MKS Korn Shell to Linux
    ... All of our install scripts are MKS toolkit korn shell scripts. ...
    (comp.unix.shell)
  • Converting from MKS Korn Shell to Linux
    ... I have recently switched from Windows to Linux on my work computer. ... All of our install scripts are MKS toolkit korn shell scripts. ...
    (comp.unix.shell)