Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
3770 connectés 

  FORUM HardWare.fr
  Programmation
  Shell/Batch

  bash : if imbriqués

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

bash : if imbriqués

n°2427373
driving_so​uth
Posté le 29-09-2022 à 08:35:02  profilanswer
 

j'essaye d'imbriquer des if mais le shell me renvoi une erreur de syntax.  
 

Citation :


############# SSH Y/N #############
 
if  [ "$ssh_backup" == "y" ] then  
 
read -p "ssh server ?    " sshserver
read -p "ssh user ?    " sshuser  
read -p "tar pigz ou dd ?    " tool_choice
 
############# SSH-TOOL ##############
 
        if [[ "$tool_choice" == "tar" ]] then
 
        backup-action-ssh-tar
 
        elif [[ "$tool_choice" == "pigz" ]] then
 
        backup-action-ssh-pigz
 
        elif [[ "$tool_choice" == "dd" ]] then
 
        backup-action-ssh-dd
        else
        echo "no comprendo"
        fi
else
########### TOOLS NO SSH ##############
        read -p "tar pigz ou dd ?     " tool_choice
        if [ "tool_choice" == "tar" ] then
backup-action-tar
        elif ["tool_choice" == "pigz" ] then
backup-action-pigz
        elif ["tool_choice" =="dd" ] then
echo "NOT YET AVAILABLE"
        fi


 

mood
Publicité
Posté le 29-09-2022 à 08:35:02  profilanswer
 

n°2427375
kajoux
Posté le 29-09-2022 à 09:49:51  profilanswer
 

Passe à la ligne pour le "then", ou mets un point-virgule.

n°2427380
driving_so​uth
Posté le 29-09-2022 à 10:49:34  profilanswer
 

merci de ton aide. neanmoins :
 

Citation :

./backup.sh: line 79: syntax error near unexpected token `else'
./backup.sh: line 79: `else'


 
ca correspond au :
 
no comprendo
fi
else


Message édité par driving_south le 29-09-2022 à 10:50:30
n°2427381
kajoux
Posté le 29-09-2022 à 10:57:19  profilanswer
 

L'endroit où un interpréteur (ou un compilateur) indique l'erreur n'est pas forcément là où elle se trouve en réalité.
Il fait ce qu'il peut (comme tout le monde).

n°2427386
driving_so​uth
Posté le 29-09-2022 à 11:06:42  profilanswer
 

exact  :jap:  
 
tu pense que les doubles accolade sont necessaires ? je l'ai lu ici , paragraphe "nested if" :
https://ryanstutorials.net/bash-scr [...] ements.php .
 
je t'avoue que j'ai interpretté le truc , mais j'ai essayé avec des simples accolades , et cela bloquait aussi.

n°2427395
kajoux
Posté le 29-09-2022 à 11:44:38  profilanswer
 

Non ici ils ne sont pas nécessaires, puisqu'il n'y a rien de spécifique à bash.
Pour être tout à fait correct (POSIX), il faudrait d'ailleurs remplacer == par = dans les tests.


Message édité par kajoux le 29-09-2022 à 11:45:37
n°2427430
driving_so​uth
Posté le 29-09-2022 à 18:32:14  profilanswer
 

oki . donc je reproduis la meme structure , avec d'autre variable , genre hors texte , j'echoise , et je vois si cela fonctionne .

n°2427683
gilou
Modérateur
Modzilla
Posté le 01-10-2022 à 10:30:09  profilanswer
 

Sous Windows, dans Visual Studio Code, tu installe le plug-in ShellCheck, et ça te montrera les problèmes de ton code batch. Par contre, ça fonctionne plus (pas maintenu) sous IntelliJ.
Et sous Linux, ça doit s'installer via un package manager.
A+,


Message édité par gilou le 01-10-2022 à 10:31:47

---------------
There's more than what can be linked! --    Iyashikei Anime Forever!    --  AngularJS c'est un framework d'engulé!  --
n°2427741
driving_so​uth
Posté le 02-10-2022 à 09:29:47  profilanswer
 

Merci . je viens de l'installer . dispo sur debian . ca à l'air bien pratique. ;)
 

Spoiler :

#!/bin/bash
 
clear
 
###################### fonctions ######################
backup-action-tar() {
 
 echo "backup-action-tar"
 tar cvf $destination/$archive_name.tar $source  ; sync
 echo "$success"
 exit 0
}
backup-action-ssh-tar() {
 
 echo "backup-action-ssh-tar"
 tar cvf $archive_name.tar $source ; scp $archive_name.tar $sshuser@$sshserver:$destination/$archive_name.tar ; rm $archive_name.tar ; sync
 echo "$success"
 exit 0
}
 
