[opensuse] Re: umm BASH gurus, what does the typo $!/bin/bash do?
David C. Rankin wrote:
On Friday 02 October 2009 03:26:05 am Brian K. White wrote:
So, you'd think the script exited, but no you were still in the script, still busy executing the first line, /bin/bash, until you typed exit or hit ctrl-d, (/bin/bash finishes), at that point the rest of the script would run.
Yah, but you out to have seen all the output when I typed
strace ./scriptname
Then the $!/bin/bash was not in the first line: puma:tmp $ strace ./try.sh execve("./try.sh", ["./try.sh"], [/* 145 vars */]) = -1 ENOEXEC (Exec format error) dup(2) = 3 fcntl64(3, F_GETFL) = 0x8002 (flags O_RDWR|O_LARGEFILE) fstat64(3, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 8), ...}) = 0 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f6d000 _llseek(3, 0, 0xbff8b20c, SEEK_CUR) = -1 ESPIPE (Illegal seek) write(3, "strace: exec: Exec format error\n", 32strace: exec: Exec format error) = 32 close(3) = 0 munmap(0xb7f6d000, 4096) = 0 exit_group(1) = ? As I wrote, such a file only works when called by bash directly, not via exec(), which is used by strace. Joachim -- =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Joachim Schrod Email: jschrod@acm.org Roedermark, Germany -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
Hello, On Fri, 02 Oct 2009, Joachim Schrod wrote:
Then the $!/bin/bash was not in the first line:
puma:tmp $ strace ./try.sh execve("./try.sh", ["./try.sh"], [/* 145 vars */]) = -1 ENOEXEC (Exec format error) [..] As I wrote, such a file only works when called by bash directly, not via exec(), which is used by strace.
But without strace, the kernel feeds the file to the user's shell. $ cat t.sh $!/bin/bash echo Hello $ strace bash -c './t.sh' ## shortened output, my comments after '###' ### start the 'bash -c' process execve("/bin/bash", ["bash", "-c", "./t.sh"], [/* 103 vars */]) = 0 [..] ### try exec t.sh execve("./t.sh", ["./t.sh"], [/* 100 vars */]) = -1 ENOEXEC (Exec format error) ### as commented open("./t.sh", O_RDONLY|O_LARGEFILE) = 3 read(3, "$!/bin/bash\necho Hello\n", 80) = 23 close(3) [..] open("./t.sh", O_RDONLY|O_LARGEFILE) = 3 [..] read(3, "$!/bin/bash\necho Hello\n", 23) = 23 ### now the script is fed to the user's default-shell and the first ### line is executed (i.e. start the /bin/bash after the $!): fork() = 7811 ### within the 'wait' the interactive /bin/bash started from script ### is running, I exit by pressing Ctrl-d: wait4(-1, dh@slarty[11]: (0)$ exit WIFEXITED(s) && WEXITSTATUS(s) == 0], 0, NULL) = 7811 [..] --- SIGCHLD (Child exited) --- wait4(-1, 0xbfffe348, WNOHANG, NULL) = -1 ECHILD (No child processes) ### we're back to the t.sh script [..] read(3, "echo Hello\n", 23) = 11 write(1, "Hello\n", 6Hello ) read(3, "", 23) = 0 munmap(0x40014000, 4096) = 0 _exit(0) = ? Using the extra bash -c simulates the behaviour you'd get without strace. Any questions? ;) -dnh -- If you think, you're wrong, you might be right! -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org
participants (2)
-
David Haller
-
Joachim Schrod