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




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?

[[ ... ]] is a Korn-shell-ism. It's purpose is similar to the POSIX [
.... ], but it's implemented differently. The POSIX [ ... ] syntax is
actually a command named [ which takes a bunch of arguments, the last
of which is the word ] . The [[ ... ]] syntax, on the other hand, is
parsed specially by the Korn shell. Its operators are different too.

[ ... ] is a command which parses a simple expression language equipped
with various operators for comparing numbers, strings, testing and
comparing file system objects for various conditions and such. The
command yields a successful termination status if the test is true, and
fails otherwise, so it's most commonly used as the controlling
expression of an if, or while:

if [ this_file -ot that_file ] ; then echo this_file is older than
that_file ; fi

( ... ) specifies a command sequence that is executed within a
subshell: that is to say, a child process. Useful if you want to
isolate a script from the effects of some command, such as variable
assignments, changes of the current working directory, process
termination, and such.

# cd that doesn't affect parent script
( cd project; ./configure )

# termination that doesn't affect parent script
( set -e ; cp bin/$PROGRAM $INSTALL_DIR ; cp lib/$LIBRARY
$INSTALL_DIR )

In the last example, the "set -e" turns on a mode which causes the
shell to terminate with a failed status if any of the commands fails.
Because this is in a subshell, it does not cause the parent to exit,
only the subshell. The parent can simply test for the overal result:

if ! ( set -e ; cp ... ) ; then
# subshell command sequence failed
fi

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.

Basically never use the [[ ]] if you care about portability to anything
but the Korn shell.

Use [ ] when you need the special test expression language for testing
strings for empty/nonempty/equal, simple numeric comparisons, or file
system tests.

Use ( ) to isolate multiple commands to a subshell.

Use no brackets or parentheses at all if you just want to test the
termination status of a command, or of the last command of a sequence:

if /bin/true ; then echo "true succeeded, as it should!" ; fi

Can someone point me to a resource?

http://www.opengroup.org/onlinepubs/007908799/xcu/chap2.html

.



Relevant Pages

  • Re: shell scripting
    ... and am assuming an sh-compatible shell here.... ... command 1 options arguments ... To run the script, do: ... then you need to use the full path to execute it: ...
    (comp.unix.questions)
  • Re: shell scripting
    ... and am assuming an sh-compatible shell here.... ... command 1 options arguments ... To run the script, do: ... then you need to use the full path to execute it: ...
    (comp.unix.shell)
  • Re: cd.exe
    ... hopefully will use the value of SHELL to invode the command. ... sets up the build environment and invokes bash. ... The script translates ... when invoked from the script though works fine from the command line. ...
    (comp.os.os2.programmer.porting)
  • Re: [ Attn: Randy ] Ad-hoc Parsing?
    ... I can write and compile ... >>programs inside a shell, ... But for me it is essential, that the script ... > internal command), the this script can be executed even if there ...
    (alt.lang.asm)
  • Re: Script to extract portions of text from a text file
    ... > But your example is not comparing the same command. ... it wouldn't be sent to the shell until you press enter). ... >> behaves differently in a script and at the prompt ... While awk does: ...
    (comp.unix.shell)