[Tech] breaking out of if-blocks in bash

Today I wanted to break out of an if-block in bash.
Neither the break nor the continue works mean anything in an if-block, so what to do? Create a subprocess (if your code allows it):
(
[ -w / ] && exit
...
)
fi
EOF
Back home
20:28:38 11.03.2011 | Permalink | Trackback URI
mirabilos (2011-03-14 13:03:45)
bar=0
if foo; then
bar=1
if baz; then “break”; fi
blah=1
fi
echo $bar
This will break:
bar=0
if foo; then (
bar=1
if baz; then exit; fi
blah=1
); fi
echo $bar
This will work:
bar=0
while foo; then
bar=1
if baz; then break; fi
blah=1
break
fi
echo $bar
//mirabilos • tg@d.o • mksh author
mirabilos (2011-03-14 13:05:22)
Erm… yes.
Warning: preg_match() [function.preg-match]: Unknown modifier 'h' in /home/www/blog.crash-override.net/engine/functions.php on line 439
Your comment has been submitted
blindcoder (2011-03-14 13:18:40)
Thanks for the code example, I guess I'll use that one from now on.
The php error comes from a stray entry in my link-blacklist. Have to check where that one came from...


