Re: bash et condition avec le if

felix via gull <gull-04vmY7CpswVinyhjFObuvR+/[email protected]>
Newsgroups gmane.org.user-groups.gull.general
Message-ID <[email protected]>
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  -  <[email protected]>  -  http://www.f-hauri.ch
_______________________________________________
gull mailing list
[email protected]
https://forum.linux-gull.ch/mailman/listinfo/gull
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.