Re: [gull] bash et condition avec le if

2022-04-04 Thread felix via gull
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  --  http://www.f-hauri.ch
___
gull mailing list
gull@forum.linux-gull.ch
https://forum.linux-gull.ch/mailman/listinfo/gull

[gull] netfunc.bash

2022-04-04 Thread felix via gull
Bonjour,

J'ai (enfin) publié ma release de ma batterie de function IPv4, pour laquelle
j'ai supprimé tous les forks et optimisé l'éxecution:

  https://f-hauri.ch/vrac/netfunc.bash.txt

Ce fichier est un fichier bash, à ``sourcer'':

  . netfunc.bash
  Bash source file for IPV4 computing - netfunc v0.13
   - int2ip   integer to IPv4
   - int2rev  3 most significant bytes, for reverse DNS X.X.X.in-addr.arpa
   - checkIsIpthrow an error if not valid IPv4
   - ip2int   IPv4 to integer 32 bits value.
   - mask2int from mask len to integer value of IPv4 mask
   - int2mlen compute mask len from integer value of IPv4 mask
   - be2int   Big Endian to INTeger, ( for reading proc/net/route )
   - int2uu   1st run generate int2uu() to render bash's 64# from integer
   - int2bin  1st run generate int2bin to render 32bits, mark mask with a 
dot.
   - rangeIp  Generate array of integer representation of IPv4 from argument
   - multipingOne ping on many target. Use integer representation of IPv4
   - ip2hnam  Fork to `host` and store in _ipResInv, if not already stored.
   - hname2ip Fork to `host` and store in _ipResolv, if not already stored.
   - isup Monitor ping many targets simultaneously (hit any key to quit)
   - mpingquick ping scan IPv4 ranges and resolv host name of waked 
hosts
   - ipcalc   compute and render network submited as [ip mask] or 
[ip/masklen]
   - getLocalNet  Get local config from /proc/net/ pseudo files
   - commonNetcompute smallest common network for many IP/net
   - reCIDR   Compute shortened list of CIDR for argument or STDIN
   - netfuncFList List functions (this.) [-v]

Quelques applications:

 $ ipcalc 192.168.1.33/24
 
+-+---+---+--+-+
 | Network base|192.168.1.0| #0|1100 10101000 0001| 
|
 | Mask|255.255.255.0  |/24|  | 
|
 | Broadcast   |192.168.1.255  |   #255|1100 10101000 0001| 
|
 | Host/net|254|Class C|  | 
|
 
+-+---+---+--+-+
 | First host  |192.168.1.1| #1|1100 10101000 0001| 
0001|
 |>Host|192.168.1.33   |#33|1100 10101000 0001| 
0011|
 | Last host   |192.168.1.254  |   #254|1100 10101000 0001| 
1110|
 
+-+---+---+--+-+

 $ isup 192.168.1.1 192.168.1.33 google.com
  Elaps Cnt  zrh..14  fir..ll  19...33
10.0964   3 11.30.4130.252
 la boucle est interruptible en appuyant sur [return].

 $ commonNet 10.11.12.13 10.11.15.2
 
+-+---+---++---+
 | Network base|10.11.12.0 | #0|1010 1011 11|00 
|
 | Mask|255.255.252.0  |/22|  11|00 
|
 | Broadcast   |10.11.15.255   |  #1023|1010 1011 11|11 
|
 | Host/net|1022   |Class A||   
|
 
+-+---+---++---+
 | First host  |10.11.12.1 | #1|1010 1011 11|00 
0001|
 |>IP min  |10.11.12.13|#13|1010 1011 11|00 
1101|
 | IP max  |10.11.15.2 |   #770|1010 1011 11|11 
0010|
 | Last host   |10.11.15.254   |  #1022|1010 1011 11|11 
1110|
 
+-+---+---++---+

 $ reCIDR 10.11.12.13/23 10.11.15.2/23
 10.11.12.0/22

-- 
 Félix Hauri  --  http://www.f-hauri.ch
___
gull mailing list
gull@forum.linux-gull.ch
https://forum.linux-gull.ch/mailman/listinfo/gull