Exit bash script on error in loop

Johnny Rosenberg gurus.knugum at gmail.com
Thu Sep 12 20:59:31 UTC 2013


2013/9/12 Nils Kassube <kassube at gmx.net>

> 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.
>
> Why do you put the yad command in the background? Maybe that's the
> reason why it doesn't work. Would it be a problem if the script stops
> after the yad job exits? After all it seems like it should display the
> error message for only 5 seconds.
>

I tried without putting it to the background too before I posted this, but
there was no difference, so I kept the & character in this example.


>
> > (yad is a dialogue, similar to zenity, or even a zenity fork, I think
> > – yad=Yet Another Dialogue)
>
> Why do you use yad and not zenity? A short test with zenity worked here.
>

Zenity had a nasty bug when used in a special way, and since I don't like
mixing things that basically do the same thing, I just decided to go with
yad instead. This example works with both, but I have a few more scripts
which didn't work with Zenity, and the developers didn't seem too keen on
correcting the bug. Right now I don't even remember what the bug was about,
though…

Anyway, here's a ”working” example to show you what I mean. Just type it in
and run it… (replace yad with zenity if you don't have yad installed, or
install yad).

#!/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

Since the path given doesn't exist, the progress dialogue will count down
time for ${WaitTime} seconds. After that the Folder missing dialogue will
appear and disappear after 5 seconds. The script should then exit, but it
doesn't, because the last dialogue comes up, and it shouldn't, should it?
If it should, what die I do wrong?



Johnny Rosenberg




>
>
> Nils
>
>
> --
> 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/113d8e7d/attachment.html>


More information about the ubuntu-users mailing list