Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

267
Vistas
Why shell does not set a variable which is piped?

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
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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
over 4 years ago · Santiago Trujillo Denunciar

0

You can pass the variables to the sub shell's environemnt:

foo=1
bar=2

echo "sds" | foo="$foo" bar="$bar" while ... 
over 4 years ago · Santiago Trujillo Denunciar

0

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:

  1. 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

  2. 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" 
    
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda