1. The [ and the test builtin commands. In this context (after the [ and the test command) parameter expansion, word splitting and pathname expansion are performed[1]. So beware of the following situation when using the [ and the test commands:
$ var="a b"
$ [ $var = "a b" ]
bash: [: too many arguments
-- pathname expansion:
Code:
$ touch x
$ var=\*
$ set -x
$ [ $var = "*" ]
+ '[' x = '*' ']'
- particular syntax for logical comparison:
Code:
$ [ value = value -a 1 -lt 2 ]&&echo true
true
2. The [[ non standard (ksh93, zsh and bash) compound command. Parameter expansion is performed inside [[ ]].
No word splitting and pathname expansion are performed[2]. Support more operators.
Consider the following:
- no need to quote unset variables or variables with null values or values containing IFS characters:
Code:
$ unset var
$ set -x
$ [[ $var -eq value ]]
+ [[ '' -eq value ]]
Code:
$ var=a\ b
$ set -x
[[ $var == "a b" ]]
+ [[ a b == \a\ \b ]]
- new versions of bash support new operators:
Code:
$ var=abc
[[ $var =~ ^a ]]&&echo OK
OK
$ echo "${BASH_REMATCH[@]}"
a
- readable operators for logical comparison:
Code:
$ [[ value == value && 2 < 1 ]]||echo false
false
3. The compound command ((expression)). From the manual pages for bash[3]:
Quote:
The expression is evaluated according to the rules described below under ARITHMETIC
EVALUATION. If the value of the expression is non-zero, the return status is 0; oth-
erwise the return status is 1. This is exactly equivalent to let "expression".
Code:
$ unset a
$ ((++a))&&echo $a # version >= 3
1
And as far as your particular problem is concerned it seems like a limitation of the old version of bash you're using,
your code works with recent versions of the bash shell.
[1] For more details see the manual pages for bash, the section Shell Builtin Commands and CONDITIONAL EXPRESSIONS.
[2] For more details see the manual pages for bash, the section Compound Commands. Tilde expansion, arithmetic expansion, command substitution, process substitution, and quote removal are also performed.
[3] For more information see:
Cound anyone help me on how to compare date in Unix using if function on bash file?
current=date
if ###syntax is wrong, could anyone correct it for me
then
rm -rf /usr/local/src
fi
Thank You... (17 Replies)
Hi there,
im having issue with comparing two variables, in a bash script.
im trying to do the following:
- get a word from user 1
- split the word into array
- get a character from user2
trying to compare the character entered by user 2 with every single character in the array... (2 Replies)
Hi guys i need help with comparing lines in 2 separate files. Both files contain the same amount of lines and i need to output the difference into the 2nd file.
The 1st file is always correct.
1st file (Expected.e):
Tuesday, 11 August 2009
Wednesday, 12 August 2009
Thursday, 13 August 2009... (2 Replies)
Hi, I need a script that will:
1. Go through about 20 different folders, each containing about 20 1d files. The 1d files go something like this:
22.253 37.707 78.117 112.374 127.944 156.067 180.956 233.785 249.256 ... (1 Reply)
Hi guys, I hope you can enlight me with a script I'm doing for Solaris 10.
Script goes like this:
#!/usr/bin/bash
fechahoy=`perl /export/home/info/John/fechamod.pl`
fechayer=`perl /export/home/info/John/fecha.pl`
echo $fechahoy
echo $fechayer
DAT1=`ssh ivt@blahblah ls -la... (1 Reply)
Hi, I have a question on comparing time using bash. Tried searching a lot up but couldn't figure it out. So I have this:
CURRENT_TIME=$(date +%H:%M)
if
then echo "Continue"
else echo "Quit"
fi
I tried a lot of different combinations of comparing, but nothing seems to work. Any help... (2 Replies)
Hi,
I am working in bash in Mac OSX, I have following 'input.txt' file:
<INFO> HypoTestTool: >>> Done running HypoTestInverter on the workspace combined
<INFO> HypoTestTool: The computed upper limit is: 11 +/- 1.02651
<INFO> HypoTestTool: expected limit (median) 11
<INFO> HypoTestTool: ... (13 Replies)
Hy friends, I am newbie to bash scripting, can anyone explain how b=${a/23/BB} # Substitute "BB" for "23". this line converts "b" into string and and "d" into Integer. Thanks in advance (4 Replies)
Would like to compare 2 XML Strings which has certain known fields changed. For example, Date field will always have differences. When comparing both strings, skip/mask all the occurring Date Field's `DtField1` and `DtField2`
Note: these are not formatted xml format.
File1:
... (1 Reply)
Hi,
I can't figure out what I'm missing. I'm running a query to see if there are any streams recording on my DVR before starting a scripted update. I'm guessing that it is viewing $VIDEO as a string instead of an int. I've tried everything I saw on google but it still comes back as $VIDEO is... (8 Replies)