GNU/Linux >> Tutoriels Linux >  >> Linux

Inclure conditionnellement une étape Pipe dans un script Bash ?

Cette question a déjà des réponses ici :Pipeline conditionnel

(7 réponses)
Fermé il y a 3 ans.

J'ai un script comme celui-ci :

flag=false
# Do a bunch of stuff that might set flag to true.
if [[ "$flag" == "true" ]]; then
   command \
       | pipe_command_a \
       | pipe_command_b \
       | pipe_command_c \
       | pipe_command_d \
       > "${output_path}"
else
   command \
       | pipe_command_a \
       | pipe_command_c \
       | pipe_command_d \
       > "${output_path}"
fi

La seule différence entre flag étant true ou false fait est que pipe_command_b peut ne pas être exécuté. Existe-t-il un moyen de réduire cela pour ne pas avoir à répéter toutes les choses courantes ?

Réponse acceptée :

Utilisez cat au lieu de la commande si vous voulez l'ignorer :

command=cat
if [[ $flag == true ]] ; then
    command=pipe_command_b
fi
command \
    | pipe_command_a \
    | $command       \
    | pipe_command_c

Linux
  1. Comment diviser une chaîne dans un script bash

  2. Comment inclure un fichier dans un script shell bash

  3. Exécuter le script bash en tant que démon

  4. Correspondance de modèle de script bash

  5. Exécutez la commande bash sur le pipeline jenkins

35 exemples de scripts bash

Comment exécuter un script bash

Typeset -a donne une erreur dans le script ?

Script Bash pour remplir un modèle ?

Comment définir les paramètres lorsque Pipe Bash Script to Bash ?

Script Bash vers Center Fortune/texte depuis Stdin/pipe ?