Bonjour,
Débutant et autodidacte au bash, je bloque actuellement sur l'amélioration d'un de mes scripts (
Copy-Flash).
La première partie du code remanié me permet de récupérer une liste de nombre sous la forme
17
23
24
Chaque entrée correspondant à un fichier dans une arborescence du type
/dossier1/dossier2/XX, je souhaiterais ensuite les utiliser respectivement (en parallèle ou successivement) pour effectuer des opérations au sein d'une boucle :
- définition d'une variable à chacun en récupérant le nom du fichier dans ses propriétés
- boucle testant l'augmentation de la taille du fichier
- sortie de boucle lorsque la taille du fichier est stable
- copie et renommage du fichier
Actuellement, mon script fonctionne parfaitement avec une seule entrée. Mais est-il possible d'effectuer la même procédure sur chaque élément de la liste ?
Pour être plus clair, voici le script fonctionnel en sélectionnant un seul fichier :
#!/bin/bash
#########################################################################################################
# Description: Permet de sauvegarder les videos flash en cours dans le navigateur. #
# Dépendances: grep; zenity; awk; #
# Auteurs: lylambda; bece (amélioration) #
# Version: Copy-Flash 0.4 #
# Licence: gpl #
# http://forum.ubuntu-fr.org/viewtopic.php?id=424080 #
#########################################################################################################
COPY_FOLDER=$HOME/Bureau
# Dossier de copie
## /!\ À remplacer si besoin
if [[ -d $COPY_FOLDER ]]; then
echo "directory ... ok" > /dev/null
else
notify-send -i error "Copy-Flash" "\nErreur : $COPY_FOLDER introuvable\nCopie dans /tmp"
COPY_FOLDER=/tmp
fi
#Utiliser /tmp comme dossier de copie alternatif si celui indiqué n'existe pas
BROWSER="firefox"
# Nom du navigateur à utiliser
## /!\ À remplacer si besoin
if [[ $BROWSER=firefox || $BROWSER=iceweasel ]]; then
NPROCESS=$(ps aux | grep flash | grep -v "grep" | awk '{print $2}')
# Numéro du processus flash si utilisation de firefox ou iceweasel
else
NPROCESS=$(pgrep $BROWSER)
# /!\ Commande testée et validée pour midori, arora et uzbl-core. Erreur possible pour d'autres navigateurs.
fi
FICHIER=$(cd /proc/$NPROCESS/fd && ls -la | grep Flash | awk '{print $8}' && cd)
# Liste les fichiers vidéos
NBRE_FICHIER=$(echo $FICHIER | wc -w)
# Compte le nombre de fichiers vidéos.
if [ "$NBRE_FICHIER" == "0" ]; then
notify-send -i error "Copy-Flash" "\nErreur : aucune vidéo trouvée"
# Notification d'erreur. Utilisez la ligne suivante en cas de préférence pour les fenêtres zenity
## zenity --info --title="Erreur" --text="Aucune vidéo trouvée."
elif [ "$NBRE_FICHIER" != "1" ]; then
to_print=""
for poet in $FICHIER;
do date=$(stat -Lc '%y' /proc/$NPROCESS/fd/$poet | awk -F \. '{print $1}');
nom=$(stat -c '%N' /proc/$NPROCESS/fd/$poet | awk '{print $3}'| cut -d"/" -f3);
to_print="$to_print \"\" \"$poet\" \"$nom\" \"$date\"";
done;
# Fenêtre zenity de sélection unique (option --radiolist) si plusieurs vidéos en parallèle
video_a_copier=$(echo $to_print | xargs zenity --title='Choix du film à copier' --list --text="" --column=" " --column="N°" --column="Nom" --column="Date" --radiolist --width=500 --height=300 --separator=' ');
renommer=$(stat -c '%N' /proc/$NPROCESS/fd/$video_a_copier | awk '{print $3}'| cut -d"/" -f3)
# Choix de la vidéo à copier
if [ -z "$video_a_copier" ]; then
exit 0;
# Bouton annuler ou aucune vidéo choisie
fi
taille_1=$(stat -Lc '%s' /proc/$NPROCESS/fd/$video_a_copier)
sleep 1
taille_2=$(stat -Lc '%s' /proc/$NPROCESS/fd/$video_a_copier)
while test $taille_1 -ne $taille_2
do
taille_mo=$(($taille_2/10**6))
notify-send -i clock "Copy-Flash" "\nAttente de la fin de mise en cache : $taille_mo Mo" &
taille_1=$(stat -Lc '%s' /proc/$NPROCESS/fd/$video_a_copier)
sleep 10
taille_2=$(stat -Lc '%s' /proc/$NPROCESS/fd/$video_a_copier)
# Attente durant la mise en cache de la vidéo choisie et notifications
if test $taille_1 -eq $taille_2
then
break
fi
done
taille_mo=$(($taille_2/10**6))
cp /proc/$NPROCESS/fd/$video_a_copier $COPY_FOLDER; mv $COPY_FOLDER/$video_a_copier $COPY_FOLDER/$renommer &
notify-send -i folder-download "Copy-Flash" "\nCopie de la vidéo $renommer ($taille_mo Mo) \n$COPY_FOLDER" &
# Copie de la vidéo et notification si la mise en cache est terminée
else
taille_1=$(stat -Lc '%s' /proc/$NPROCESS/fd/$FICHIER)
sleep 1
taille_2=$(stat -Lc '%s' /proc/$NPROCESS/fd/$FICHIER)
while test $taille_1 -ne $taille_2
do
taille_mo=$(($taille_2/10**6))
notify-send -i clock "Copy-Flash" "\nAttente de la fin de mise en cache : $taille_mo Mo" &
taille_1=$(stat -Lc '%s' /proc/$NPROCESS/fd/$FICHIER)
sleep 10
taille_2=$(stat -Lc '%s' /proc/$NPROCESS/fd/$FICHIER)
if test $taille_1 -eq $taille_2
then
break
fi
done
taille_mo=$(($taille_2/10**6))
renommer=$(stat -c '%N' /proc/$NPROCESS/fd/$FICHIER | awk '{print $3}'| cut -d"/" -f3)
cp /proc/$NPROCESS/fd/$FICHIER $COPY_FOLDER; mv $COPY_FOLDER/$FICHIER $COPY_FOLDER/$renommer &
notify-send -i folder-download "Copy-Flash" "\nCopie de la vidéo $renommer ($taille_mo Mo) \n$COPY_FOLDER" &
fi
# Notifications, attente et copie directe si une seule vidéo est présente
exit 0
Et voici les modifications du code pour sélectionner plusieurs fichiers (les points de blocages sont indiqués en commentaires):
#!/bin/bash
#########################################################################################################
# Description: Permet de sauvegarder les videos flash en cours dans le navigateur. #
# Dépendances: grep; zenity; awk; #
# Auteurs: lylambda; bece (amélioration) #
# Version: Copy-Flash 0.5 alpha #
# Licence: gpl #
# http://forum.ubuntu-fr.org/viewtopic.php?id=424080 #
#########################################################################################################
COPY_FOLDER=$HOME/Bureau
# Dossier de copie
## /!\ À remplacer si besoin
if [[ -d $COPY_FOLDER ]]; then
echo "directory ... ok" > /dev/null
else
notify-send -i error "Copy-Flash" "\nErreur : $COPY_FOLDER introuvable\nCopie dans /tmp"
COPY_FOLDER=/tmp
fi
#Utiliser /tmp comme dossier de copie alternatif si celui indiqué n'existe pas
BROWSER="firefox"
# Nom du navigateur à utiliser
## /!\ À remplacer si besoin
if [[ $BROWSER=firefox || $BROWSER=iceweasel ]]; then
NPROCESS=$(ps aux | grep flash | grep -v "grep" | awk '{print $2}')
# Numéro du processus flash si utilisation de firefox ou iceweasel
else
NPROCESS=$(pgrep $BROWSER)
# /!\ Commande testée et validée pour midori, arora et uzbl-core. Erreur possible pour d'autres navigateurs.
fi
FICHIER=$(cd /proc/$NPROCESS/fd && ls -la | grep Flash | awk '{print $8}' && cd)
# Liste les fichiers vidéos
NBRE_FICHIER=$(echo $FICHIER | wc -w)
# Compte le nombre de fichiers vidéos.
if [ "$NBRE_FICHIER" == "0" ]; then
notify-send -i error "Copy-Flash" "\nErreur : aucune vidéo trouvée"
# Notification d'erreur. Utilisez la ligne suivante en cas de préférence pour les fenêtres zenity
## zenity --info --title="Erreur" --text="Aucune vidéo trouvée."
elif [ "$NBRE_FICHIER" != "1" ]; then
to_print=""
for poet in $FICHIER;
do date=$(stat -Lc '%y' /proc/$NPROCESS/fd/$poet | awk -F \. '{print $1}');
nom=$(stat -c '%N' /proc/$NPROCESS/fd/$poet | awk '{print $3}'| cut -d"/" -f3);
to_print="$to_print \"\" \"$poet\" \"$nom\" \"$date\"";
done;
# Fenêtre zenity de sélection multiple (option --checklist) si plusieurs vidéos en parallèle
video_a_copier=$(echo $to_print | xargs zenity --title='Choix du film à copier' --list --text="" --column=" " --column="N°" --column="Nom" --column="Date" --checklist --width=500 --height=300 --separator='\n');
echo "$video_a_copier" > $COPY_FOLDER/recup1;
# "echo" utilisé pour vérifier la sortie en liste
renommer=$(stat -c '%N' /proc/$NPROCESS/fd/$video_a_copier | awk '{print $3}'| cut -d"/" -f3);
# Sélection des vidéos à copier et variable de renommage
echo "$renommer" > $COPY_FOLDER/recup2
# "echo" utilisé pour vérifier le renommage en série
## Problème : seule la première vidéo obtient une variable de renommage
if [ -z "$video_a_copier" ]; then
exit 0;
# Bouton annuler ou aucune vidéo choisie
fi
taille_1=$(stat -Lc '%s' /proc/$NPROCESS/fd/$video_a_copier)
sleep 1
taille_2=$(stat -Lc '%s' /proc/$NPROCESS/fd/$video_a_copier)
while test $taille_1 -ne $taille_2
do
taille_mo=$(($taille_2/10**6))
notify-send -i clock "Copy-Flash" "\nAttente de la fin de mise en cache : $taille_mo Mo" &
taille_1=$(stat -Lc '%s' /proc/$NPROCESS/fd/$video_a_copier)
sleep 10
taille_2=$(stat -Lc '%s' /proc/$NPROCESS/fd/$video_a_copier)
# Attente durant la mise en cache et notifications
if test $taille_1 -eq $taille_2
then
break
fi
done
taille_mo=$(($taille_2/10**6))
cp /proc/$NPROCESS/fd/$video_a_copier $COPY_FOLDER; mv $COPY_FOLDER/$video_a_copier $COPY_FOLDER/$renommer &
notify-send -i folder-download "Copy-Flash" "\nCopie de la vidéo $renommer ($taille_mo Mo) \n$COPY_FOLDER" &
# Copie de la vidéo et notification si la mise en cache est terminée
## Problème : seule la premiere vidéo est copiée
else
taille_1=$(stat -Lc '%s' /proc/$NPROCESS/fd/$FICHIER)
sleep 1
taille_2=$(stat -Lc '%s' /proc/$NPROCESS/fd/$FICHIER)
while test $taille_1 -ne $taille_2
do
taille_mo=$(($taille_2/10**6))
notify-send -i clock "Copy-Flash" "\nAttente de la fin de mise en cache : $taille_mo Mo" &
taille_1=$(stat -Lc '%s' /proc/$NPROCESS/fd/$FICHIER)
sleep 10
taille_2=$(stat -Lc '%s' /proc/$NPROCESS/fd/$FICHIER)
if test $taille_1 -eq $taille_2
then
break
fi
done
taille_mo=$(($taille_2/10**6))
renommer=$(stat -c '%N' /proc/$NPROCESS/fd/$FICHIER | awk '{print $3}'| cut -d"/" -f3)
cp /proc/$NPROCESS/fd/$FICHIER $COPY_FOLDER; mv $COPY_FOLDER/$FICHIER $COPY_FOLDER/$renommer &
notify-send -i folder-download "Copy-Flash" "\nCopie de la vidéo $renommer ($taille_mo Mo) \n$COPY_FOLDER" &
fi
# Notifications, attente et copie directe si une seule vidéo est présente
exit 0