| View previous topic :: View next topic |
| Author |
Message |
Vieri Tux's lil' helper

Joined: 18 Dec 2005 Posts: 109
|
Posted: Thu Oct 16, 2008 4:10 pm Post subject: [SOLVED] simple bash logic question |
|
|
Not strictly Gentoo-related but I'm wondering if someone could enlighten me on this.
I'd like to understand how "grouped conditions" work in bash. When I run the following code and the mount point doesn't exist, it runs the commands wthin || (). So, "error" is echoed out as expected. However, "continue" is also printed and if I check $? after the run I get 0.
Why doesn't the script "exit" with status 1 on the first line, within || () ?
The only way I found this to work as I want it to is by using the last lines I commented out.
Bash code:
| Code: |
#!/bin/bash
mount /furnstugurn || (echo "error"; exit 1)
echo "continue"
exit 0
#if [ $? -ne 0 ]; then
# echo "error"; exit 1
#fi
|
Thanks in advance,
Vieri
Last edited by Vieri on Thu Oct 16, 2008 5:48 pm; edited 1 time in total |
|
| Back to top |
|
 |
yabbadabbadont Veteran


Joined: 13 Mar 2003 Posts: 4739
|
Posted: Thu Oct 16, 2008 4:18 pm Post subject: |
|
|
| By placing the echo and exit within parends (), you are running those commands within a sub-process that will "exit 1". Because it is a sub-process, it doesn't affect the main process. |
|
| Back to top |
|
 |
Vieri Tux's lil' helper

Joined: 18 Dec 2005 Posts: 109
|
Posted: Thu Oct 16, 2008 5:48 pm Post subject: |
|
|
| Thanks! |
|
| Back to top |
|
 |
elzbal Guru


Joined: 30 Aug 2002 Posts: 364 Location: Seattle, WA, USA
|
Posted: Sun Oct 19, 2008 10:07 pm Post subject: |
|
|
Just a reference note for future readers of this thread...
See also "man bash". Look at the "Shell Grammar" section, specifically the sections on "Compound Commands" and "Lists". There's a bit more information there about this. |
|
| Back to top |
|
 |
|