Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

252
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda