Exit bash script on error in loop
Johnny Rosenberg
gurus.knugum at gmail.com
Thu Sep 12 21:16:22 UTC 2013
2013/9/12 Tony Arnold <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
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
> 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/20130912/bdaa7a69/attachment.html>
More information about the ubuntu-users
mailing list