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