# Debian 10 - declare array in scripting bash

**URL:** <https://www.debian-fr.org/t/debian-10-declare-array-in-scripting-bash/86942>\
**Category:** Programmation\
**Created:** [Août 11, 2022, 12:43 UTC](https://www.debian-fr.org/t/debian-10-declare-array-in-scripting-bash/86942 "2022-08-11T12:43:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon36725218](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@anon36725218](https://www.debian-fr.org/u/anon36725218)\
**Post date:** [Août 11, 2022, 12:43 UTC](https://www.debian-fr.org/t/debian-10-declare-array-in-scripting-bash/86942/1 "2022-08-11T12:43:28Z")

</div>

Bonjour.

Je n’arrive pas à créer des tableaux dans `/bin/bash` de Debian ! Qu’est ce que c’est que çà … ?

Il y quelques années jamais du remplacer `/bin/bash` (truc dangereux 😕) par une ancienne version si je me rappelle bien 😕

```nohighlight
14:36:28 root@lv1.w1a:~ # /bin/bash --version
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
Licence GPLv3+ : GNU GPL version 3 ou ultérieure <http://gnu.org/licenses/gpl.html>

Ceci est un logiciel libre ; vous être libre de le modifier et de le redistribuer.
AUCUNE GARANTIE n'est fournie, dans les limites permises par la loi.
14:36:29 root@lv1.w1a:~ # 

```

```nohighlight
14:36:39 root@lv1.w1a:~ # uname -a
Linux w1a 4.19.0-8-amd64 #1 SMP Debian 4.19.98-1+deb10u1 (2020-04-27) x86_64 GNU/Linux

14:36:39 root@lv1.w1a:~ # 

```

```nohighlight
14:37:06 root@lv1.w1a:~ # cat /etc/os-release
PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

14:37:06 root@lv1.w1a:~ # 

```

```nohighlight
14:37:41 root@lv1.w1a:~ # cat /etc/debian_version
10.12
14:37:41 root@lv1.w1a:~ # 

```

J’ai fais un script tout bête… mais j’ai du faire une boucle 😅 pour récupérer une valeur du tableau `$RESOLVERS` aléatoirement - `shuf -i 0-1 -n1` (soit 0 soit 1 par exemple)

```nohighlight
14:37:42 root@lv1.w1a:~ # cat /etc/debian_version
#!/usr/bin/env bash
######!/bin/ksh

# Sur debian - no create array ???
# /bin/sh -> dash
# /bin/bash ?
# Only ksh88

# Date Actuelle
DATE=`date +%Y\/%m\/%d\ %H\:%M\:%S`

# Domain list check
DOMAINLIST="zw3b.fr zw3b.net zw3b.tv zw3b.site zw3b.blog lab3w.fr lab3w.com ipv01.net ipv10.net"

RAND="`shuf -i 0-2 -n1`"
#RAND="1"

#declare -A RESOLVERS
#RESOLVERS=("dns.google" "45.90.28.0" "2620:0:ccc::2")

#declare -A RESOLVERS=([0]="dns.google" [1]="45.90.28.0")
#RESOLVERS[0]="dns.google"
#RESOLVERS[1]="45.90.28.0"

#echo ${RESOLVERS[@]};
#exit

RESOLVERS="dns.google 45.90.28.0 resolver2.ipv6-sandbox.opendns.com"
RESOLVERS="dns.googleeee 465.90.28.0 resolver10.ipv6-sandbox.opendns.com"

c=0
for r in $RESOLVERS
do
        if [$c = $RAND]
        then
        RESOLVER=$r
        fi
        c=$(($c+1))

done

echo $RESOLVER
#exit

for d in ${DOMAINLIST}
do
        echo "#------------------------------"
        echo "${DATE} ${USER}@$(hostname -a) -> dig SOA ${d} @${RESOLVER} +short"
        DIG=`dig SOA ${d} @${RESOLVER} +short 2>/dev/null`
        if ["${DIG}" != ""]
        then
                echo ${DIG}
        else
                echo "ERROR : ${d}"
                echo "SEND SMS/MAIL"
        fi
done
echo "#------------------------------"

exit 0
14:37:42 root@lv1.w1a:~ # 

```

Qui aurait une procédure !?

Salutations,  
Romain

---

<div class="post-metadata">

**Author:** ![anon36725218](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@anon36725218](https://www.debian-fr.org/u/anon36725218)\
**Post date:** [Août 11, 2022, 1:39 UTC](https://www.debian-fr.org/t/debian-10-declare-array-in-scripting-bash/86942/2 "2022-08-11T13:39:17Z")

</div>

Bon j’ai trouvé il **fallait** mettre **le script en exécutable !**

```
chmod u+x dns-check-zones-up.bash

```

😃

---

<div class="post-metadata">

**Author:** ![anon36725218](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@anon36725218](https://www.debian-fr.org/u/anon36725218)\
**Post date:** [Août 11, 2022, 2:59 UTC](https://www.debian-fr.org/t/debian-10-declare-array-in-scripting-bash/86942/3 "2022-08-11T14:59:19Z")

</div>

> **Le script final :)**
>
> Vérifie que mes zones DNS de domaines sont actives et récupére le SOA sur un résolveur DNS aléatoirement configuré et envoie un mail s’il y a une erreur.
> 
> Avec une `crontab` (tâche planifiée) toutes les 6 heures et quelques minutes aléatoirement - pour ne pas surcharger nos/les serveurs - dans ce monde informatisé - toutes nos tâches planifiées à la même heure.  
> Imaginez que j’ai, que tu as un service utilisé par 100 000 personnes qui est utilisé depuis une crontab, puis les 100 000 personnes lance la crontab à minuit 00h00 : La surcharge à minuit, c’est trop lourd. La solution est d’utilisez une variable aléatoire avant le lancement du script directement dans la `crontab` avec un `shuf -i 0-1800 -n1` avec un lancement toutes les 2 heures - comme cela toutes les 2 heures et 30 minutes (maximum) le script s’exécutera - c’est important - ou dans le script lui-même 🙂
> 
> ```
> #!/bin/bash
> ##############################
> # Check DNS Zones UP
> ##############################
> 
> # Date Actuelle
> DATE=`date +%Y\/%m\/%d\ %H\:%M\:%S`
> 
> # Domain list check
> DOMAINLIST="zw3bbbbb.fr zw3b.net zw3b.tv zw3b.site zw3b.blog lab3w.fr lab3w.com ipv01.net ipv10.net"
> 
> RAND="`shuf -i 0-2 -n1`"
> RAND="0"
> 
> #declare -A RESOLVERS
> #RESOLVERS=("dns.google" "45.90.28.0" "2620:0:ccc::2")
> RESOLVERS=("dns.google")
> 
> ERR=0
> PRINT="#------------------------------\n"
> PRINT+="Check DNS Zones UP\n"
> PRINT+="|\n"
> for d in ${DOMAINLIST}
> do
> PRINT+="#------------------------------\n"
> PRINT+="${DATE} ${USER}@$(hostname -a) -> dig SOA ${d} @${RESOLVERS[$RAND]} +short\n"
> DIG=`dig SOA ${d} @${RESOLVERS[$RAND]} +short 2>/dev/null`
> if ["${DIG}" != ""]
> then
> PRINT+="${DIG}\n"
> else
> ERR=1
> PRINT+="ERROR : ${d}\n"
> fi
> done
> PRINT+="#------------------------------"
> 
> if [$ERR = 1]
> then
> echo -e $PRINT
> #echo -e $PRINT | mail -s "[ERROR]DNS" superadmin2@domain.tld,superadmin@domain.tld
> echo "| SEND SMS/MAIL"
> echo "#------------------------------"
> 
> else
> echo -e $PRINT
> fi
> 
> exit 0
> 
> ```
> 
> Avec la tache planifiée «&nbsp;`crontab`&nbsp;» toutes les 6h aléatoire des minutes 😉  
> NdMoi-même : Recommendation random scheduled tasks !
> 
> ```
> # DNS CHECK ZONE (toutes les 6h aléatoire des minutes (3600/60))
> 0 */6 * * * sleep $(shuf -i 0-3600 -n1) ; /root/dns-check-zones-up.sh 1>>/var/log/syslog 2>/dev/null 2>&1
> 
> ```

_J’ai passé le sujet dans «&nbsp;Programmation&nbsp;»_

**Comment faire un saut de ligne** à chacune de mes variables `$PRINT` hors commande `echo` ?

J’ai essayé cela :

```
#!/bin/bash

ARRAY=(one two three)
echo ${ARRAY[@]}

PRINT=""
PRINT+="one"
PRINT+="\r"
PRINT+=`echo "\n"`
PRINT+="\\n"
PRINT+="$(echo "\\n")"
PRINT+=" two $(echo "\n")"
PRINT+=" three "'\n'

echo $PRINT

```

Me retourne :

```
16:31:25 root@lv1.w1a:~ # bash test-scripting.bash
one two three
one\r\n\n\n two \n three \n

```

😕

Quelqu’un doit bien savoir faire çà 🙂 😂

Un petit lien çà ne fait toujours du bien : [https://tldp.org/LDP/Bash-Beginners-Guide/html/index.html](https://tldp.org/LDP/Bash-Beginners-Guide/html/index.html) 🙂

---

<div class="post-metadata">

**Author:** ![anon29980398](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@anon29980398](https://www.debian-fr.org/u/anon29980398)\
**Post date:** [Août 11, 2022, 3:31 UTC](https://www.debian-fr.org/t/debian-10-declare-array-in-scripting-bash/86942/4 "2022-08-11T15:31:25Z")

</div>

Bonjour

Utilise l’option **e**

```nohighlight
echo -e $PRINT

```

---

<div class="post-metadata">

**Author:** ![anon36725218](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@anon36725218](https://www.debian-fr.org/u/anon36725218)\
**Post date:** [Août 11, 2022, 3:45 UTC](https://www.debian-fr.org/t/debian-10-declare-array-in-scripting-bash/86942/5 "2022-08-11T15:45:55Z")

</div>

> [@anon29980398](#):
>
> Utilise l’option **e**

😍 😍 😍 😍 😍 😍 😍 😍 😍 😍 😍 😍 😍 😍

Merci @anon29980398, bonjour 😉

😌

---

<div class="post-metadata">

**Author:** ![PengouinPdt](https://www.debian-fr.org/user_avatar/www.debian-fr.org/pengouinpdt/32/3978_2.png) [@PengouinPdt](https://www.debian-fr.org/u/PengouinPdt)\
**Post date:** [Août 12, 2022, 11:01 UTC](https://www.debian-fr.org/t/debian-10-declare-array-in-scripting-bash/86942/6 "2022-08-12T11:01:04Z")

</div>

Bonjour

> [@anon36725218](#):
>
> J’ai essayé cela :
> 
> ```auto
> #!/bin/bash
> 
> ARRAY=(one two three)
> echo ${ARRAY[@]}
> 
> PRINT=""
> PRINT+="one"
> PRINT+="\r"
> PRINT+=`echo "\n"`
> PRINT+="\\n"
> PRINT+="$(echo "\\n")"
> PRINT+=" two $(echo "\n")"
> PRINT+=" three "'\n'
> 
> echo $PRINT
> 
> ```

Beurk, caca, vraiment !

Ecris plutôt :

```bash
#!/bin/bash

(...)

mssg="
one

 two 
 three 

"

echo "${mssg}"
# ou mieux :
printf '%s\n' "${mssg}"

```

Tu as aussi l’usage des [HEREDOC](https://linuxize.com/post/bash-heredoc/) 😉

Qui dans ce cas donnerait :

```bash

cat << DELIMITATEUR

one

 two 
 three
DELIMITATEUR

```

---

<div class="post-metadata">

**Author:** ![tfc](https://www.debian-fr.org/user_avatar/www.debian-fr.org/tfc/32/6424_2.png) [@tfc](https://www.debian-fr.org/u/tfc)\
**Post date:** [Août 12, 2022, 1:02 UTC](https://www.debian-fr.org/t/debian-10-declare-array-in-scripting-bash/86942/7 "2022-08-12T13:02:13Z")

</div>

Salut,

ha ouais merci, c’est bien plus élégant tout ça.

- documents intégrés (délimiteurs): [Chapitre&nbsp;19.&nbsp;Documents intégrés](https://abs.traduc.org/abs-fr/ch19.html)

- printf : [Chapitre&nbsp;15.&nbsp;Commandes internes et intégrées](https://abs.traduc.org/abs-fr/ch15.html#ex47)

---

<div class="post-metadata">

**Author:** ![anon36725218](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/a/8e8cbc/32.png) [@anon36725218](https://www.debian-fr.org/u/anon36725218)\
**Post date:** [Août 12, 2022, 1:32 UTC](https://www.debian-fr.org/t/debian-10-declare-array-in-scripting-bash/86942/8 "2022-08-12T13:32:32Z")

</div>

Ok merci pour ces infos … avec un «&nbsp;`echo -e`&nbsp;» c’est niquel 🙂

- `-e` qui signifie interpréter les caractères déspécifiés par une contre-oblique.

C’était pour le script masqué 😉 en [commentaire 2](https://www.debian-fr.org/t/debian-10-declare-array-in-scripting-bash/86942/2).

Bonne fin de journée.

---

<div class="post-metadata">

**Author:** ![absurdistan](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/a/e47c2d/32.png) [@absurdistan](https://www.debian-fr.org/u/absurdistan)\
**Post date:** [Août 20, 2022, 11:35 UTC](https://www.debian-fr.org/t/debian-10-declare-array-in-scripting-bash/86942/9 "2022-08-20T23:35:27Z")

</div>

J’aime bien tout ce que vous dites … Va falloir que je regarde ça de plus prêt ça a l’air sympa ! Merci 🙂

---

<div class="post-metadata">

**Author:** ![anon29980398](https://www.debian-fr.org/letter_avatar_proxy/v4/letter/a/f4b2a3/32.png) [@anon29980398](https://www.debian-fr.org/u/anon29980398)\
**Post date:** [Août 21, 2022, 2:23 UTC](https://www.debian-fr.org/t/debian-10-declare-array-in-scripting-bash/86942/10 "2022-08-21T02:23:40Z")

</div>

Il y a aussi, la dernière version de : [Introduction à la programmation en Bash (par Eric Sanchis)](http://aral.iut-rodez.fr/fr/sanchis/enseignement/bash/)
