The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > OS Specific Forums > AIX
Google UNIX.COM


AIX AIX is IBM's industry-leading UNIX operating system that meets the demands of applications that businesses rely upon in today's marketplace.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
help me in sending parameters from sqlplus script to unix shell script Hara Shell Programming and Scripting 2 01-29-2008 12:31 PM
Shell Script: want to insert values in database when update script runs ring Shell Programming and Scripting 1 10-25-2007 12:06 AM
here document to automate perl script that call script hogger84 Shell Programming and Scripting 3 10-22-2007 07:15 AM
returning to the parent shell after invoking a script within a script gurukottur Shell Programming and Scripting 5 09-26-2006 04:05 AM
return valuse from child script to parent script borncrazy Shell Programming and Scripting 1 08-20-2004 12:39 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-07-2007
Registered User
 

Join Date: Oct 2007
Posts: 289
aix script

hi all,

do yo know what is double "[[" and "]]" means?

Here's an example:

[[ -z $ERROROUTPUT ]] || echo "$TITLE"

thanks,
itik
Reply With Quote
Forum Sponsor
  #2  
Old 11-07-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,298
IT's a synonym for test, it returns true or false.

That code fragment means: test if $ERROROUTPUT is zero length || echo "$TITLE"
It doesn't make much sense in a programming context because the || allows the echo part to run regardless - it is a boolean or

i.e., the echo happens whether the first test evaluates true or false...
Reply With Quote
  #3  
Old 11-07-2007
Registered User
 

Join Date: Oct 2007
Posts: 171
Quote:
Originally Posted by jim mcnamara View Post
IT's a synonym for test, it returns true or false.

That code fragment means: test if $ERROROUTPUT is zero length || echo "$TITLE"
It doesn't make much sense in a programming context because the || allows the echo part to run regardless - it is a boolean or

i.e., the echo happens whether the first test evaluates true or false...
I beg to differ, "||" is and works as an else...
...testing if .bash_history is a file:
Quote:
lakris@Lakrix:~$ [[ -f .bash_history ]] && echo true
true
lakris@Lakrix:~$ [[ -f .bash_history ]] || echo true
lakris@Lakrix:~$ [[ ! -f .bash_history ]] || echo true
true
lakris@Lakrix:~$ [[ ! -f .bash_history ]] && echo true
lakris@Lakrix:~$
but it is a "test" and I do not really know the difference from a single "[", it may depend on Your shell. I think that in Bash it doesn´t matter.

/L
Reply With Quote
  #4  
Old 11-08-2007
Registered User
 

Join Date: Oct 2007
Posts: 155
The form "[ ]" is essentially calling the external "test" command, and thus everything is evaluated by the shell first. That's why you get "test: argument expected" when an unquoted variable is NULL, since the expression evaluates to "[ -z ]".

When using "[ ]", you really need to quote all values/variables. But, with the double-brackets, the test is a shell builtin. So nothing needs to be quoted, since it is not evaluated prior to being tested. Plus, you will save dozens of milliseconds by avoiding an external program.

Using the double-bracket with && is my favorite way of doing a simple if/then:

Code:
[[ 1 == 1 ]] && echo "equal" || echo "not equal"
which is *essentially* the same as

Code:
if [[ 1 == 1 ]]; then
    echo "equal"
else
    echo "not equal"
fi
I say "essentially" because the if/then executes the TRUE or the FALSE clause, never both. The other form can run both if the command after the && fails:

Code:
[[ 1 == 1 ]] && ehco "equal" || echo "not equal"
This will always print "not equal" since "ehco" is not a command and fails.

You can also do compounding:

Code:
[[ 1 == 1 ]] && { echo "equal"; date; } || echo "not equal"
but don't try to get too clever. A 50-line conditional statement using amperstands and braces is harder to read then plain old if/then/else.

Note that the amperstand can be used after any ordinary command:

Code:
grep -q localhost /etc/hosts && echo "found" || echo "not found"

Last edited by gus2000; 11-09-2007 at 12:24 PM.
Reply With Quote
  #5  
Old 11-08-2007
Registered User
 

Join Date: Oct 2007
Posts: 171
Thanks a bunch for the explanation!
Now I can save hundreds of milliseconds in my scripts
I had some idea that the quoting rules were different, but wasn't clear about exactly what it meant in practical use. And man bash isn't very helpful, or just too much text for me...

/Lakris
Reply With Quote
  #6  
Old 11-12-2007
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,262
Quote:
Originally Posted by Lakris View Post
Now I can save hundreds of milliseconds in my scripts
That can amount to an awful lot of time if you do it inside a deeply-nested loop. Suppose you execute the loop surrounding it 100.000 times: 100ms times 100.000 are 10.000 seconds, which are ~3 hours - not bad, yes?

bakunin
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0