|
Home > Archive > Unix Shell > October 2006 > Exiting from a subshell
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Exiting from a subshell
|
|
| radhika.penagonda@gmail.com 2006-10-24, 1:17 pm |
| I have these commands:
cat abc || (echo "Failed to cat abc" ; exit 1)
echo "Did not exit"
This is what I am trying to do :If cat abc fails, echo and exit
However, instead of exiting, the shell script prints the Did not exit
as well.
what is going wong in this execution or my understanding?
How do I do what I want here.
| |
| Radoulov, Dimitre 2006-10-24, 1:17 pm |
|
> cat abc || (echo "Failed to cat abc" ; exit 1)
> echo "Did not exit"
>
> This is what I am trying to do :If cat abc fails, echo and exit
>
> However, instead of exiting, the shell script prints the Did not exit
> as well.
>
> what is going wong in this execution or my understanding?
> How do I do what I want here.
$ cat abc || (echo "Failed to cat abc" ; exit 1;) && echo "Did not exit"
cat: abc: No such file or directory
Failed to cat abc
or
$ cat abc 2>&- || (echo "Failed to cat abc" ; exit 1;) && echo "Did not
exit"
Failed to cat abc
Regards
Dimitre
| |
| Radoulov, Dimitre 2006-10-24, 1:17 pm |
| > > cat abc || (echo "Failed to cat abc" ; exit 1)
>
> $ cat abc || (echo "Failed to cat abc" ; exit 1;) && echo "Did not exit"
> cat: abc: No such file or directory
> Failed to cat abc
>
> or
>
> $ cat abc 2>&- || (echo "Failed to cat abc" ; exit 1;) && echo "Did not
> exit"
> Failed to cat abc
My mistake,
if you want to exit, don't open a new shell with (), use {} instead:
cat abc || { echo "Failed to cat abc" ; exit 1; }
Regards
Dimitre
| |
| radhika.penagonda@gmail.com 2006-10-24, 1:17 pm |
| Suppose in place of echo "Did not exit", I have a whole series of
commands in the shell cript, I won't be able to do &&
In that case, instead of exiting, it executes all the commands that
follow exit 1;
Please suggest how to exit from the subshell ( ) and shell script
Radoulov, Dimitre wrote:
>
> $ cat abc || (echo "Failed to cat abc" ; exit 1;) && echo "Did not exit"
> cat: abc: No such file or directory
> Failed to cat abc
>
> or
>
> $ cat abc 2>&- || (echo "Failed to cat abc" ; exit 1;) && echo "Did not
> exit"
> Failed to cat abc
>
>
> Regards
> Dimitre
| |
| radhika.penagonda@gmail.com 2006-10-24, 1:17 pm |
| I am using the Ksh
I tried the { }, in place of ( ). It gives syntax errors...
Radoulov, Dimitre wrote:
>
> My mistake,
> if you want to exit, don't open a new shell with (), use {} instead:
>
> cat abc || { echo "Failed to cat abc" ; exit 1; }
>
>
>
> Regards
> Dimitre
| |
| radhika.penagonda@gmail.com 2006-10-24, 1:17 pm |
|
radhika.penago...@gmail.com wrote:[vbcol=seagreen]
> I am using the Ksh
>
> I tried the { }, in place of ( ). It gives syntax errors...
>
>
> Radoulov, Dimitre wrote:
| |
| radhika.penagonda@gmail.com 2006-10-24, 1:17 pm |
| Sorry, I tried again. It works!!! :O)
When i tried last, I had missed out the ';' before }
Thanks a ton. I have been racking my brain since morning..
radhika.penago...@gmail.com wrote:[vbcol=seagreen]
> I am using the Ksh
>
> I tried the { }, in place of ( ). It gives syntax errors...
>
>
> Radoulov, Dimitre wrote:
| |
| Michael Tosch 2006-10-25, 1:27 pm |
| radhika.penagonda@gmail.com wrote:
> Suppose in place of echo "Did not exit", I have a whole series of
> commands in the shell cript, I won't be able to do &&
>
> In that case, instead of exiting, it executes all the commands that
> follow exit 1;
>
> Please suggest how to exit from the subshell ( ) and shell script
>
>
>
> Radoulov, Dimitre wrote:
>
if cat abc
then
echo "Did not exit"
else
echo "Failed to cat abc"
exit 1
fi
--
Michael Tosch @ hp : com
| |
| Michal Nazarewicz 2006-10-26, 7:16 am |
| radhika.penagonda@gmail.com writes:
> I have these commands:
>
> cat abc || (echo "Failed to cat abc" ; exit 1)
> echo "Did not exit"
>
> This is what I am trying to do :If cat abc fails, echo and exit
>
> However, instead of exiting, the shell script prints the Did not exit
> as well.
>
> what is going wong in this execution or my understanding?
> How do I do what I want here.
For things as simple as printing error message and then exiting I use
a function like so:
#v+
err () {
__err_num=$1
shift
printf %s\\n "$*" >&2
if [ X"$__err_num" != X- ]; then exit "$__err_num"; fi
}
#v-
and then use it like so:
#v+
cat abc || err 1 Could not cat abc
#v-
or if I only want to print error message:
#v+
cat abc || err - Could not cat abc
#v-
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
|
|
|
|
|