LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-23-2013, 08:33 PM   #1
Longeron
LQ Newbie
 
Registered: Jan 2013
Posts: 5

Rep: Reputation: Disabled
[Bash script] Executing commands in the current shell, while not launching subshells


Hello,

I'm trying to learn to write bash scripts -- and one of the problems I've been looking at is writing commands like

"ps –Af | grep foo"

without launching subshells. i.e. the commands execute in the current shell only, without launching subshells.

How can this be done?
 
Old 01-23-2013, 09:26 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Execution of a script depends upon it's shebang, that is, whatever you define at the beginning of your script.
Code:
#!/bin/bash
If you are stuck somewhere, then invoke the script in debug mode to check for any error(s), like:-
Code:
#!/bin/bash
set -xv     # Add this. It will debug every line of your script
However, share part of your script, where you're facing problem.
 
Old 01-23-2013, 09:32 PM   #3
Longeron
LQ Newbie
 
Registered: Jan 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
It is #!/bin/bash

But my question is not about a specific script, but all scripts where you would want to execute all commands in the current shell (without launching subshells, even background subshells.)

There's no error, I'm just asking if it is possible to do this.

Last edited by Longeron; 01-23-2013 at 09:41 PM.
 
Old 01-23-2013, 10:18 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
The example you gave,
Code:
ps –Af | grep foo
does not use any subshells. Perhaps you are misusing the terminology. The ps and grep commands are separate executables, not part of the shell, and will be executed as separate processes that are children of the current shell. If it's the use of child processes that you want to avoid entirely, you are restricted to the commands listed in the SHELL BUILTIN COMMANDS section of the bash manual page, and even then, use of pipes and certain I/O redirections will result in some of those commands being run in a subshell.

It's actually possible to do quite a bit with nothing more than builtin shell commands and constructs (BTDT, rescuing a screwed up system using nothing more than a stand-alone shell and no external commands), but you have to forget about using anything not included in your shell's manpage.
 
1 members found this post helpful.
Old 01-23-2013, 11:06 PM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Generally commands are not shell specific but OS specific. Although different shells use different ways of handling variables.
As I already said, everything inside your script will follow your shell interpreter i.e. shabang.

If you're invoking any command inside a script, certainly you need not to go into any sub-shell.

Anyway, specify any sample script or situation (if any) where you needed this, so people can suggest accurate answers.
 
Old 01-23-2013, 11:09 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
rknichols explains the general answer (processes vs sub-shells) nicely. As shivaa, said, if you are having specific problems, please show us the details.
 
Old 01-24-2013, 02:21 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
If you mean wanting the output of the commands piped through the shell without executing a subshell to control it like in <() or $(), you need to use exec:
Code:
OUTPUT=$(exec ps –Af | grep foo)
read OUTPUT < <(exec ps –Af | grep foo)
Edit: Actually it does executes a subshell but it's "exec()"-ed or replaced with the process of ps.

(Thinking when those styles of scripting would reach online tutorials again claiming it's their own ideas made standard.)

Last edited by konsolebox; 01-24-2013 at 02:28 AM.
 
Old 01-27-2013, 08:26 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
What I really want to know is why you're worrying about this in the first place. Chaining commands together with pipes like that is the usual, even recommended, way to connect them together.

The only time a subshell creates any problem at all is when one of the subsequent commands sets a variable/environmental value that needs to be used later in the script, and there are several easy ways to work around that.

I set variables in a loop that's in a pipeline. Why do they disappear after the loop terminates? Or, why can't I pipe data to read?
http://mywiki.wooledge.org/BashFAQ/024
 
Old 01-27-2013, 09:53 AM   #9
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
Quote:
Originally Posted by rknichols View Post
The example you gave,
Code:
ps ľAf | grep foo
does not use any subshells. Perhaps you are misusing the terminology. The ps and grep commands are separate executables, not part of the shell, and will be executed as separate processes that are children of the current shell. If it's the use of child processes that you want to avoid entirely, you are restricted to the commands listed in the SHELL BUILTIN COMMANDS section of the bash manual page, and even then, use of pipes and certain I/O redirections will result in some of those commands being run in a subshell.
In reality, it does use a subshell. First the command is parsed, and pipe is setup; then a subshell is created (via fork) which inherits the stdin/stdout, sets up the parameter list and then execs the command. It does this from right to left.
 
Old 01-27-2013, 01:29 PM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 2,959

Rep: Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268Reputation: 1268
True, but since fork() ... exec() is the only way to spawn a new process, and since that fork()ed copy of the parent process doesn't go through any of a shell's startup and initialization (linking run-time libraries, processing /etc/bashrc, etc.), that hardly counts as "launching a subshell".

Last edited by rknichols; 01-27-2013 at 01:40 PM.
 
Old 01-27-2013, 08:55 PM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by David the H. View Post
What I really want to know is why you're worrying about this in the first place. Chaining commands together with pipes like that is the usual, even recommended, way to connect them together.

The only time a subshell creates any problem at all is when one of the subsequent commands sets a variable/environmental value that needs to be used later in the script, and there are several easy ways to work around that.
You might find it worth noting as well that there are cases where a process needs to be directly manipulated like through signals. And sometimes a signal from a subchild doesn't reach its grandparent and vice versa through the middle process so sometimes it's helpful to have nothing in between. You can't also know the subprocess' PID directly with "$!" and either use an external command or walk through the maps in /proc. Sending message through another pipe to the parent or using a shared file or named pipe to get the process' ID or other means of IPC is not a good idea I think. If the subshell is used to prepare execution of another process it would be easier for subsequent commands to just assume that the subshell is the command to be executed itself. It actually gets more difficult if the subshell is to run asynchronously since you don't know when the command would run in the subshell.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] executing shell commands in makefiles entz Programming 1 03-24-2012 12:15 AM
Linux bash I am writing shell scrip samuel.m012 Linux - Newbie 4 05-28-2010 09:39 AM
GUI for executing shell commands shishirkotkar Linux - Newbie 1 04-12-2008 10:36 AM
Executing shell commands through PHP!!! Richtown Linux - Newbie 6 04-12-2006 03:57 PM
executing shell commands in c++ true_atlantis Programming 2 10-01-2004 05:53 PM


All times are GMT -5. The time now is 09:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration