|
|
|||
![]() |
Department of Engineering |
| University of Cambridge > Engineering Department > computing help |
X=${H:-/help}' sets the
variable X to the value of H if H exists, otherwise
it sets X to /help.
for i in 1 2 3 do echo $RANDOM is a random number done
word1=one word2=two number=1The variable
word$number has the value one but how
can the value be accessed? If you try echo $word$number
or even echo ${word$number} you won't get the right answer.
You want $number processed by the shell, then the resulting
command processed. One way to do this is to use eval echo \$word$number - the \ symbol delays the interpretation of the first $
character until eval does a second pass.
((a=a<<2+3)) is the equivalent of let a="a<<2+3" - note
that this variant saves on quotemarks.
[[ -f /etc/passwd ]]
echo the passwd file exists
is another way of doing
if [ -f /etc/passwd ]
then
echo the passwd file exists
fi
or you could use the logical && operator, which like its C cousin
only evaluates the second operand if the first is true.
[ -f /etc/passwd ] && echo the passwd file exists
for((i=1; $i<3; i=i+1)) do echo $i doneRegular expressions (wildcard characters etc) can be used in some comparisons. In the following,
?[aeiou]? is a regular expression that
matches a character followed by one of a, e, i , o ,u, followed by a character
for word in four six sly
do
if [[ $word == ?[aeiou]? ]]
then
echo $word is 3 letters long with a central vowel
else
echo $word is not 3 letters long with a central vowel
fi
done