User Tools

Site Tools


notes:bash-progress-dialog

BASH : Barre de progression de sous-tâches

Script de démonstartion d'une barre de progression utilisant DIALOG 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/task${i}_libelle))
    	LANG=en_US "${cmd[@]}" & 
    	PROC_ID=$!
		wait ${PROC_ID}
        echo "$?" >$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
   		echo "on fait un commit" &
    	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
    	echo "on fait un push" &
		PROC_ID=$!
    	PROC_ID=$!
		wait ${PROC_ID}
        echo "$?" >$filep
	fi
    )  

}



progress-dialog () {

unset TIME_TO_FINISH PROGRESS STATUS
pfile="/tmp/tasks_procfile"
i=0
while /bin/true;
do
   	unset TASKS
    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
           	else
           		PROGRESS[$i]=0
       	   	fi
        TASKS+=("${LIBELLE[$i]}" "${STATUS[$i]}")
    done

    # 0: success
    # 1: failed
    # 2: passed
    # 3: completed
    # 4: checked
    # 5: done
    # 6: skipped
    # 7: in progress
    # -X: 0-100, progress of process
    dialog \
        --title "Mixed gauge demonstration" \
        --backtitle "Backtitle" \
        --mixedgauge "This is a prompt message,\nand this is the second line." \
            0 0 $(($((${PROGRESS[1]}+${PROGRESS[2]}+${PROGRESS[3]}))/3)) \
            "${TASKS[@]}"

    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"
task1 /data/espace-pro/workspace/git/kickstarts & 

progress-dialog

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