[gull] bash et condition avec le if
felix
felix at f-hauri.ch
Tue Apr 5 07:52:47 CEST 2022
Salut Céd,
Le Tue, Mar 29, 2022 at 09:34:11PM +0200, Cédric BRINER via gull a écrit :
> en python, je peux écrire
> if ("a" == "b")and("c" == "c"):
> echo "succes"
> else
> echo "pas succes"
if [[ $a == b ]] && [[ $c == c ]] ;then ....
if [ "$a" -eq "$b" ] && [ "$c" -eq "42" ] ; then
(-eq implique des valeurs numériques)
> comment peut-on faire un truc pareil en shell:
> j'ai vu qu'il y a :
> - l'opérateur "-o" "-a"
> - l'opérateur || ou &&
>
Je n'utilise quasiment jamais `-o'.
D'une manière générale, entre ``if'' et ``then'', tu peux mettre un peu
ce que tu veux:
if { read _;read _ _ _ use _;} < <(df -k /); [[ $use -gt 1234 ]] ;then ....
> et comment est-ce que ça fonctionne avec des trucs un peu plus complexe
> comme:
> if (("a"=="b")or("a"=="d"))and("a"=="d")
Parenthèses en imbrication, comme en maths:
if [[ ad == ab ]] || [[ aa == ac ]] && [[ cc == cc ]] ;then
echo yes ;else echo no ;fi
Voire:
if { [[ ad == ab ]] || [[ aa == ac ]] ;} && [[ cc == cc ]] ;then
echo yes ;else echo no ;fi
> Si quelqu'un a une bonne documentation sur le sujet.
$ man -P'pager +/Compound\ Commands' bash
> et ce serait bien aussi de pouvoir faire un
>
> if [[ -x filepath ]] and [[ -n filepath ]]
if [[ -n "$filepath" ]] && [[ -x "$filepath" ]] ;then
Mais si tu utilises globshell pour définir ta variable, la moitié du boulot
et déjà faite:
filepath=(/path/to/files-*.txt)
if [[ ! -e "$filepath" ]] ;then echo No file found. ; exit ; fi
for file in "${filepath[@]}";do
echo "Processing '$file'."
La première ligne créé une variable de type ``array'' qui contiendra
- soit un seul element qui correspondra à ton pattern ('/path/to/files-*.txt`)
et qui ne sera donc PAS un fichier,
- soit une liste d'élément correspondant à ton pattern, que tu pourras accéder
simplements "${filepath[n]}" (pour n = 0 .. qte elements -1)
--
Félix Hauri - <felix at f-hauri.ch> - http://www.f-hauri.ch
More information about the gull
mailing list