I am having a trivial doubt. Please see the below pipeline code sequence.
I am aware that the command that follows pipe will run in the sub shell by the Unix kernel. But how about here? Since these set of commands are grouped under "parantheses", will they run inside another sub shell of pipe's shell? Hope my question is not so be-wilder to be answered
Last edited by royalibrahim; 01-20-2010 at 02:33 PM..
We're not ignoring you if we haven't answered the instant you want an answer. You don't have to bump threads. In fact you shouldn't, it's against the rules you read and agreed to when you registered.
I don't think you've got that quite right. Without complex structures like brackets or loops, a command after a pipe needs no subshell. The shell just creates a new process, arranges the files how you wanted, and replaces the new process with the requested command.
A subshell does have to stick around to manage situations like you illustrated:
It has to stay resident in order to wait for command2 to finish before starting command3.
A subshell also exists in a situation like this:
The subshell has to stay resident to process code after the pipe inside the loop.
These subshells have side-effects. Try this:
It will output "hello", since variables changed inside the subshell aren't changed in its parent.
But, anything to the right of a pipe will run in a subshell, not just loops. In non POSIX shells, the pipe spawns the sub shell. The use of a single pipe in a shell creates TWO subshells, one for each side (a pipeline with two | creates three subshells, for the three commands, etc.)
So my question is, how many subshells would've spawned in this scenario.
Also, in
The background job detaches from the current shell and runs in a subshell (separate environment) and also, we are encapsulating this command inside parantheses, hence we are forcing it to run inside a subshell again. So would it create 2 subshells here? one for parantheses and one for "background"??
But, anything to the right of a pipe will run in a subshell, not just loops
There is no subshell.
There's not 5 subshells either.
Quote:
So my question is, how many subshells would've spawned in this scenario.
I think you're under a misapprehension here. The shell does clone itself in order to do redirection through pipes, but unless its actually needed, the subshell does not stick around: Once it's redirected file descriptors the way you asked it to, it replaces itself with the program you asked it to run. By the very act of running the process you asked it to, the subshell is wiped out. In its place is a brand new process with all the same redirections as the subshell used to have.
Quote:
The background job detaches from the current shell and runs in a subshell (separate environment) and also, we are encapsulating this command inside parantheses
Not "also". A subshell remains because you encapsulated it with brackets. If you had not, no subshell would remain -- the subshell would exist for less than eyeblink, connecting file descriptors as specified then replacing itself with the new command. Why bother waiting around when there's nothing left for it to do?
...I'm beginning to have a sneaking suspicion that when you say "subshell" you mean "process". Not all processes are shells.
The result is the cat executable is then running in the new process, not the shell.
When the process ends, the shell resumes in the parent process which waited for the cat process to end.
So I have this basic script, see below
import subprocess
import shlex
command = "gcloud projects list"
subprocess.check_output(shlex.split(command))
subprocess.check_call(shlex.split(command))
The subprocess.check_call(shlex.split(command)) actually return what I expect. It returns... (6 Replies)
After struggling with this for days now, I'm reaching out to the experts of all things linux for some help with this.
I'm trying to run the following working command (on command line) inside a python script using subprocess:
rsync -avzh --no-perms --delete --include="*sub*" --exclude='*'... (2 Replies)
Hi guys,
I'm learning python and perl and i was trying to run from python a perl script using the subprocess module.
I have an issue that i don't understand regarding this.
I run this code:
#!/usr/bin/python
import subprocess
p2 = subprocess.Popen(,stdout=subprocess.PIPE)
output2 =... (2 Replies)
Hi,
I am trying to run a shell script using subprocess in python.
I can run simple script with arguments using subprocess.But I am not able to embed xterm in subrocess command.
#!/usr/bin/python
import subprocess
subprocess.call()
Above code gives me error.
Please help me in... (2 Replies)
Hi Guys,
Just a question about subprocesses.. Lately one of our servers has started to throw out the following error:
SYSTEM ERROR: Too many subprocesses, cannot fork. Errno=12
We've already increased the threshold twice. Its now up to 8000 and the swap space has also been increased. We... (6 Replies)
Hi All
Here i have a piece of code,
set filename "./GopiRun.sh"
#I need to wait here until the GopiRun.sh is completed how do i achive this
exit. (1 Reply)
can we use pipes to redirect the output of any command to grep .....
like i wanted to write this script about checking the online status of a certain user so ...can i send the output of who to grep directly using pipes...
one way was this :
who > temp
grep $uname temp
i was wondering if... (4 Replies)