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

255
Vistas
bash echo no funciona después de que grep no devuelve nada

Entiendo que las personas generalmente usan grep en el símbolo del sistema de Linux (no en un script). Sucede que puse grep en un script y me encontré con un caso extraño. Si el comando grep no devuelve nada, el eco de la siguiente línea no funciona. A continuación se muestra el guión.

 grep "abc" /home/testuser/myapp.log echo "abc"

¿Es este el comportamiento normal de grep? ¿Si es así por qué?

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Parecería que set -e está habilitado en su script.

Normalmente, el estado de salida de grep es 0 si se selecciona una línea, 1 si no se seleccionó ninguna línea y 2 si se produjo un error. Sin embargo, tenga en cuenta que si se usa -q o --quiet o --silent y se selecciona una línea, el estado de salida es 0 incluso si se produce un error. En su caso, si no se encuentra abc en /home/testuser/myapp.log , grep devolverá 1.

El shell bash normalmente ejecutará la siguiente línea en su secuencia de comandos, es decir, echo "abc" incluso si grep devuelve un estado de salida de 1. Sin embargo, si set -e está habilitado, bash no ejecutará más líneas en su secuencia de comandos.

Desde la página de manual de bash :

 -e Exit immediately if a pipeline (which may consist of a single simple command), a list, or a compound command (see SHELL GRAMMAR above), exits with a non-zero status. The shell does not exit if the command that fails is part of the command list immediately following a while or until keyword, part of the test following the if or elif reserved words, part of any command executed in a && or || list except the command following the final && or ||, any command in a pipeline but the last, or if the command's return value is being inverted with !. If a compound command other than a subshell returns a non-zero status because a command failed while -e was being ignored, the shell does not exit. A trap on ERR, if set, is executed before the shell exits. This option ap‐ plies to the shell environment and each subshell environment separately (see COMMAND EXECUTION ENVIRONMENT above), and may cause subshells to exit before executing all the commands in the subshell. If a compound command or shell function executes in a context where -e is being ignored, none of the commands executed within the compound command or function body will be affected by the -e setting, even if -e is set and a command returns a failure status. If a compound command or shell function sets -e while executing in a con‐ text where -e is ignored, that setting will not have any effect until the compound command or the command con‐ taining the function call completes.
over 4 years ago · Santiago Trujillo Denunciar

0

Puede usar el siguiente comando para echo con grep :

 printf "%s\n" "$(grep -o "abc" /home/testuser/myapp.log)"

Referencias
  • página del manual de grep
  • ¿Cómo usar echo con grep en un script de shell de Unix?
over 4 years ago · Santiago Trujillo Denunciar

0

¿Qué quisiste decir con " el eco de la siguiente línea no funciona "? ¿No se alcanza el echo en absoluto? ¿Se ejecuta pero produce un código de salida distinto de cero? ¿O ves abc en la salida? Sabiendo que su secuencia de comandos no devolverá nada para grep , y no sabiendo nada más sobre el resto de la secuencia de comandos, puedo decir que la secuencia de comandos no generará nada en la salida stdout para el comando grep y abc en la salida stdout para el echo .

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