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
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments