Well, guys(&gals...), second try, this time writen in a fault-tolerant editor like vi in my linux PDA, while watching "O' Brother" in the TV - I was not going to miss it
.
Another long off-topic post ...
SQUARE BRACKETS
--------------------
Square brackets, [ ], have two main uses within bash:
a) as an alias of the
test command, so useful in unix scripts that it has this 'special' abreviature. You must write a opening bracket, space, the test parameters, space and the closing bracket. This command is worth of a detailed study of its own.
Code: Select all
$ var=5
$ test $var -ge 5 && echo "This is a really big number"
This is a really big number
$ [ $var -ge 5 ] && echo "This is a really big number"
This is a really big number
b) As a file wildcard, together with * and ?. It is, IMO, the most useful of the wildcards (and shamefully forgotten in the poor copy of scripting in DOS command mode). Digressions aside, it specifies a character in a range or list:
[a-f] validates from a to f
[0-4] validates from 0 to 4
[f-a] validates nothing
[s4tHf] validates s,4,t,H and f (but not h, at least in Unix!!)
[^a-f] validates all except from a to f
Now, some samples:
Code: Select all
$ ls texture[2-4]2.dds
texture32.dds
texture42.dds
(sorry,there is no file texture22.dds 8) )
$ ls texture[^468]2.*
texture12.dds
texture32.dds
texture52.dds
texturea2.dds
textureb2.dds
textureb2.png
$ ls texture[2-48ac]2.dds
texture32.dds
texture42.dds
texture82.dds
texturea2.dds
$ ls texture[2-48ac][1-24].dds
texture31.dds
texture32.dds
texture34.dds
texture42.dds
texture44.dds
texture81.dds
texture82.dds
texturea2.dds
In some commands such as grep, vi, these expressions are used also for pattern recognition (i.e. look for some group of words...). Let's defer it for now.
BRACKETS (BRACELETS?)
------------------------------
As {} are referred too as brackets, let's explain these as well.
They have two uses again (we unixers exploit symbols a lot
) :
a) as delimiter of blocks of code. Its main use is for defining functions:
Code: Select all
# This is a really old routine, but I still love it ;-)
prompt()
{
i=0
while true
do
echo "$1(y/n)?\c"
read yn
case "$yn" in
y|Y|yes|YES|Yes|yEs|yES|yeS|YEs|YeS|aye ) return 0;;
n|N|no|NO|No|nO) return 1;;
* ) if [ $i -lt 5 ]
then echo "You must answer yes or no, please"
else echo "Answer YES or NO, YOU ID*OT !!!!"
fi
((i=i+1)) ;;
esac
done
}
prompt "Are you sure to delete $1 " && rm $1
b) as delimiter for variable names, when we need to use them concatenated with a literal:
Code: Select all
$ name=Javi
$ echo My name is $nameer
My name is
$ echo My name is ${name}er
My name is Javier
In the former case, we try to show the variable nameer, which is unassigned. The second is correctly displayed.
bracketing isn't necessary when the variable is followed by a punctuation character ([^a-zA-Z0-9_]):
Code: Select all
$ prefix=555
$ echo My phone is $prefix-6543322 # prefix-6543322 is not a valid variable name
My phone is 555-6543322
$ echo My phone is ${prefix}-6543322
My phone is 555-6543322
End of story ( and movie...
)
Thanks you all for your patience...