Search code examples
bashshellfor-loopscriptingzenity

Pass data from for loop to zenity progress


I have a for loop for loop over an array which holds files (or besser filenames) i want to display a zenity progress bar, which shows the current file in the for loop.

With the following code i managed to display the zenity progress bar but it only shows "Running..." until it there are no more entries in that array.

How can i echo the files in zenity?

if [ ${#FILES[@]} != 0 ] || die "No files to download"
then
(
    for i in ${FILES[@]}
    do
        ssh -i $ID $SSH_OPT $HOST "scan Downloads/'$i'" && OK=true
        if [ ! -v ${OK+x} ]
        then
            echo "Downloading $i"
            scp $SSH_OPT $USER@$HOST:/home/$USER/Downloads/$i /home/$USER/Downloads/
            ssh -i $ID $SSH_OPT $HOST "rm -f Downloads/$i"
        else
            echo "$i contains a virus."
            ssh -i $ID $SSH_OPT $HOST "rm -f Downloads/$i"
        fi
done
echo "100"
) | zenity --progress \
     --title="Download from $HOST" \
     --percentage=0
fi

Solution

  • To change the text shown in the zenity dialog box, you must prefix the message by a '#'. To change the progress bar percent, you just need to send the number.

    Thus, you can echo instruction like that:

        then
            echo "# Downloading ..."
            echo "$i"
    

    If you want, you can put all on the same line if you prefer this legibility:

            echo -e "# Downloading ...\n$i"