Sponsored Content
Top Forums UNIX for Dummies Questions & Answers brackets vs parentheses - single and double Post 302240411 by jim mcnamara on Thursday 25th of September 2008 05:28:56 PM
Old 09-25-2008
First read the man page for test.

[[ and [ are shell builtins (in modern shells)
for test. test or [[ - you have to complete them with ]] - are use either in if statments or in combined statements
Code:
if [[ 1 -eq $var ]] ; then
   echo "hi there"
fi
# same thing written another way as a one-liner
[[ 1 -eq $var ]] &&  echo "hi there"

# test if a file exists
[[ -f somefile ]] && echo "found somefile "

the -eq -gt -lt -ge -le comparisons are for comaring integer numbers in [[ ]]
comparing strings- [[ "$string1" = "$string2" ]]

There are other modern operators for testing file times: for example -ot -nt
See the ksh man page

$(( )) does arithmetic operations - whole numbers (long integers).
allowed operators are + - * / and some others like %.
$(( 3 + 1 )) evaluates a 4

$( some command[s] ) runs one or more commands in background
it works just like the backticks: `command`
read all of a file into an envrionment variable
myvar=$(< somefile)
put the date in a variable
myvar=$( date )
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

single or double quote in SED

i m trying the following command but its not working: sed 's/find/\'replace\'/g' myFile but the sed enters into new line # sed 's/find/re\'place/g' myFile > I havn't any idea how to put single quote in my replace string. Your early help woud be appreciated. Thanx (2 Replies)
Discussion started by: asami
2 Replies

2. UNIX for Dummies Questions & Answers

why put double square brackets in an if clause?

what is the rationale behind putting double square brackets in an if clause? for e.g. if ] || ] || ]; then echo some fields are null fi (5 Replies)
Discussion started by: napolayan
5 Replies

3. Shell Programming and Scripting

WHy the double square brackets?

One of the senior administrators gave me a shell script to modify and it begins as follows: if ] && ] {more code follows} Why the double square brackets? (10 Replies)
Discussion started by: mojoman
10 Replies

4. Shell Programming and Scripting

Use of double square brackets in ksh

Hi First apologies if this has been raised before. I've got the following in a ksh script: if ] For some reason this does not work. But if I remove the double square brackets to: if This works. I thought ksh supported the ]. Or is there more to it? Thanks in advance. (3 Replies)
Discussion started by: tsu3000
3 Replies

5. Shell Programming and Scripting

why double brackets for array loop?

Dear All, Recently got some sample from internet search for array handle in shell script, and I can not understand why we need to put double brackets when extracting array values by this way : for ((i=0; i<${#admins}; i++)); do echo "${admins}" done if modify from double to single... (2 Replies)
Discussion started by: tiger2000
2 Replies

6. Shell Programming and Scripting

Double square brackets question

Hi, I just came across an interesting shell script syntax like the one below: ] && (trap 'rm -rf ${WORK_DIR}/*.$$; echo "\n\nInterrupted !!\n\n"; exit 4' 1 2 3 15) Can someone please explain the code snippet above? The trap command bit is fine but ] && is the hazy part. Generally we use an... (2 Replies)
Discussion started by: King Nothing
2 Replies

7. UNIX for Dummies Questions & Answers

Brackets, parentheses in Shell Scripting

Hello, I have done a great deal of research on this and still cannot come to an understanding of the following: In if-else statements in shell scripting I have seen examples of single brackets, double brackets, single parantheses, and double paratheses. When should each one be used? And... (2 Replies)
Discussion started by: mojoman
2 Replies

8. Shell Programming and Scripting

Rules with using double parentheses in Bash

Hi.could you explain me what are the rules when we are using double parentesis in if statement,if I put ,the code is working ,with (( is not #!/bin/bash if (($# > 0)) then if ((! -d "$1")) then echo "Directory $1 not found" fi else echo Problem fi (8 Replies)
Discussion started by: lio123
8 Replies

9. UNIX for Dummies Questions & Answers

Single or double square brackets

Hi frieds, I don't understand the difference between single square bracket and double square brackets in a IF condition. Ex. if ; then RETURNJOB=1 else RETURNJOB=0 fi It run, but this if ]; then RETURNJOB=1 else RETURNJOB=0 fi (4 Replies)
Discussion started by: dogshort
4 Replies

10. Shell Programming and Scripting

Replace double quotes with a single quote within a double quoted string

Hi Froum. I have tried in vain to find a solution for this problem - I'm trying to replace any double quotes within a quoted string with a single quote, leaving everything else as is. I have the following data: Before: ... (32 Replies)
Discussion started by: pchang
32 Replies
test(1) 						      General Commands Manual							   test(1)

NAME
test - condition evaluation command SYNOPSIS
expr DESCRIPTION
The command evaluates the expression expr and, if its value is True, returns a zero (true) exit status; otherwise, a nonzero (false) exit status is returned. also returns a nonzero exit status if there are no arguments. The following primitives are used to construct expr: True if file exists and is readable. True if file exists and is writable. True if file exists and is executable. True if file exists and is a regular file. True if file exists and is a directory. True if file exists and is a character special file. True if file exists and is a block special file. True if file exists and is a named pipe (fifo). True if file exists and its set-user-ID bit is set. True if file exists and its set-group-ID bit is set. True if file exists and its sticky bit is set. True if file exists and has a size greater than zero. True if file exists and is a symbolic link. True if the open file whose file descriptor number is fildes (1 by default) is associated with a terminal device. True if the length of string s1 is zero. True if the length of the string s1 is non-zero. True if strings s1 and s2 are identical. True if strings s1 and s2 are not identical. s1 True if s1 is not the null string. True if the integers n1 and n2 are algebraically equal. Any of the comparisons and can be used in place of These primaries can be combined with the following operators: Unary negation operator. Binary AND operator. Binary OR operator has higher precedence than Parentheses for grouping. Note that all the operators and flags are separate arguments to Note also that parentheses are significant to the shell and therefore must be escaped. All file test operators return success if the argument is a symbolic link that points to a file of the file type being tested. is interpreted directly by the shell, and therefore does not exist as a separate executable program. EXTERNAL INFLUENCES
International Code Set Support Single byte and multibyte character code sets are supported. EXAMPLES
Exit if there are not two or three arguments: Create a new file containing the text string if the file does not already exist: Wait for myfile to become non-readable: WARNINGS
When the form of this command is used, the matching must be the final argument, and both must be separate arguments from the arguments they enclose (white space delimiters required. Parentheses and other special shell metacharacters intended to be handled by test must be escaped or quoted when invoking from a shell. Avoid such problems when comparing strings by inserting a non-operator character at the beginning of both operands: This approach does not work with numeric comparisons or the unary operators because it would affect the operand being checked. AUTHOR
was developed by the University of California, Berkeley and HP. SEE ALSO
find(1), sh-posix(1), sh(1). STANDARDS CONFORMANCE
test(1)
All times are GMT -4. The time now is 02:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy