Why shell does not set a variable which is piped?

RajSanpui

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
William Pursell

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why does my variable set in a do loop disappear? (unix shell)

From Dev

Why is this shell variable not set correctly

From Dev

Which location does shell assign normal variable?

From Dev

Why does `cd` have no effect if output is piped?

From Dev

Why setx command does not set an user variable?

From Dev

Why does tmux set TERM variable to screen?

From Dev

Why is there no wait function for condition_variable which does not relock the mutex

From Dev

Why does Debian set the login shell of user sync to /bin/sync?

From Dev

Why does exporting a variable in an ssh shell print the list of exported variables?

From Dev

Why does grep ignore the shell variable containing directories to be ignored?

From Dev

Why does the error message appear when `IF` comparison is in the piped block command?

From Dev

Why does bash output data instead of executing, when a script is piped?

From Dev

Why does make ignore escape sequence if the output is piped

From Dev

Why does piped FFMPEG output differ from an explicit file?

From Dev

Why does conditional variable assignment set a value but standard assignment does not?

From Dev

Access $? Variable with a piped statement?

From Dev

Access $? Variable with a piped statement?

From Dev

Output piped command to variable

From Dev

Why does Highstock initialisation set series variable to null?

From Dev

C++: Why does a functor for set order which returns false only lets one element be added to the set?

From Dev

Why does my php HOME environment variable not match the HOME variable in my linux bash shell?

From Dev

Mapping a Piped Shell Command in Vim

From Dev

Shebang does not set SHELL in cron

From Dev

Why does `type which` say that `which is hashed`?

From Dev

Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

From Dev

Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

From Dev

Why does bash exit immediately when waiting for a command to complete and receives SIGHUP for which a trap has been set?

From Dev

Shell, bash, linux ; after variable declaration, why semi-colon and what does it do?

From Dev

Why does variable is not defined?

Related Related

  1. 1

    Why does my variable set in a do loop disappear? (unix shell)

  2. 2

    Why is this shell variable not set correctly

  3. 3

    Which location does shell assign normal variable?

  4. 4

    Why does `cd` have no effect if output is piped?

  5. 5

    Why setx command does not set an user variable?

  6. 6

    Why does tmux set TERM variable to screen?

  7. 7

    Why is there no wait function for condition_variable which does not relock the mutex

  8. 8

    Why does Debian set the login shell of user sync to /bin/sync?

  9. 9

    Why does exporting a variable in an ssh shell print the list of exported variables?

  10. 10

    Why does grep ignore the shell variable containing directories to be ignored?

  11. 11

    Why does the error message appear when `IF` comparison is in the piped block command?

  12. 12

    Why does bash output data instead of executing, when a script is piped?

  13. 13

    Why does make ignore escape sequence if the output is piped

  14. 14

    Why does piped FFMPEG output differ from an explicit file?

  15. 15

    Why does conditional variable assignment set a value but standard assignment does not?

  16. 16

    Access $? Variable with a piped statement?

  17. 17

    Access $? Variable with a piped statement?

  18. 18

    Output piped command to variable

  19. 19

    Why does Highstock initialisation set series variable to null?

  20. 20

    C++: Why does a functor for set order which returns false only lets one element be added to the set?

  21. 21

    Why does my php HOME environment variable not match the HOME variable in my linux bash shell?

  22. 22

    Mapping a Piped Shell Command in Vim

  23. 23

    Shebang does not set SHELL in cron

  24. 24

    Why does `type which` say that `which is hashed`?

  25. 25

    Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

  26. 26

    Does powershell let you detect if output is being directed to the screen, vs piped to variable/file?

  27. 27

    Why does bash exit immediately when waiting for a command to complete and receives SIGHUP for which a trap has been set?

  28. 28

    Shell, bash, linux ; after variable declaration, why semi-colon and what does it do?

  29. 29

    Why does variable is not defined?

HotTag

Archive