Hi,
I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh).
The goal of my script if to read a "config file" (like "ini" file), and make various report. I really simplified my script and reproduce the problem. It seems that variable inside "while loop" are lost in bash, but not in ksh?
Here's the script:
#!/bin/bash
function read_configfile
{
typeset configfile="$1"
typeset Hostname="$2"
grep '=' $configfile | sed "s/'//g" | while read ligne; do
variable="$(echo "$ligne" | awk -F= '{print $1}')"
value="$(echo "$ligne" awk -F= '{print $2}')"
eval "$variable='$value'"
done
echo "Affectation=$Affectation"
}
read_configfile bogus.in $(hostname)
And the bogus.in file:
Affectation=Yes life is good
When run, the output is
Affectation=
So... First of all I want to understand WHY the behavior is different. Is it because the way the "pipe" are processed?
Second, I found a way to bypass this. I just put every "variable=value" in a file (echo equation >>file), and outside the loop I'm doing an eval, and it works. But I would prefer NOT to use temporary file, so if you have any suggestions...
Thanks.