aix script


 
Thread Tools Search this Thread
Operating Systems AIX aix script
# 1  
Old 11-07-2007
aix script

hi all,

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

Here's an example:

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

thanks,
itik
# 2  
Old 11-07-2007
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...
# 3  
Old 11-07-2007
Quote:
Originally Posted by jim mcnamara
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
# 4  
Old 11-08-2007
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. Smilie

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 04:24 PM..
# 5  
Old 11-08-2007
Thanks a bunch for the explanation!
Now I can save hundreds of milliseconds in my scripts Smilie
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
# 6  
Old 11-12-2007
Quote:
Originally Posted by Lakris
Now I can save hundreds of milliseconds in my scripts Smilie
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script in AIX

Hello Experts My SAP application is running in IBM AIX. Everyday my application generate some files in the below path as below. root@sedcaspm0502: pwd /interfaces/RFTS/B11/archive root@sedcaspm0502: ls -lrt -rw-r r 1 ppgadm sapsys 1039445 May 08 01:20... (1 Reply)
Discussion started by: sundar.c79@gmai
1 Replies

2. Shell Programming and Scripting

Need help in shell script on AIX

Hi All, I am connecting from ServerA(Unix) to ServerB (AIX). Copying a Database dump file from A to B. On B i need to import that dump file. I have used the following code to do this operation but ended up with error Pseudo-terminal will not be allocated because stdin is not a terminal.... (2 Replies)
Discussion started by: maddyd2k
2 Replies

3. UNIX for Dummies Questions & Answers

Shell Script in AIX

Hi , Good Morning every one !!! I am very new to this forum and new to Shell Script as well , hope some script Guru's will help me . I have a requirment to write a shell script . The requirment is like this . There are couple of shell scripts scheduled in the Crontab (some scripts run... (5 Replies)
Discussion started by: dilipd21
5 Replies

4. Shell Programming and Scripting

Aix .ksh for loop script.

Hi, I'm trying to write a for loop to run through a list of servers and for each server copy a file to a backup file. But I can't seem to get it to run through my server list. It work for individual servers, please see below. #!/bin/ksh SSH_USERID=khcuser webservers="server1 server2" ... (2 Replies)
Discussion started by: elmesy
2 Replies

5. AIX

help for aix script

hi all i want help to write the script thatti check rmt drive serial number if the same in two site Production and HA, the recomended like the follwing: The : AIX script to set up tape devices on OS to be identical on both nodes - define a rule, according to the renaming of the tape... (4 Replies)
Discussion started by: nancy_ghawanmeh
4 Replies

6. Shell Programming and Scripting

AIX script, help I'm just a backup!

Hello, I am trying to get the following Lawson command to work in an AIX script for a developer and I'm stumped, the Admin for this server is on vacation and I have very limited AIX scripting knowledge, I cannot seem to get this to work no matter what I try... lawcmp test900 ap HM500 This is... (1 Reply)
Discussion started by: j_aix
1 Replies

7. Shell Programming and Scripting

shell script on AIX

Hi! I have made a simple script for writing the following line on a file (file.txt): list= 1 2 3 and thr script looks like this: N=3 ll=(1 2 3) echo -n "list= ">> file.txt j=0 while ; do echo -n ${ll}" ">> file.txt let j++ done The code works fine on Linux,... (3 Replies)
Discussion started by: carl.alv
3 Replies

8. UNIX for Advanced & Expert Users

script migration from HP-UX to AIX

Dear All, What points should i keep in mind while migrating scripts from HP-UX to AIX. Are there any notes available for this? cheers, vishal (1 Reply)
Discussion started by: vishal_ranjan
1 Replies

9. Shell Programming and Scripting

UNIX AIX 5.3 Script Help

Hello all, I have a few questions regarding a UNIX script I've been asked to write (even though I barely know UNIX!). I would like to write a script, that when run, will copy a file from one directory to another, copy and rename the same file with a datestamp to an archive directory, and them... (10 Replies)
Discussion started by: yall
10 Replies

10. UNIX for Dummies Questions & Answers

[AIX] executing script

Hello! I need to run a script from the root user as the other user. I know that I can issue the following command: su - user_name -c "/path_to_script/script_name.sh" But there is a small problem with the above command. I must export a environment variable before I run the script. In the... (1 Reply)
Discussion started by: piooooter
1 Replies
Login or Register to Ask a Question