Comment utiliser la commande yes sous Linux

La commande oui

Que fait oui?

yes

yes yes
yes anything you like

Mais à quoi ça sert?

Utiliser oui avec des scripts

#!/bin/bash

# ...
# in the middle of some long script
# obtain a response from the user
# ...

echo "Are you happy to proceed? [y,n]"
read input

# did we get an input value?
if [ "$input" == "" ]; then

   echo "Nothing was entered by the user"

# was it a y or a yes?
elif [[ "$input" == "y" ]] || [[ "$input" == "yes" ]]; then

   echo "Positive response: $input"

# treat anything else as a negative response
else

   echo "negative response: $input"

fi

  • A “yes” or “y” indicates a positive response.
  • Any other input is considered a negative response.
  • Pressing Enter with no input text does nothing.

chmod +x long_script.sh

./long_script.sh

yes | ./long_script.sh

yes yes | ./long_script.sh

Ne dites pas oui sans y penser

Utiliser oui avec des commandes

yes | sudo apt-get install fortune-mod

sudo apt-get -y install fortune-mod

yes | yum install fortune-mod

dnf -y install fortune-mod

Quelques autres trucs oui

yes "$(seq 1 20)" | while read digit; do echo digit; sleep 1; done

yes long line of meaningless text for file padding > test.txt
ls -lh test.txt
wc test.txt

yes long line of meaningless text for file padding | head -50 > test.txt