1846472 Members
4611 Online
110256 Solutions
New Discussion

Re: Shell question

 
SOLVED
Go to solution
SHABU KHAN
Trusted Contributor

Shell question

Hi All,

There is a script which executes different shell scripts within it, let's say I have a script which executes 3 different scripts, my question is how many shell does it spawn, if three, how can I make it not to spawn new shells and run the script in the same shell ..

Any help would be greatly appreciated.

Thanks,
Shabu
8 REPLIES 8
Jordan Bean
Honored Contributor

Re: Shell question

If the scripts are written for the same shell, then you may simply source them.

If sh or ksh, then `. script` or `. /path/to/script`.

If csh, then `source script` or `source /path/to/script`.

But if you want each of these to run concurrently, then they must run in there own shells.
A. Clay Stephenson
Acclaimed Contributor

Re: Shell question

Hi:

In your case, each script is a new process but only the parent and one child is run at a time therefore you have at most two shells running at any one time.

You have a couple of options:

. myshell2.sh

This will run .myshell.sh as simply a part of the parent shell BUT you must make certain that myshell2.sh does not use an exit statement or myshell2.sh AND the parent (since they are one and the same) will terminate.

You could also use exec BUT this is not a good answer because exec replaces the current shell with another.



If it ain't broke, I can fix that.
Steven Sim Kok Leong
Honored Contributor

Re: Shell question

Hi,

Further to this, if you are using pipes in your shell script, each pipe will spawn off a different process.

eg. ps -fae|grep blah

There will be two processes spawned.

Hope this helps. Regards.

Steven Sim Kok Leong
S.K. Chan
Honored Contributor
Solution

Re: Shell question

When you run another script in your "main script", another shell will be spawned. To avoid it, start the script with a "."

. <script-name>
SHABU KHAN
Trusted Contributor

Re: Shell question


Thanks for all your answers !

So, in my case can I say that my script spawns three shells and if I want my script to not to do that then

. scriptname will only run in the current shell (one shell) provided I don't have an exit statement ?

Will my script perform better if I don't break up my script into different scripts and do everything in one single script ?

Thanks for your assistance Guys !

-Shabu
Darrell Allen
Honored Contributor

Re: Shell question

That's almost an "it depends" question. I don't think there's much difference in system overhead between running one shell or 4, even if you source them or not.

If you create variables in the scripts you call and want to use them in the calling script, then you should source the called scripts.

If the called scripts can "stand on their own" and would be useful to be stand-alone scripts, then that's what I'd do.

If you can run the called scripts concurrently, you could start them in the background from the calling script. You can use the "wait" command if you need all of them to finish before continuing in the calling script.

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
SHABU KHAN
Trusted Contributor

Re: Shell question

Thanks Darrell !

That's what I thought too, shouldn't be much of a overhead.

Different question: How do I make changes to the "Assign Points" section ? Do you know ?

-Shabu
Peter Kloetgen
Esteemed Contributor

Re: Shell question

Hi Shabu,

I think it is not possible to make changes after assigning points.

Please remember, if you start a script like described by the others here:

. scriptname

you have to be sure, that your script is using the same syntax! This way, you can't use scripts which use diffenrent shells, like kornshell- scripts and in that script c-shell- scripts. This would cause errors. You can simply find out which shell interprets the script in most cases looking for a line like this:

# !/usr/bin/ksh -->> kornshell
# !/usr/bin/csh -->> c- shell

Allways stay on the bright side of life!

Peter



I'm learning here as well as helping