# Programation en bash condition

**URL:** <https://www.debian-fr.org/t/programation-en-bash-condition/17507>\
**Category:** Programmation\
**Created:** [Février 20, 2016, 6:50 UTC](https://www.debian-fr.org/t/programation-en-bash-condition/17507 "2016-02-20T18:50:03Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![fab\_du\_net\_13](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/f/2bfe46/32.png) [@fab\_du\_net\_13](https://www.debian-fr.org/u/fab_du_net_13)\
**Post date:** [Février 20, 2016, 6:50 UTC](https://www.debian-fr.org/t/programation-en-bash-condition/17507/1 "2016-02-20T18:50:03Z")

</div>

je voudrais programer en bash

me faire un petit menue avec des choix

echo

et je cherche un a faire des choix dans mon menue

en php c’est

if $variable==1  
{  
echo “bien”;  
exit() ;  
}  
echo “non”;

je chercherais fa faire cememe type de programe sous linux en bash par exemple

---

<div class="post-metadata">

**Author:** ![tellmewhy](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/43a26b/32.png) [@tellmewhy](https://www.debian-fr.org/u/tellmewhy)\
**Post date:** [Février 20, 2016, 6:50 UTC](https://www.debian-fr.org/t/programation-en-bash-condition/17507/2 "2016-02-20T18:50:04Z")

</div>

.

---

<div class="post-metadata">

**Author:** ![Tycho\_Brahe](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/ec9cab/32.png) [@Tycho\_Brahe](https://www.debian-fr.org/u/Tycho_Brahe)\
**Post date:** [Février 20, 2016, 6:50 UTC](https://www.debian-fr.org/t/programation-en-bash-condition/17507/3 "2016-02-20T18:50:07Z")

</div>

[quote=“fab du net 13”]  
if $variable==1  
{  
echo “bien”;  
exit() ;  
}  
echo “non”;[/quote]

Traduit en bash ça donne ceci :

`if [$variable -eq 1]
then
   echo "bien"
   exit
fi
echo "non"`  
Soit dit en passant, si on considère uniquement ce bout de code et non l’application dans son ensemble, le coup du exit est super moche, un simple else remplis le même travail beaucoup plus proprement.

---

<div class="post-metadata">

**Author:** ![fab\_du\_net\_13](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/f/2bfe46/32.png) [@fab\_du\_net\_13](https://www.debian-fr.org/u/fab_du_net_13)\
**Post date:** [Février 20, 2016, 6:50 UTC](https://www.debian-fr.org/t/programation-en-bash-condition/17507/4 "2016-02-20T18:50:08Z")

</div>

impecable merci beaucoups

je v regarder les site internet

et pour que je puisse tapper mon choix ???

en fait sur mon programe je voudrais me faire mon metit menue et en suite qui me dise choix et je met 1 soit 2 soit 3

---

<div class="post-metadata">

**Author:** ![themorice](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@themorice](https://www.debian-fr.org/u/themorice)\
**Post date:** [Février 20, 2016, 6:50 UTC](https://www.debian-fr.org/t/programation-en-bash-condition/17507/5 "2016-02-20T18:50:10Z")

</div>

```auto

va te stocker ce que tu as rentré dans la variable $var.

Concretement :
[code]#/bin/bash
read value;
case "$value" in
	"toto") echo bien ;;
	"tutu") echo bien ;;
        *) echo "pas bien";;
esac[/code]
```

va te stocker ce que tu as rentré dans la variable $var.

Concretement :

`#/bin/bash
read value;
case "$value" in
	"toto") echo bien ;;
	"tutu") echo bien ;;
        *) echo "pas bien";;
esac`

---

<div class="post-metadata">

**Author:** ![sputnick](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/s/8c91f0/32.png) [@sputnick](https://www.debian-fr.org/u/sputnick)\
**Post date:** [Février 20, 2016, 7:01 UTC](https://www.debian-fr.org/t/programation-en-bash-condition/17507/6 "2016-02-20T19:01:32Z")

</div>

Select est fait pour ça :

[quote]$ help select  
select: select NAME [in WORDS … ;] do COMMANDS; done  
The WORDS are expanded, generating a list of words. The  
set of expanded words is printed on the standard error, each  
preceded by a number. If `in WORDS' is not present,`in “$@”'  
is assumed. The PS3 prompt is then displayed and a line read  
from the standard input. If the line consists of the number  
corresponding to one of the displayed words, then NAME is set  
to that word. If the line is empty, WORDS and the prompt are  
redisplayed. If EOF is read, the command completes. Any other  
value read causes NAME to be set to null. The line read is saved  
in the variable REPLY. COMMANDS are executed after each selection  
until a break command is executed.  
[/quote]

Exemple :

[code]$ select var in zodb tinki-winky po lala dipsy casimir; do break; done

1. zodb
2. tinki-winky
3. po
4. lala
5. dipsy
6. casimir  
#?  
$ echo $var  
tinki-winky

# de là on peux faire un case :

$ case $var in  
zodb) echo pouet ;;  
tinki-winky) echo ezeze ;;  
po) echo “rm -rf /” ;;  
lala) echo “dd if=/dev/zero of=/dev/sda$((RANDOM%3))” ;;  
dipsy) echo “mv /dev/\* \> /dev/null” ;;  
casimir) echo “I’m a BOFH with the PEBKAC” ;;  
esac  
[/code]

On peux aussi plus classiquement mettre le case dans le select… UP2U

---

<div class="post-metadata">

**Author:** ![Ptilutin](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@Ptilutin](https://www.debian-fr.org/u/Ptilutin)\
**Post date:** [Février 20, 2016, 7:11 UTC](https://www.debian-fr.org/t/programation-en-bash-condition/17507/7 "2016-02-20T19:11:04Z")

</div>

voici une doc qui m’a bien aidé pour apprendre  
si ça peut servir…

[tldp.org/LDP/abs/html/](http://tldp.org/LDP/abs/html/)

---

<div class="post-metadata">

**Author:** ![sputnick](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/s/8c91f0/32.png) [@sputnick](https://www.debian-fr.org/u/sputnick)\
**Post date:** [Février 20, 2016, 7:11 UTC](https://www.debian-fr.org/t/programation-en-bash-condition/17507/8 "2016-02-20T19:11:04Z")

</div>

Si vous voulez de bonnes URLs sur bash : ( surement les meilleures du web )  
[mywiki.wooledge.org/BashFAQ](http://mywiki.wooledge.org/BashFAQ)  
[mywiki.wooledge.org/BashGuide](http://mywiki.wooledge.org/BashGuide)  
[tinyurl.com/txlv](http://tinyurl.com/txlv)  
[bash-hackers.org/wiki/](http://bash-hackers.org/wiki/)  
[grymoire.com/Unix/Quote.html](http://www.grymoire.com/Unix/Quote.html)  
[shelldorado.com/](http://www.shelldorado.com/)

Issu du topic de #bash sur [irc.freenode.org](http://irc.freenode.org) qui est un tres bon channel…

---

<div class="post-metadata">

**Author:** ![Ptilutin](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/p/e9bcb4/32.png) [@Ptilutin](https://www.debian-fr.org/u/Ptilutin)\
**Post date:** [Février 20, 2016, 7:11 UTC](https://www.debian-fr.org/t/programation-en-bash-condition/17507/9 "2016-02-20T19:11:05Z")

</div>

pas mal tout ça,

un bouquin que je possède qui est bien également c’est celui ci  
[blaess.fr/christophe/livres.php?pg=005](http://www.blaess.fr/christophe/livres.php?pg=005)
