Exit bash script on error in loop

Johnny Rosenberg gurus.knugum at gmail.com
Fri Sep 13 17:04:28 UTC 2013


2013/9/13 Tony Arnold <tony.arnold at manchester.ac.uk>

> Johnny,
>
> Do you just echo a percentage of progress number to the progress
> process, i.e., a number between 0 and 100? Why not just echo 100 just
>  before the exit statement.


I probably don't understand what you mean, but I want the progress bar to
be just that, a bar that show the progress smoothly. In this case, I give
the user ${WaitTime} seconds to connect his device (or at least I do in the
real code). The progress bar is supposed to illustrate how much time there
is for the user to do that.

I probably misunderstood the question completely, maybe because English is
not my native language.

Anyway, it seems like the problem is solved now anyway. Only details left
now to make my script bug free… I'll look into it later this weekend.


> Won't that kill the progress bar too? You
> will need the --auto-close option, which you have anyway!
>

The progress bar was killed properly. The problem was that ”exit 1” didn't
stop the script, so whatever I had after the loop was still executed.


Johnny Rosenberg



>
> Just a thought.
>
> Regards,
> Tony.
>
> On 12/09/13 22:40, Johnny Rosenberg wrote:
> > Okay, now that I found that the pipe was causing the problem, I also
> > found the answer: PIPESTATUS. See below…
> >
> >
> > 2013/9/12 Johnny Rosenberg <gurus.knugum at gmail.com
> > <mailto:gurus.knugum at gmail.com>>
> >
> >     2013/9/12 Tony Arnold <tony.arnold at manchester.ac.uk
> >     <mailto:tony.arnold at manchester.ac.uk>>
> >
> >         Johnny,
> >
> >         On 12/09/13 19:19, Johnny Rosenberg wrote:
> >         >
> >         > I can't make exit work as I thought it would work…
> >         >
> >         > #!/bin/bash
> >         >
> >         > Title="Error"
> >         > ERROR="Something bad happened."
> >         >
> >         > while [ something ]; do
> >         >     if [[ something_bad ]]; then
> >         >         yad --title "${Title}" \
> >         >             --image=error \
> >         >             --text "${ERROR}" \
> >         >             --no-buttons \
> >         >             --timeout 5 &
> >         >         exit 1
> >         >     fi
> >         > done
> >         > more_statements
> >         >
> >         > # End of code
> >         >
> >         > The problem is that even if something_bad happens,
> >         more_statements are
> >         > executed. The yad error messages is displayed as expected,
> though.
> >         >
> >         > (yad is a dialogue, similar to zenity, or even a zenity fork,
> >         I think –
> >         > yad=Yet Another Dialogue)
> >         >
> >         > So it seems like only the loop is exited when exit 1 is
> >         executed, not
> >         > the whole script. So how can I exit the whole script if
> >         something bad
> >         > happens inside a loop? I'd prefer a solution that doesn't make
> it
> >         > necessary to rewrite the script from scratch…
> >
> >         Odd. The following works for me. I'm on 13.04. Cannot find yad
> >         in the
> >         repositories. Where did you get it from?
> >
> >
> >     I don't remember, but I think I compiled it from source.
> >
> >
> >         And are you definitely using
> >         bash for your script?
> >
> >
> >     I'm on Ubuntu 12.04 and I didn't change anything like that.
> >
> >
> >
> >         #!/bin/bash
> >
> >         i=4
> >         while [ "$i" -gt "0" ]; do
> >             echo "$i - Hello"
> >             if [ "$i" -eq "2" ]; then
> >                 zenity --error --text "Oh no! i=3"
> >                 exit 1
> >             fi
> >             let i=i-1
> >         done
> >         echo "Oops!"
> >
> >
> >
> >     This one works for me too. I also replaced zenity with yad, and it
> >     still works.
> >
> >     I did some more experiments and I found that it's my pipe to a yad
> >     progress bar that cause the problem. Unfortunately I didn't include
> >     it in my example, but here's another example, which I included in a
> >     reply to another reply:
> >
> >     #!/bin/bash
> >
> >     WaitTime=10
> >     TargetPath="This/path/does/not/exist"
> >
> >     if [ ! -e "${TargetPath}" ]; then
> >         Time=0
> >         StartTime=$(date +%s%N)
> >
> >         while [ ! -e "${TargetPath}" ]; do
> >             sleep .1
> >             Time=$(($(date +%s%N)-StartTime))
> >             if [[ Time -ge WaitTime*10**9 ]]; then
> >                 yad --title "Folder missing" \
> >                     --image=error \
> >                     --text "Your device is not connected." \
> >                     --no-buttons \
> >                     --timeout 5
> >                 exit 1
> >             fi
> >             echo "$((Time/10**7/WaitTime))"
> >             Seconds=$((WaitTime-Time/10**9))
> >             MM=$((Seconds/60))
> >             SS=$((Seconds-MM*60))
> >             echo \#$(printf "%s %d:%02d." "Time to connect your device:"
> >     "$MM" "$SS")
> >         done | \
> >         yad --progress \
> >             --title="Folder missing" \
> >             --image=time \
> >             --percentage=0 \
> >             --auto-close
> >     fi
> >
> >
> > Now, it seems like I need to check for the pipe status, for instance:
> > [[ PIPESTATUS[0] -ne 0 ]] && exit ${PIPESTATUS[0]}
> >
> > or
> >
> > [ ${PIPESTATUS[0]} -ne 0 ] && exit ${PIPESTATUS[0]}
> >
> > or
> >
> > [[ PIPESTATUS[0] != 0 ]] && exit ${PIPESTATUS[0]}
> >
> > or
> >
> > [[ PIPESTATUS[0] ]] && exit ${PIPESTATUS[0]}
> >
> > and so on…
> >
> > Thanks for replying and making me think in the right direction!
> >
> >
> > Johnny Rosenberg
> >
> >
> >
> >     yad --image="info" \
> >         --title="Another dialogue" \
> >         --text="You shouldn't see this dialogue"
> >
> >     # End of code
> >
> >     Remove the yad progress thing and it works. The problem is that I
> >     WANT the progress thing…
> >     So I guess the pipe is the problem here somehow, but how and what to
> >     do about it?
> >
> >
> >     Johnny Rosenberg
> >
> >
> >
> >
> >         Regards,
> >         Tony.
> >         --
> >         Tony Arnold,                        Tel: +44 (0) 161 275 6093
> >         <tel:%2B44%20%280%29%20161%20275%206093>
> >         Head of IT Security,                Fax: +44 (0) 705 344 3082
> >         <tel:%2B44%20%280%29%20705%20344%203082>
> >         University of Manchester,           Mob: +44 (0) 773 330 0039
> >         <tel:%2B44%20%280%29%20773%20330%200039>
> >         Manchester M13 9PL.                 Email:
> >         tony.arnold at manchester.ac.uk <mailto:
> tony.arnold at manchester.ac.uk>
> >
> >         --
> >         ubuntu-users mailing list
> >         ubuntu-users at lists.ubuntu.com <mailto:
> ubuntu-users at lists.ubuntu.com>
> >         Modify settings or unsubscribe at:
> >         https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
> >
> >
> >
> >
> >
>
> --
> Tony Arnold,                        Tel: +44 (0) 161 275 6093
> Head of IT Security,                Fax: +44 (0) 705 344 3082
> University of Manchester,           Mob: +44 (0) 773 330 0039
> Manchester M13 9PL.                 Email: tony.arnold at manchester.ac.uk
>
> --
> ubuntu-users mailing list
> ubuntu-users at lists.ubuntu.com
> Modify settings or unsubscribe at:
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ubuntu.com/archives/ubuntu-users/attachments/20130913/84e87d84/attachment.html>


More information about the ubuntu-users mailing list