I have a variable which i am trying to set something like this:
#!/bin/sh
found=0
id=1
echo "Hello" |
while [ $id != 5 ]
do
id=`expr $id + 1`
echo $id
found=1
done
echo "found = $found" // I expect this to be 1
Why, and how to set this value? I am forced to use like this (piped), because the actual code in production environment is:
found=0
id=1
my_mount_name="/opt/insiteone/fuse-mount"
echo "select file_system_id, mount_name from SystemTable" | mysql ifm -uroot -pinsite3 |
while read file_system_id mount_name
do
if [ "$id" == "$file_system_id" -a "$my_mount_name" == "$mount_name" ]; then
echo "Match found for file system ID and mount name"
found=1
fi
done
echo "found = $found" // I expect this to be 1, when a match, but does not
The pipe runs in a subshell. You can do a few things to make it work, the simplest is:
found=0
id=1
my_mount_name="/opt/insiteone/fuse-mount"
echo "select file_system_id, mount_name from SystemTable" |
mysql ifm -uroot -pinsite3 | {
while read file_system_id mount_name
do
if [ "$id" == "$file_system_id" -a "$my_mount_name" == "$mount_name" ]; then
echo "Match found for file system ID and mount name"
found=1
fi
done
echo "found = $found"; }
# Note the enclosing {}. Inside the black, the variable $found is set.
# After this comment, it will be zero.
This technique may require that the enclosing block be fairly large, so you may want to refactor the rest of the script to make this usable. Another option is to use a fifo or to put the echo/mysql pipeline into a process substitution. (The latter is not portable, but works in bash which may be adequate.) However, in this particular case, it is probably better to do something like:
found=0
id=1
my_mount_name="/opt/insiteone/fuse-mount"
echo "select file_system_id, mount_name from SystemTable" |
mysql ifm -uroot -pinsite3 | {
while read file_system_id mount_name
do
if [ "$id" == "$file_system_id" -a "$my_mount_name" == "$mount_name" ]; then
echo "Match found for file system ID and mount name"
exit 0 # Exit the subshell succesfully
fi
done
exit 1; } && found=1
You can pass the variables to the sub shell's environemnt:
foo=1
bar=2
echo "sds" | foo="$foo" bar="$bar" while ...
As @fedorqui commented, bash puts pipeline components in separate subshells, so the changes to your variables disappear when the subshell exits.
There are 2 strategies to deal with this:
only use the changed variables in the same subshell
echo "Hello" |
{
while [ $id != 5 ]
do
((id++))
echo $id
found=1
done
echo "found = $found" // I expect this to be 1
}
This can be a problem if you have lots of code that relies on those variables
replace the pipeline with process substitution. This means that the while loop is not executed in a subshell, it's run in the current shell:
while [ $id != 5 ]
do
((id++))
echo $id
found=1
done < <(echo "Hello")
echo "found = $found" // I expect this to be 1
This can suffer from poor readability, but you can put as many newlines inside <(...) as you want.
Your production code rewritten with process substitution (and bash/ksh conditional syntax):
found=0
id=1
my_mount_name="/opt/insiteone/fuse-mount"
while read file_system_id mount_name; do
if [[ $id == $file_system_id ]] && [[ $my_mount_name == $mount_name ]]; then
echo "Match found for file system ID and mount name"
found=1
fi
done < <(
echo "select file_system_id, mount_name from SystemTable" |
mysql ifm -uroot -pinsite3
)
echo "found = $found"