test condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting test condition
# 1  
Old 12-03-2008
test condition

Hi there,

I tried to search for this almost everywhere, but didnt get any proper information on it.

What is the difference between

Code:
[[  ]]

[ ]

Some of the code works when I have only single condition i.e.

Code:
[[ -z $arg1 ]] && $dothis1 || $dothis2

But if i try to include another testcondition to the above code like below

Code:
[[ -z $arg1 -a -n $arg2 ]] && $dothis1 || $dothis2

This works when I replace with single square brackets
Code:
[ -z $arg1 -a -n $arg2 ] && $dothis1 || $dothis2

Why is that so?
# 2  
Old 12-03-2008
The shell keyword [[ requires a different syntax,
see below.

With bash:

Code:
$ type [[ 
[[ is a shell keyword
$ help [[
[[ ... ]]: [[ expression ]]
    Returns a status of 0 or 1 depending on the evaluation of the conditional
    expression EXPRESSION.  Expressions are composed of the same primaries used
    by the `test' builtin, and may be combined using the following operators
    
        ( EXPRESSION )  Returns the value of EXPRESSION
        ! EXPRESSION    True if EXPRESSION is false; else false
        EXPR1 && EXPR2  True if both EXPR1 and EXPR2 are true; else false
        EXPR1 || EXPR2  True if either EXPR1 or EXPR2 is true; else false
    
    When the `==' and `!=' operators are used, the string to the right of the
    operator is used as a pattern and pattern matching is performed.  The
    && and || operators do not evaluate EXPR2 if EXPR1 is sufficient to
    determine the expression's value.

So:

Code:
$ [ x = x -a x = x ] && echo ok
ok
$ [[ x = x -a x = x ]] && echo ok
bash: syntax error in conditional expression
bash: syntax error near `-a'

But:

Code:
$ [[ x == x && x == x ]] && echo ok
ok

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Test a condition for n times and take action

i have to test a condition for n times and if its value is greater than one each time for continuous 5 iterations it will take action My Approach run the same command 5 times in for loop and divert the output to a file and then read the values from file and use if else if and take action ... (1 Reply)
Discussion started by: abhaydas
1 Replies

2. UNIX for Dummies Questions & Answers

Simple negated test condition

Without grep, I'd like to make a test condition so that any a word that does not have the successive letters car in it will be echoed. for example, bluecar will contain "car" so it will show up as a no var=bluecar $ echo $var|if ]; then echo "yes";fi yes this variable contains "car" so I... (5 Replies)
Discussion started by: newbie2010
5 Replies

3. UNIX for Advanced & Expert Users

Lower case test condition

I want to locate directories that are upper, lower or have both upper and lower cases. What I have is: find /tmp/$var2 -type d' " ); && echo "host case is incorrect" || echo "host case is correct" This actually is part of a larger script and it does work but the problem is that it... (3 Replies)
Discussion started by: newbie2010
3 Replies

4. UNIX for Advanced & Expert Users

Condition to test if a host can be pinged

I have a much larger script that takes an input file of hosts and determines if we support them and checks to ensure the FQDN coincides with our DNS. For instance, the hostname may return a different FQDN when passed to the "host" command, so I keep the value of the output of the "host" command... (4 Replies)
Discussion started by: MaindotC
4 Replies

5. Shell Programming and Scripting

How to check weather a string is like test* or test* ot *test* in if condition

How to check weather a string is like test* or test* ot *test* in if condition (5 Replies)
Discussion started by: johnjerome
5 Replies

6. Shell Programming and Scripting

Test condition

Hello, what is the better and correct way to perform a comparison: I have been using the following with no problems: if ] then .... fi I have seen this also used : if then .... fi When I try : if then .... fi I get an error like .... the test condition expects a... (4 Replies)
Discussion started by: gio001
4 Replies

7. UNIX for Dummies Questions & Answers

Condition test

Hi there, When I try to do a condition on test: $ str1=abcd $ test $str1 $ echo $? 0 Is there anyway to display the answer to be 'TRUE' or 'YES'? rather than 0? If so, how can I do it without using awk or sed. (2 Replies)
Discussion started by: felixwhoals
2 Replies

8. Shell Programming and Scripting

Condition test ( [[ ]] ) doubt

Hi , I have a doubt on condition test ( ] ). Pls refer blow program. #!/bin/ksh TEMP= if ;then echo $TEMP else print 'invalid option' fi Above script's TEMP variable has no value so it gives "invalid option" as output. But I got an error before priting the string . Result : ... (2 Replies)
Discussion started by: thambi
2 Replies

9. Shell Programming and Scripting

test and if condition

Guys look at this: i have to write a script that takes a file as an argument. The script should be able to determine what permissions the owner, group and everybody has for the file passed in. The output should be displayed similar to this. READ WRITE EXECUTE OWNER LEE.BALLANCORE YES YES NO... (9 Replies)
Discussion started by: ciroredz
9 Replies

10. Shell Programming and Scripting

need help with test condition in shell script

I'm new to scripting and I need help with a bourn shell script. What i'm trying to do is a test condition where "if the time is within 2 hours, it's true" and so on. The time is in the following format DATE=`/bin/date +"%Y%m%d%H%S"` for example, 20060907152000. So, what the script first... (9 Replies)
Discussion started by: pieman8080
9 Replies
Login or Register to Ask a Question