subshell & background function


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers subshell & background function
# 1  
Old 03-12-2003
subshell & background function

Hello all,

Can someone explain to me the advantage between using subshell over a function call in scripts? To me these are the same. Am I wrong to think this?
# 2  
Old 03-12-2003
When you place a block of commands in between parenthesis instead of curly brackets, a subshell is created to run the commands instead of just running them in the current shell.

The subshell command sequence is done in a subprocess. This means that changing a process-specific characteristic (for example, the current directory) won't affect the current process after the subshell is finished.

However, if you change a process-specific characteristic, such as the current directory, inside a function, then when the function is finished, the current directory remains changed. That could be a good thing or a bad thing, depending on what you want.
# 3  
Old 03-12-2003
The above comments are very important to understand, but a subshell will actually spawn a new shell process. For example, time the two following scripts to see which is faster:
#1
Code:
#! /bin/ksh
n=0
until (( n == 1000 )); do
     print Huh? >/dev/null
     ((n=n+1))
done

#2
Code:
#! /bin/ksh
n=0
until (( n == 1000 )); do
     ( print Huh? >/dev/null ; )
     ((n=n+1))
done

I should mention that I cannot test this at the moment, so I might be giving a bad example...
# 4  
Old 03-12-2003
They were both too fast to time in seconds, but the first script definately executed faster than the second script.
# 5  
Old 03-13-2003
Thank you oombera and LivinFree for your reply . I think I got it now.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass return value of a function in background process

Hi, I have created a function f1 defined in script A.sh .I have called this function in background . But I want to use its return value for another function f2 in script A.sh. I tried declaring it as a global variable, yet it always returns the status as 0. Is there any way with which I can get... (7 Replies)
Discussion started by: ashima jain
7 Replies

2. UNIX for Dummies Questions & Answers

Implement the '&&' function in a shell

Hello, I'm currently implementing the && function in a shell using C. For example, if we input cmd1 && cmd2, then cmd2 executes only when cmd1 exits successfully. I'm thinking about: int main() { int i; char **args; while(1) { printf("yongfeng's shell:~$ "); args =... (5 Replies)
Discussion started by: Yongfeng
5 Replies

3. Shell Programming and Scripting

While loop hangs in function running in background

Hello Everyone, I am writing a shell script to fetch log files from remote servers within a time range. It copies log files to local server, grep for date and then compares the time stamp of each log entry with the once specified. Below is the code. # Get log and Parsing function ... (1 Reply)
Discussion started by: kanagalamurali
1 Replies

4. Shell Programming and Scripting

Execute a function in background and then suspend it

Here is some back ground on the script. The script is to poll an arbitrary number of DB's. To do this I am creating a function that takes the file_path to the DB and the min poll interval as arguments. The function will be called for each DB and then ran in the background. The function I was... (6 Replies)
Discussion started by: ryandavison
6 Replies

5. Shell Programming and Scripting

A puzzle with a printing function executing in background

Somebody on a thread in the (french) Mandriva Forum recently suggested a script, designed to provide a tool to display kind of "temporisation widgets" on the console (to be ultimately pasted in other more complex scripts). One version of this script was something like the following, which seems... (6 Replies)
Discussion started by: klease
6 Replies

6. AIX

Background & is considered as Idle

Our Aix Unix has one issue. If I type xedit & after 30 minutes, xedit auto shut down. If I type xedit xedit will run forward. I feel this & doesn't perform as it should be. When I use &, system consider this process as idle. How to fix this issue? Does this mean unix env... (12 Replies)
Discussion started by: david_hu_66
12 Replies

7. Shell Programming and Scripting

A question about the PID of a background function

Dear all, I'm writing a KornShell script that calls inside it a function in background mode #!/bin/ksh function myfunction { . . .} myfunction |& . . . How can I capture the PID of the function myfunction that runs in background? Thanks in advance :) (2 Replies)
Discussion started by: dariyoosh
2 Replies

8. UNIX for Dummies Questions & Answers

alias for running in background &

Hi all, I am kinda new to this unix environment, and now I get confused to figure out this alias problem. I would like to make my xemacs to run in background every call, and I am trying to do it this way in .bashrc alias e='xemacs &* & ' i also tried e () { xemacs "&*" &} but they are... (0 Replies)
Discussion started by: hfireflyu
0 Replies

9. Programming

want to run a function in background

consider the given prg. main() { ..... function1(); /* to write into a file or log */ printf(" "); ..... } when the control reaches function1(), it should get executed in the background.At the same time main's printf(" ") statement should also get executed.i.e... (5 Replies)
Discussion started by: bankpro
5 Replies
Login or Register to Ask a Question