backup-action-pigz() {
 echo "backup-action-pigz"
 tar cvf $destination/$archive_name $source ; pigz -r -6 $destination/$archive_name ; sync  
 echo "$success"
 exit 0
}
 
backup-action-ssh-pigz() {
 echo "backup-action-ssh-pigz"
 tar cvf $archive_name.tar $source ; pigz -6 $archive_name.tar ; scp $archive_name.tar.gz $sshuser@$sshserver:$destination/$archive_name.tar.gz ; rm $archive_name.tar.gz ; sync
 echo "$success"
 exit 0
}  
backup-action-ssh-dd() {
 echo "backup-action-ssh-dd selectionné"
 pv -pter -i 1 $source|pigz -6|ssh $sshuser@$sshserver dd bs=64M conv=notrunc,noerror of=/home/$sshuser/$archive_name
 echo "$success"
 exit 0
}
backup-action-dd() {
 echo "backup-action-dd selectionné"
 pv -pter -i 1 $source|pigz -6| dd bs=64M conv=notrunc,noerror of=$destination/$archive_name ; sync
 echo "$success"
 exit 0
}
 
 
 
################## saisie des valeurs initiales ###############
success="Backup terminé"
read -p "Enter target to save :  " source
read -p "Enter archive file name :  " archive_name
read -p "tar pigz or dd ? :   " tool_choice
read -p "Enter destination directory :   " destination
#source=$(zenity --entry --title "drfzzz-backup" --text "Selection de la cible:" --width=400 --height=200)
#archive_name=$(zenity --entry --title "drfzzz-backup" --text "Nom de l'archive" --width=400 --height=200)
#tool_choice=$(zenity --list --width=400 --height=200 --column Selection --column outil TRUE tar FALSE pigz FALSE dd --radiolist)
#destination=$(zenity --entry --title="drfzzz-backup" --text "Selection destination" --width=400 --height=200)
 
#zenity --info --width=400 --height=200 --text "$source $archive_name $tool_choice $destination"
echo "source $source nom archive $archive_name destination $destination outil $tool_choice"
 
 
if [ "$tool_choice" = "tar" ] ; then
 
tool="tar"
 
elif [ "$tool_choice" = "pigz" ] ; then
 
tool="pigz"
 
elif [ "$tool_choice" = "dd" ] ; then
 
tool="dd"
fi
 
read -p "backup via ssh ? y/n   " ssh_backup
 
if [ "$ssh_backup" = "y" ] ; then  
 read -p "ssh server ?" sshserver
 read -p "sshuser ?" sshuser
 fi
 
 if [ "$ssh_backup" = "y" ] && [ "$tool" = "tar" ] ;then
 backup-action-ssh-tar
 fi
 
 if [ "$ssh_backup" = "y" ] && [ "$tool" = "pigz" ] ; then
 backup-action-ssh-pigz
 fi
 
 if [ "$ssh_backup" = "y" ] && [ "$tool" = "dd" ] ; then
 backup-action-ssh-dd
 fi
 
 
#ssh_backup=$(zenity --entry --width=400 --height=200 --title "drfzzz-backup" --text "sauvegarde via ssh y/n ?" )
#read -p "sauvegarde via ssh ?     " ssh_backup
 
 
#if [ "$ssh_backup" == "y" ] ; then
 
#sshserver=$(zenity --entry --title="drfzzz-backup" --text "entrer l'adresse du serveur" --width=400 --height=200)
#sshuser=$(zenity --entry --title "drfzzz-backup" --text "entrer le nom utilisateur" --width=400 --height=200)
 
 
#read -p "ssh server ?" sshserver
#read -p "sshuser ?" sshuser
#backup-action-ssh
#fi
 
if [ "$tool" == "tar" ] ; then
 
backup-action-tar
 
elif [ "$tool" == "pigz" ] ; then
 
backup-action-pigz
 
elif [ "$tool" == "dd" ] ; then
 
echo "NOT AVAILABLE YET"
 
else
echo "no comprendo"
fi


 
 
so far , cela fonctionne ! la partie tool_choice fait doublon . c'est une relique de l'ancien temps  :o  
edit: donc j'ai deplacé la saisie "destination" : pour la section ssh et local. et une seul variable outil => "tool" .
dans le cas d'une sauvegarde ssh , le "sync" doit etre fait sur la machine distante , je le sais .  
je pense à rajouter un hashage sha , que je stockerai sur une 3eme emplacement . on peut tout imaginer . via email par exemple. ou une 3eme machine physique .  
 
tant qu'a faire , je vais rajouter l'inverse des fonctions : restauration depuis backup. une fois que tout fonctionnera, je pourrai peut etre ajouter une interface graphique. mais zenity ca me gonfle.  
 
ps: comment on colle un spoiler mais compact/deroulant ?


