Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: How to add Zenity/YAD to this script to show progress bar?

  1. #1
    Join Date
    Nov 2012
    Beans
    265
    Distro
    Ubuntu 13.04 Raring Ringtail

    Question How to add Zenity/YAD to this script to show progress bar?

    Hi,

    I have this script that I found on the net which helps me copy files with specific names to specific folders.
    I want to use this script to copy some large files from local disk to a NAS, and I would like to see a GUI progress bar while copying.
    Can anyone help me with that?

    Here's the script:

    Code:
    dir1=~/tmp/dir1
    dir2=~/tmp/dir2
    
    
    for f in *.avi *.mp4; do
        case $f in
             *[Tt][Ee][Ss][Tt]*) mv -n $f $dir1 ;;
             *[Ff][Ii][Ll][Ee]*) mv -n $f $dir2 ;;
        esac
    done
    Thanks!
    Last edited by Thee; May 21st, 2014 at 02:32 PM.

  2. #2

    Re: How to add Zenity/YAD to this script to show progress bar?

    I'd make the 'for f in" a function:

    Code:
    dir1=~/tmp/dir1 ;<-- this may not work, try absolute /path/to/tmp/dir1
    dir2=~/tmp/dir2 ;<-- this may not work, try absolute /path/to/tmp/dir2
    
    function do_work ()
    {
    for f in *.avi *.mp4; do
        case $f in
             *[Tt][Ee][Ss][Tt]*) mv -n $f $dir1 ;;
             *[Ff][Ii][Ll][Ee]*) mv -n $f $dir2 ;;
        esac
    done
    }
    then 'touch' a /tmp file and call the function, then remove the /tmp file as in
    Code:
    touch /tmp/$$
    ( ( echo 1 ; while [ -f /tmp/$$ ] ; do sleep 1 ; done ; echo 100 ) | yad  --progress --pulsate --auto-close --text="Doing Work..."  --width=150 --title="" --undecorated --no-buttons) & do_work
    rm /tmp/$$
    See http://bournetoraiseshell.com/articl...11070711554993 for my similar script.

    I haven't been able to have the progress bar work in this kinds of activity, so I used pulsate to indicate activity.
    Hope that helps.
    Last edited by Habitual; May 18th, 2014 at 08:23 PM.
    Windows assumes the user is an idiot.
    Linux demands proof.

  3. #3
    Join Date
    Nov 2012
    Beans
    265
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: How to add Zenity/YAD to this script to show progress bar?

    Yeah I guess the pulsate will have to do then, even if it doesn't help to know how much is copied. Thanks in any case

  4. #4
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: How to add Zenity/YAD to this script to show progress bar?

    in order to have proper progress bar you'd have to collect relevant files first (put them in array) count them, optionally calculate their sizes if you want progress by size not by number of items.


    Code:
    #!/bin/bash
    
    dir1=./dir1
    dir2=./dir2
    
    
    declare -A files # [$file]=$destination
    
    for f in *.avi *.mp4
    do
        case $f in
             *[Tt][Ee][Ss][Tt]*) dest=$dir1 ;;
             *[Ff][Ii][Ll][Ee]*) dest=$dir2 ;;
             *)                  continue ;;
        esac
        files[$f]=$dest   
    done
    
    for k in "${!files[@]}"
    do
        echo mv -n -t "${files[$k]}" "$k" >&2
        sleep 0.1
        echo $(( ++i*100/${#files[@]} ))%
    done | zenity --progress
    Last edited by Vaphell; May 19th, 2014 at 12:08 AM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  5. #5

    Re: How to add Zenity/YAD to this script to show progress bar?

    Thanks Vaphell,
    I can always count on you to make code "better".
    Windows assumes the user is an idiot.
    Linux demands proof.

  6. #6
    Join Date
    Nov 2012
    Beans
    265
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: How to add Zenity/YAD to this script to show progress bar?

    Thanks! I'll give it a try

  7. #7
    Join Date
    Nov 2012
    Beans
    265
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: How to add Zenity/YAD to this script to show progress bar?

    Looks like it doesn't work..

    Code:
    movefile3.sh: 8: movefile3.sh: declare: not found
    movefile3.sh: 17: movefile3.sh: files[file.avi]=/home/thee/: not found
    movefile3.sh: 17: movefile3.sh: files[test.avi]=/home/thee/: not found
    movefile3.sh: 20: movefile3.sh: Bad substitution

  8. #8
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: How to add Zenity/YAD to this script to show progress bar?

    are you using bash or sh?
    associative arrays are a bash feature.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  9. #9
    Join Date
    Nov 2012
    Beans
    265
    Distro
    Ubuntu 13.04 Raring Ringtail

    Re: How to add Zenity/YAD to this script to show progress bar?

    I used sh, my fault, but still the script doesn't move the files when I used bash instead... only shows 100% progress bar and OK button..

    Code:
    $ bash movefile3.sh
    mv -n -t /home/thee/ file.avi
    mv -n -t /home/thee/ test.avi

  10. #10
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: How to add Zenity/YAD to this script to show progress bar?

    note that in my proof of concept i put echo(sent to stderr) in front of mv, effectively neutering it, so the progress of the processing could be eyeballed in terminal too to confirm the progress bar works correctly. I also put superfluous sleep there to emulate mv delays, because otherwise the whole loop effectively did nothing and ended in 0.01s.

    don't call shell explicitly when running script, you have a hashbang line (#!/bin/bash) for that. chmod u+x the script and run it like

    Code:
    ./movefile3.sh
    the system will use the shell specified in the script.
    Last edited by Vaphell; May 21st, 2014 at 01:26 PM.
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •