Link to home
Start Free TrialLog in
Avatar of jrram
jrramFlag for United States of America

asked on

How to kill parent shell/process from child subshell/process?

In the code below, I'm checking to make sure the correct # of parameters were passed into the script.  If not, I'd like to display a message to the user, then exit the program.  

Method #1:
The problem that I have here is since I enclosed my commands in (), a subshell gets created so when I call exit from the usage() function, only the subshell is killed and the rest of the code in my parent shell still executes.  This of course causes an error since not all the necessary parameters are available.

Method #2:
I tried the commands w/o the ().  The problem with this is the usage() function is always called regardless of if I have enough valid parameters or not.


So my question is 1) For Method 1, how do I kill off both the parent and child shell and for Method 2, how to I get the program to run both the echo statement and usage function in the main shell ONLY when its a valid error.
function usage () {
	echo "*** Not enough parameters ***"
        exit 1
}
 
#Method 1
[ $# -ne 6 ] && ( echo "Error: Incorrect number of parameters..." ; usage )
 
Method 2
[ $# -ne 6 ] && echo "Error: Incorrect number of parameters..." ; usage
 
#Rest of script...

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tintin
Tintin

or
if [ $# -ne 6 ]
then
   echo "Error: Incorrect number of parameters..."
   usage
fi

Open in new window

ozo and Tintin already provided proper solutions to your question, but if you're really after killing the parent process (the parent shell in this case) you could use the following code in your "usage function":
function usage () {
        echo "*** Not enough parameters ***"
        kill $$
}

Open in new window

Avatar of jrram

ASKER

Thanks for the comments guys:

>> ozo

Your solution worked perfectly.

>> Tintin

I had this code originally, but I was trying to reduce the number of lines in my code so that's why I was wanting a 1 line approach.  I appreciate the feedback though

>> agriesser

This works perfectly also.


I will add another 100 points to this for anyone to answer to a similar question.  See the post below.
Avatar of jrram

ASKER

I'm trying to convert this code to be a one line statement:

if test -d $downloadTo;
then
        cd $downloadTo
else
    echo "Directory: $downloadTo does not exist"
    exit 1
fi


In my first attempt I tried:

   test -d $downloadTo && cd $downloadTo || echo "Directory: $downloadTo does not exist" ; exit 1

Then, using ozo's suggestion I converted it to:

   test -d $downloadTo && cd $downloadTo || echo "Directory: $downloadTo does not exist" && exit 1


With both of these, it always executes the exit 1 statement.  It seem to be interpreted as:

if test -d $downloadTo;
then
        cd $downloadTo
else
    echo "Directory: $downloadTo does not exist"
fi

exit 1

======

How can I get the exit one statement to only execute in conjunction with the echo?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Just out of interest: You know that such one-liners are much harder to read and understand than the well written if block above, do you?
Avatar of jrram

ASKER

I understand where you're coming from, but I only do the one liners when the action I'm trying to perform is very simple and I make it a habit to thoroughly comment my code as to not make it difficult to maintain.  When I'm writing something more complex like sed or awk statements I rarely try to condense them.

Just wanted to have that said.
Does the one-liner above work for you?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jrram

ASKER

Thanks for all you guys help on this.