Mailinglist Archive: opensuse (818 mails)

< Previous Next >
Re: [opensuse] different types of shell scripts
Hello,

On Fri, 23 Sep 2011, Greg Freemyer wrote:
Suppose I have a "file" (not a script) that has content:

###
TEST_DEV=/dev/sda3
echo $TEST_DEV
###

If I source that (. filename) it will not create a new process. The
shell itself is able to process those commands. (It has a built-in
echo command.)

Correct.

xterm1> cat /tmp/t.sh
TEST_DEV=/dev/sda3
echo $TEST_DEV
/bin/echo $TEST_DEV
xterm1> echo $$
3236
xterm2> LANG=C strace -f -e process -p 3236
xterm1> . /tmp/t.sh
/dev/sda3
/dev/sda3

[meanwhile on xterm2, pruned to the relevant syscalls]
clone(Process 24593 attached
[pid 24593] execve("/bin/echo", ["/bin/echo", "/dev/sda3"], [/* 125 vars */]) = 0
Process 3236 resumed
Process 24593 detached
--- SIGCHLD (Child exited) @ 0 (0) ---

xterm1> /tmp/t.sh
/dev/sda3
/dev/sda3

[meanwhile on xterm2, pruned to the relevant syscalls]
clone(Process 24635 attached
[pid 24635] execve("/tmp/t.sh", ["/tmp/t.sh"], [/* 125 vars */]) = -1 ENOEXEC
(Exec format error)
[pid 24635] execve("/bin/sh", ["/bin/sh", "/tmp/t.sh"], [/* 125 vars */]) = 0
[pid 24635] clone(Process 24636 attached
child_stack=0, flags=CLONE_CHILD_CLEARTID|CLONE_CHILD_SETTID|SIGCHLD,
child_tidptr=0x7f55576939d0) = 24636
[pid 24636] execve("/bin/echo", ["/bin/echo", "/dev/sda3"], [/* 122 vars */]) = 0
Process 24635 resumed
Process 24636 detached
[pid 24635] --- SIGCHLD (Child exited) @ 0 (0) ---
Process 3236 resumed
Process 24635 detached
--- SIGCHLD (Child exited) @ 0 (0) ---

If I execute the above (./filename or just plain filename) then a new
shell child process is invoked to run the commands.

If I modify the file to say:

###
TEST_DEV=/dev/sda3
/bin/echo $TEST_DEV
echo $TEST_DEV
###

Then when I source the file, a child process (/bin/echo) is launched,
but only for that one command, the first and last are still processed
by the original shell.

And if I execute the file I get both a child shell process and a
grandchild /bin/echo process.

Correct. See above. Don't speculate, use the tools ;)

-dnh

--
I find all proselytisation to be misguided at least. The way I see it, it's
not the Enlightened or the Chosen trying to save me or anyone, but just some
meme trying to survive in the great war for brain real estate.
-- Maarten Wiltink
--
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >