Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird Script Sourcing

Status
Not open for further replies.

SamBones

Programmer
Aug 8, 2002
3,186
US
I have a user that was having problems with a script. While looking at her script, I saw something odd that seems to work, but I'm not sure what it's doing.

From within one script, she's sourcing another using a dot. This is normal usage. What's odd is that she's put an ampersand at the end of the line to run it in the background. Sourcing is to run the commands in the called script in the same process that's calling it, but the ampersand is to force it into a background process.

I know it's a misuse of sourcing, but it seems to work. I told her to change the dot to "nohup", and that seemed to break it. This was due to other issues in the script, but I'm still curious about what exactly was happening process-wise.

Here's an example of what she's doing...
Code:
#!/bin/ksh

. myscript.sh > myscript.log 2>&1 &
Any thoughts?


 
Interesting! It seems to defeat the purpose of sourcing myscript.sh because (after examining the behaviour of ksh under HP-UX with the truss system-call trace tool), it seems to cause a fork() to handle the background processing, so any variables that are set will be lost as per any normal background or child process.

It could have a use though as a slightly more efficient way to run some commands in the background without exec()ing a new shell? That's what happens if you simply ./myscript.sh &.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top