Message édité par driving_south le 02-10-2022 à 12:38:40
n°2427757
driving_so​uth
Posté le 02-10-2022 à 12:52:53  profilanswer
 

c'est boiteux pour l'instant. m'enfin c'est le projet.  
l'ultime test sera de le faire essayer par quelqu'un qui ne l'a pas écris .

mood
Publicité
Posté le 02-10-2022 à 12:52:53  profilanswer
 

n°2427758
driving_so​uth
Posté le 02-10-2022 à 13:32:31  profilanswer
 

okay . premiere esquisse.  
 

Spoiler :

#!/bin/bash
 
clear
 
###################### fonctions ######################
backup-action-tar() {
 
 echo "backup-action-tar"
 tar cvf $destination/$archive_name.tar $source  ; sync
 echo "$success"
 exit 0
}
backup-action-ssh-tar() {
 
 echo "backup-action-ssh-tar"
 tar cvf $archive_name.tar $source ; scp $archive_name.tar $sshuser@$sshserver:$destination/$archive_name.tar ; rm $archive_name.tar ; sync
 echo "$success"
 exit 0
}
 
backup-action-pigz() {
 echo "backup-action-pigz"
 tar cvf $destination/$archive_name $source ; pigz -r -6 $destination/$archive_name ; sync  
 echo "$success"
 exit 0
}
 
backup-action-ssh-pigz() {
 echo "backup-action-ssh-pigz"
 tar cvf $archive_name.tar $source ; pigz -6 $archive_name.tar ; scp $archive_name.tar.gz $sshuser@$sshserver:$destination/$archive_name.tar.gz ; rm $archive_name.tar.gz ; sync
 echo "$success"
 exit 0
}  
backup-action-ssh-dd() {
 echo "backup-action-ssh-dd selectionné"
 pv -pter -i 1 $source|pigz -6|ssh $sshuser@$sshserver dd bs=64M conv=notrunc,noerror of=/home/$sshuser/$archive_name
 echo "$success"
 exit 0
}
backup-action-dd() {
 echo "backup-action-dd selectionné"
 pv -pter -i 1 $source|pigz -6| dd bs=64M conv=notrunc,noerror of=$destination/$archive_name ; sync
 echo "$success"
 exit 0
}
 
 
 
################## saisie des valeurs initiales ###############
success="Backup terminé"
read -p "Enter target to save :  " source
read -p "Enter archive file name :  " archive_name
read -p "tar pigz or dd ? :   " tool
echo "source <= $source target => $archive_name outil: $tool"
read -p "backup via ssh ? y/n   " ssh_backup
 
 
################## section SSH ####################
if [ "$ssh_backup" = "y" ] ; then  
 echo "section ssh"
 read -p "ssh server ?  " sshserver
 read -p "sshuser ?  " sshuser
 read -p "destination ?  " destination
fi
 
 if [ "$ssh_backup" = "y" ] && [ "$tool" = "tar" ] ;then
 backup-action-ssh-tar
 
 elif [ "$ssh_backup" = "y" ] && [ "$tool" = "pigz" ] ; then
 backup-action-ssh-pigz
 
 
 elif [ "$ssh_backup" = "y" ] && [ "$tool" = "dd" ] ; then
 backup-action-ssh-dd
 fi
 
 
################# section LOCALE ####################
if [ "$ssh_backup" = "n" ] ; then
echo "section locale"
read -p "destination ?" destination
#else echo "no comprendo locale"
fi  
 if [ "$ssh_backup" = "n" ] && [ "$tool" == "tar" ] ; then
 
 backup-action-tar
 
 elif [ "$ssh_backup" = "n" ] && [ "$tool" == "pigz" ] ; then
 
 backup-action-pigz  
 
 elif [ "$ssh_backup" = "n" ] && [ "$tool" == "dd" ] ; then
 
 echo "NOT AVAILABLE YET"
 
else
echo "no comprendo"
fi
 


 
ok. j'en suis là .  
si vous voyez des trucs pas corrects ,


Message édité par driving_south le 02-10-2022 à 13:55:47

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  Shell/Batch

  bash : if imbriqués

 

Sujets relatifs
Script bash/python extraire données bddLire et déplacer un fichier TAR avec nom alternatif bash
[RESOLU]formulaires imbriqués dans un tableauprobleme execution docker-compose en script bash
[Shell Bash] Utilisation des semaphores pour paralleliser mon script[BASH] bad substitution sur formatage variable
bash- recherche lignes dans bloc et affichage dans tableaubesoin d'aide Shell/bash svp
[Shell Bash] Vérifier l'absence d'erreur sur équipement Cisco[BASH] Commande mv sur mac
Plus de sujets relatifs à : bash : if imbriqués


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR