[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
variable assignment in string returning function
From: |
Sharuzzaman Ahmat Raslan |
Subject: |
variable assignment in string returning function |
Date: |
Wed, 27 Jan 2010 20:49:45 +0800 |
Hi,
I found the behaviour of the function below is a little bit odd. Appreciate
if someone can share his/her knowledge regarding the behaviour.
The output of the script will be:
sharuzzaman@debian:~$ ./case1.sh
Nice behaviour,
Somehow, the backtick for foo() execute the function, echoing the correct
output, but fails to set the variable $gang to the correct value. Because of
that, the function bar() did not echoing anything because the variable $gang
is null.
I would expect that $gang is set with the correct value and function bar()
will work after that.
Thanks.
System information:
OS: Debian Squeeze
Hardware: Intel x86 Pentium 4
Bash: 4.1.0(1)-release (i486-pc-linux-gnu)
#startscript----------
#!/bin/bash
# test case for variable assignment in string returning function
# case 1: function with echo
name="optimus"
foo () {
if [ "$name" = "optimus" ]
then
gang="good"
echo "Nice behaviour"
else
gang="bad"
echo "Naughty behaviour"
fi
}
bar () {
case "$gang" in
good)
echo "autobot"
;;
bad)
echo "decepticon"
;;
esac
}
behaviour=`foo`
group=`bar`
echo $behaviour,$group
#endscript------------------------
--
Sharuzzaman Ahmat Raslan
- variable assignment in string returning function,
Sharuzzaman Ahmat Raslan <=