3

I am writing a simple basic bash script (starting with #! /bin/bash, file format is .sh) and I am trying to make a progress bar work:

#!/bin/bash  
echo "You are running in LXDE mode. Answer 'yes' or 'no' on the following question to continue (or not) in LXDE mode."
zenity --question --text='Do you want to continue in LXDE mode?' --ok-label=Yes --cancel-label=No
echo "Please enter your username and password to continue because the following command needs root privileges."
zenity --password --username
echo "Please enter today's date:"
zenity --calendar --text="Please enter today's date:"
echo "Please enter your name:"
zenity --entry --text='Please enter your name on the text entry below:'
echo "Analyzing data..."
zenity --info --text='Now begin analyzing data. If it takes more than 40 seconds, click on "Cancel".'
zenity --progress --title='Analyzing data...' --pulsate

I have tried to make it move from 0% to 100%, and nothing happened. It was stuck at 0%. I have also tried to make it pulsate by using the --pulsate option, still at 0% doing nothing.

Can anyone please help me? Any help would be appreciated.

  • 2
    Please edit your original question in order to show your bash script, if you need more detailed help than via the link to the zenity docs, (help.gnome.org/users/zenity/stable/progress.html.en). – sudodus Jul 1 '17 at 16:12
  • 1
    You didn't say how "you tried to make it move from 0 to 100". More importantly like @sudodus requested post your code so we can critique it. – WinEunuuchs2Unix Jul 1 '17 at 16:43
  • @WinEunuuchs2Unix I cannot upload the .sh script file right now. Maybe tomorrow. See you tomorrow, then! – Bajiru Jul 2 '17 at 11:05
  • @sudodus I am going to upload the script to Google Drive today, edit the post and type the link. – Bajiru Jul 6 '17 at 13:41
  • 1
    I could not open your script from my phone. Google says File type not supported for both links. Why not copy script directly into your question? – WinEunuuchs2Unix Jul 6 '17 at 14:55
5

The Zenity docs have a small code snippet that should do exactly what you're looking for.

#!/bin/sh
(
echo "10" ; sleep 1
echo "# Updating mail logs" ; sleep 1
echo "20" ; sleep 1
echo "# Resetting cron jobs" ; sleep 1
echo "50" ; sleep 1
echo "This line will just be ignored" ; sleep 1
echo "75" ; sleep 1
echo "# Rebooting system" ; sleep 1
echo "100" ; sleep 1
) |
zenity --progress \
  --title="Update System Logs" \
  --text="Scanning mail logs..." \
  --percentage=0

if [ "$?" = -1 ] ; then
        zenity --error \
          --text="Update canceled."
fi

First try just copying in the code that's there and running it and confirming that it works as intended, then modify to add in your code where appropriate.

If your progress bar is stuck at zero, make sure to by-pass any sections of the script that may be hanging and making you think that it's actually working!

Edit: As stated in the Answer below, the reason it's not working is because zenity expects the progress to be echoed to it, like in the code sample.

  • This is more a comment than an answer but perhaps you don't have sufficient rep yet to post comments. So I'll upvote to give you 10 more points-- not because this is a good answer, but because it's a good comment. – WinEunuuchs2Unix Jul 1 '17 at 16:44
  • I get a 403 Forbidden on this page :/ – that other guy Jan 31 '18 at 21:36
  • You're right. Fixed, I think. – Bujiraso Feb 9 '18 at 22:48
2

The way zenity works for displaying progress bars is capturing your echo commands from your bash script via the | (pipe) redirection command (symbol).

Here is an example you can try that I lifted from Ubuntu Forums:

#!/bin/bash

# Force Zenity Status message box to always be on top.


(
# =================================================================
echo "# Running First Task." ; sleep 2
# Command for first task goes on this line.

# =================================================================
echo "25"
echo "# Running Second Task." ; sleep 2
# Command for second task goes on this line.

# =================================================================
echo "50"
echo "# Running Third Task." ; sleep 2
# Command for third task goes on this line.

# =================================================================
echo "75"
echo "# Running Fourth Task." ; sleep 2
# Command for fourth task goes on this line.


# =================================================================
echo "99"
echo "# Running Fifth Task." ; sleep 2
# Command for fifth task goes on this line.

# =================================================================
echo "# All finished." ; sleep 2
echo "100"


) |
zenity --progress \
  --title="Progress Status" \
  --text="First Task." \
  --percentage=0 \
  --auto-close \
  --auto-kill

(( $? != 0 )) && zenity --error --text="Error in zenity command."

exit 0

If you follow the link to Ubuntu Forums you can read a discussion of this script. If after that you still have questions please ask via comment below and I'll do my best to answer them for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.