User Tools

Site Tools


cookbooks:bash-progress-zenity

BASH : Barre de progression de sous-tâches

Script de démonstartion d'une barre de progression utilisant ZENITY pour tracker la progression des tâches lancées en arrière plan.

#! /usr/bin/env bash
if [ -t 0 ] ; then
   DIALOG=${DIALOG=dialog}
else
   DIALOG=${DIALOG=gdialog}
fi
fichtemp2=`tempfile 2>/dev/null` || fichtemp2=/tmp/test2$$
trap "rm -f $fichtemp2" 0 1 2 5 15
[ -n "$DEBUG" ] && set -x
not_staged_pattern="Changes not staged for commit"
untracked_pattern="Untracked files:"
commit_pattern="Changes to be committed:"
committed_patern="(.*) file changed, (.*) insertions\(+\), (.*) deletions\(-\)"
TASK[0]=0

export TASK

task1() {
	folder=$1
    pfile="/tmp/tasks_procfile"
    echo {1..3} >$pfile
	#tache 1: S'il des modif unstagged ou s'il existe des fichiers untracked on fait un git add -A
	#
	(
	filep="/tmp/task1_progress"
	git_status="$(LANG=en_US git -C $folder commit 2> /dev/null)"
	i=1
	if [[ ${git_status} =~ ${not_staged_pattern} || ${git_status} =~ ${untracked_patternn} ]] ; then
		cmd=($(cat /tmp/task1_libelle))
    	LANG=en_US "${cmd[@]}" & 
    	PROC_ID=$!
		wait ${PROC_ID}
        echo "$?" >$filep
    else
    	echo 0 >$filep
	fi
    )
	#tache 2: On refait un status 
	#		  S'il y a des changements à prendre en compte on fait un git commit
	#
	(
	filep="/tmp/task2_progress"

    git_status="$(LANG=en_US git -C $folder commit 2> /dev/null)"
   	if [[ ${git_status} =~ ${commit_pattern} ]] ; then
    	cmd=($(cat /tmp/task2_libelle))
   		LANG=en_US "${cmd[@]}" & 
    	PROC_ID=$!
		wait ${PROC_ID}
        echo "$?" >$filep
	fi
    )
	#tache 3: On refait un status
	#         S'il y a eu des changements on fait un push
	#
	(
	filep="/tmp/task3_progress"
    git_status="$(LANG=en_US git -C $folder commit 2> /dev/null)"
    if [[ ${git_status} =~ ${commited_pattern} ]] ; then
    	cmd=($(cat /tmp/task3_libelle))
    	LANG=en_US "${cmd[@]}" & 
		PROC_ID=$!
		wait ${PROC_ID}
        echo "$?" >$filep
	fi
    )  

}

progress-zenity () {

unset TIME_TO_FINISH PROGRESS STATUS
pfile="/tmp/tasks_procfile"
i=0
while /bin/true;
do
   	unset TASKS
   	TOTPR=0
   	IFS=' '
   	j=1
   	k=0
   	for i in `cat $pfile`; do (( k++ )) ; done
    for i in `cat $pfile`;
    	do
    		LIBELLE[$i]=$(cat /tmp/task${i}_libelle)
    		STATUS[$i]=$(cat /tmp/task${i}_progress)
   			if [ ${STATUS[$i]} -ne 7 ] ; then
           		PROGRESS[$i]=100
                TOTPR=$((TOTPR+PROGRESS[$i]/k))
                j=$(( i + 1))
            else
           		PROGRESS[$i]=0
                TOTPR=$((TOTPR+PROGRESS[$i]/k))
       	   	fi
       	
       	TASKS=$(cat /tmp/task${j}_libelle)	
        (
        echo "${TOTPR}" ; sleep 1 # This command will echo a line begin with number "10". It will replace --percentage option.
        echo "# ${TASKS}" ; sleep 1 # This command will echo a line begin with # character. It will replace --text option.
     	) 
    done 
    

    IFS=' '
    # Create a list contain some commands
     
    I=0
    for a in `cat $pfile`; do [[ ${PROGRESS[$a]} -ge 100 ]] && I=$((I+1)); done

    [[ $I -eq $i ]] && break
    sleep 1
done
echo waiting
wait

}
#Initialisation des taches
rm /tmp/task*
echo 7 >"/tmp/task1_progress"
echo "git -C /data/espace-pro/workspace/git/kickstarts add -A" >"/tmp/task1_libelle"
echo 7 >"/tmp/task2_progress"
echo "echo \"on fait un commit\"" >"/tmp/task2_libelle" 
echo 7 >"/tmp/task3_progress"
echo "echo \"on fait un push\"" >"/tmp/task3_libelle" 
echo "Processus Terminé..." >"/tmp/task4_libelle" 
task1 /data/espace-pro/workspace/git/kickstarts & 

p
set +x
cookbooks/bash-progress-zenity.txt · Last modified: 2025/02/19 10:59 by 127.0.0.1