- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Usage of braces in Shell Scripts?
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2006 12:16 AM
08-03-2006 12:16 AM
( ) vs (( )) and [ ] vs [[ ]]
For example:
if ( x >= y )
if (( x >= y ))
if [ x >= y ]
if [[ x >= y ]]
Also “if [ x >= y ] ; then” what is the reason of using “;” in the syntax?
Thanks,
Gulam.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2006 12:25 AM
08-03-2006 12:25 AM
SolutionI use square brackets [] to contain conditions to test for.
I think if your question is two sets of brakets versus one set it makes no differences.
The best thing to do here is to write a little script.
bvar=1
if [ $bvar -ge 1 ]
then
echo "bvar is ge 1"
fi
Then try it with braces and then two sets of braces.
You may find the results interesting.
SEP
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2006 12:30 AM
08-03-2006 12:30 AM
Re: Usage of braces in Shell Scripts?
Single parentheses group expressions.
Double parentheses treat the enclosing characters as a quoted expression, and thus the form is equavalent to 'let ...'.
Single square brackets define 'test' conditions to be evaulated as true or false.
Double square brackets also frame conditional evaluations but word splitting and file name generation are not performed within the double brackets.
See the manpages for 'sh-posix(1)' for more information, for instance.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2006 12:33 AM
08-03-2006 12:33 AM
Re: Usage of braces in Shell Scripts?
(( )) is the same as saying
let "x >= y"
Here is the man ksh description of let:
let arg ... Each arg is a separate arithmetic expression to be
evaluated. See Arithmetic Evaluation above, for a
description of arithmetic expression evaluation. The
exit status is 0 if the value of the last expression is
non-zero, and 1 otherwise.
[ ] will execute the comparison using the test command.
[[ ]] will execute the comparison using the shell's builtin test command.
man ksh should give you most (if not all) of this information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2006 01:19 AM
08-03-2006 01:19 AM
Re: Usage of braces in Shell Scripts?
there are also differencies if you use ksh or csh. For '()' in c-shell you need '[]' in korn-shell.
For double brackets see also:
http://www.context-switch.com/reference/kshref/ch4.html
HTH
Volkmar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2006 02:03 AM
08-03-2006 02:03 AM
Re: Usage of braces in Shell Scripts?
(e.g. shells like HP-UX sh, bash, ksh, zsh?)
Because for expressions within them word splitting isn't performed.
This comes handy if an assignment to a variable by e.g. command substituion (formerly backtick quotes) fails.
e.g.
$ supposed_content="$(ls /non/existing/dir 2>/dev/null)"
$ [[ -n $supposed_content ]] && echo we could stat dir
but this wouldn't work with single brackets
$ [ -n $supposed_content ] && echo we could stat dir
su: test: Specify a parameter with this command.
But be aware that filename globbing doesn't work within double brackets
$ file=/etc/h?sts
$ [ -f $file ] && echo "$file worked"
/etc/h?sts worked
$ [[ -f $file ]] && echo "$file worked"
$
Also note that the logical operators changed to the more c-like ones in [[ ]],
viz. -a becomes &&, -o || etc.
(( )) evaluates arithmetic expressions
In bash this goes as far as letting you write C-like for loops, like
for ((i=0;i<10;i++)); do
# something
done
single paretheses open a subshell for commands within them.
A common idiom used when copying directories with tar for instance
cd /opt && tar cf - perl5 | (cd /tmp && tar xf -)
The semicolon between the test operator and then is necessary due to syntax rules if you put it in one line to have a similar appearance as in other programming/scripting languages.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-03-2006 02:27 AM
08-03-2006 02:27 AM
Re: Usage of braces in Shell Scripts?
I take it that you are starting out on the scripting road.
I would suggest that you try and leave logic issues to programs like perl or awk.
Without wishing to start a flame-war, each shell has its strengths and weaknesses. And alas, similarities - which then make your hard grafted scripts, non-transferrable. The same script written in ksh, sh, csh, bash, zsh, tcsh etc, will have slight variations in what a bracket means, how and where to use a THEN or an ELSE, whether subroutines appear before or after they are called etc.
They are great for doing the basics, but soon you'll want these loops and other constructs. That's where I feel they let themselves down.
I spent years trying to take into UNIX what I learnt in VMS. I then discovered awk and then perl.
If only I knew then what I know now....
ps you could always bypass perl and awk and go straight to python, then tell me how it works ;-)