whats the function of brackets here ?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting whats the function of brackets here ?
# 1  
Old 01-08-2011
whats the function of brackets here ?

Hello,

as the title reads, in a control flow like "if" what's the function of brackets ?

Code:
if  condition  then ...

Code:
if  [[ condition ]]  then ...

Since I saw people using them I wonder whether they are just inserts for code clearness or for the reliability ?

thanks for your help

Regards
# 2  
Old 01-08-2011
Good afternoon:
Quote:
Originally Posted by Oddant
<snip>
Code:
if  condition  then ...

should be:
Code:
if command; then ...

This executes the specified command, and then executes the "then" block if the command exited with a true status ($? is 0).
Quote:
Originally Posted by Oddant
<snip>
Code:
if  [[ condition ]] then ...

should be:
Code:
if [[ condition ]]; then ...

which equivalent to:
Code:
if test condition; then ...

# 3  
Old 01-08-2011
ok that was helpful thanks even though it didn't answer the main question.

Are those brackets important to be written ? or Are those brackets written just for a better human-readability ?

thanks anyway Smilie
# 4  
Old 01-08-2011
While the brackets are for better human-readability, they are important: if command; then ... is not the same if [[ condition ]]; then ... (unless command happens to be test).
This User Gave Thanks to m.d.ludwig For This Post:
# 5  
Old 01-08-2011
We need to know what Shell you are using.

In general (and there are exceptions)
Code:
if [[ Conditional Expression  ]]
if [ Test ]

You will find in the "man" pages for your Shell that there is a clear definition of Conditional Expression as distinct from Test. Some common enquiries like "-f" are available in both syntaxes but there are many differences and exceptions.
This User Gave Thanks to methyl For This Post:
# 6  
Old 01-09-2011
Quote:
Originally Posted by m.d.ludwig
[..]
Code:
if [[ condition ]]; then ...

which equivalent to:
Code:
if test condition; then ...

[ ... ] is a synonym for test , while [[ ... ]] is a conditional expression, which is not the same. They are different beasts...
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 01-09-2011
Quote:
Originally Posted by methyl
We need to know what Shell you are using.

In general (and there are exceptions)
Code:
if [[ Conditional Expression  ]]
if [ Test ]

You will find in the "man" pages for your Shell that there is a clear definition of Conditional Expression as distinct from Test. Some common enquiries like "-f" are available in both syntaxes but there are many differences and exceptions.
I'm using bash Smilie

Thanks for these replies guys, they are helpful

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python - Function print vs return - whats wrong

All, I have a basic buzz program written in python with return function. If i change return with print,it works fine but i want to know whats wrong with return statement.Can anyone help me whats wrong with this #!/usr/bin/python def div4and6(s,e): for i in range(s,e+1): if... (5 Replies)
Discussion started by: oky
5 Replies

2. Shell Programming and Scripting

Capture the value between brackets

Hi I am having hard time getting this right and need some help. I Have 3 files, and everyone contains the following:- File1 Today, we have Name(Jack) Age(19) Class (A2) Today, we have Name(Kim) Class (G9) File2 Today, we have Name(Lee) Age(19) Class (A2) Today, we have... (8 Replies)
Discussion started by: samsan
8 Replies

3. Shell Programming and Scripting

Function works, trigger causes syntax error, whats my fault??

Needing a hint. Creating that function called Meter my simple script works well. What I want now is to start the last four commented lines to include or trigger a reaction, if there are more than n lines in that .txt-file it shall display that message for example. But the interpreter says there is... (3 Replies)
Discussion started by: 1in10
3 Replies

4. Shell Programming and Scripting

Brackets

Hi all. i need a small help. i have written an exit code, which will check whether mo.sh is successful or not. if the status is >0 it will exit the shell. 1>Do you guys think it is a correct way to write? 2>what if i change the double bracket to single. how will it change the o/p. ... (1 Reply)
Discussion started by: sub
1 Replies

5. Shell Programming and Scripting

Delete text between square brackets and also delete those square brackets using sed or awk

Hi All, I have a text file which looks like this: computer programming systems engineering I want to get rid of these square brackets and also the text that is inside these brackets. So that my final text file looks like this: computer programming systems engineering I am using... (3 Replies)
Discussion started by: shoaibjameel123
3 Replies

6. Shell Programming and Scripting

How to get the value in the first brackets

Hello, I am trying to write a script using ksh. And I want to get the value within the first brackets of a string. For example: 14/04/11 11:35: 00 This is (nn) from the earth. Then i hope to get nn in this case. Can any one advise me how to implement it? Thank you very much! nn (2 Replies)
Discussion started by: n_n
2 Replies

7. Shell Programming and Scripting

Get value between brackets

Hi I am having hard time getting this right and need some help. I Have several log files, and everyone contains the following 3 lines at the end: 4 ETW000 Disconnected from database. 4 ETW000 End of Transport (0000). 4 ETW000 date&time: 13.01.2011 - 08:03:28 I need to capture the value... (7 Replies)
Discussion started by: nimo
7 Replies

8. Shell Programming and Scripting

Whats wrong in the Function ?

Need your assistance, to find the bug in the function. Function usage erroring out even after passing parameters. usage() { if || ; then echo "************************************************************" echo " CHECK USAGE FOR CORRECT PARAMETERS ... (26 Replies)
Discussion started by: raghunsi
26 Replies

9. UNIX for Dummies Questions & Answers

usage of brackets for if

Hi, I would like to know about the usage of brackets to negate a set of boolean evaluations an equivalent to what i am trying is below if or || ] ] then echo"valid time of day" else echo "invalid input" exit fi the bracket structue as mentioned doesnt work .. i am using ksh.... (4 Replies)
Discussion started by: lakshmikanth
4 Replies

10. Shell Programming and Scripting

Whats wrong with my function?? <newbie>

First of all im using Bash, on a Debian-based machine. I tried to write a function that if the ls program found listed more than 25 lines I would automaticly use "ls | less". Its on another computer but if I recall it looked something like this... Note: some code may look strange because im on... (4 Replies)
Discussion started by: riwa
4 Replies
Login or Register to Ask a Question