On Wednesday, December 15, 2021 at 5:49:14 AM UTC+8, Helmut Waitzmann wrote:
"hongy...@gmail.com" <hongy...@gmail.com>:
And strace can't trace the system calls and signals of this script:
$ strace ./test.shAn interpreter (here: shell) script is not a runable program
execve("./test.sh", ["./test.sh"], 0x7ffc7d8136c0 /* 102 vars */) = -1 ENOEXEC (Exec format error)
strace: exec: Exec format error
+++ exited with 1 +++
image. That's why the system call execve, that is told to execute
that script, returns an error.
See the execve(2) and errno(3) manual pages and the POSIX error
numbers (<https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_03>)
for what the ENOEXEC error is.
Give that script a Shebang in the first line of it, then make it executable:
chmod -- u+x ./test.sh
Try again tracing it. Does ist work now?I've granted the execute permission to the script:
$ ls -la test.sh
-rwxrw-r-- 1 werner werner 29 Dec 15 09:02 test.sh
$ strace ./test.sh
execve("./test.sh", ["./test.sh"], 0x7ffc06199f90 /* 103 vars */) = -1 ENOEXEC (Exec format error)
strace: exec: Exec format error
+++ exited with 1 +++
"hongy...@gmail.com" <hongy...@gmail.com>:
And strace can't trace the system calls and signals of this script:
$ strace ./test.shAn interpreter (here: shell) script is not a runable program
execve("./test.sh", ["./test.sh"], 0x7ffc7d8136c0 /* 102 vars */) = -1 ENOEXEC (Exec format error)
strace: exec: Exec format error
+++ exited with 1 +++
image. That's why the system call execve, that is told to execute
that script, returns an error.
See the execve(2) and errno(3) manual pages and the POSIX error
numbers (<https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_03>)
for what the ENOEXEC error is.
Give that script a Shebang in the first line of it, then make it
executable:
chmod -- u+x ./test.sh
Try again tracing it. Does ist work now?
Let me append my ceterum censeo:
You really should get a book about the unix kernel, not just about
the shell. Learn the purpose and behavior of the system calls – in
this case: the execve system call and the various wrappers for that
system call in the C language runtime library: the exec family of
functions.
Understand, what the wrapper function execlp does, when given a file
with read and execute permissions, starting with a Shebang line:
It invokes the named interpreter, giving it the pathname of the
given (script) file as a parameter.
Then, get a book about the shell (or just read the shell manual page
and the POSIX specification <https://pubs.opengroup.org/onlinepubs/9699919799/utilities/toc.html>),
to know, what the shell does when reading and executing a shell
script, especially: Understand, how the shell transforms a shell
command line invoking a simple command into the corresponding execve argument vector.
Finally, understand, why the seen "strace" behavior above follows
from the lessions learned.
As long as you don't understand the unix system calls provided by
the kernel, you'll neither understand the strace error message above
nor make progress in shell programming.
$ strace ./test.sh
execve("./test.sh", ["./test.sh"], 0x7ffc06199f90 /* 103 vars */) = -1 ENOEXEC (Exec format error)
strace: exec: Exec format error
+++ exited with 1 +++
Additional information:
$ cat -A test.sh
$
#!/bin/bash$
echo 'Hello.'$
On Tue, 14 Dec 2021 20:12:40 -0500, hongy...@gmail.com <hongy...@gmail.com> wrote:
$ strace ./test.sh
execve("./test.sh", ["./test.sh"], 0x7ffc06199f90 /* 103 vars */) = -1 ENOEXEC (Exec format error)
strace: exec: Exec format error
+++ exited with 1 +++
Additional information:
$ cat -A test.shStrange.
$
#!/bin/bash$
echo 'Hello.'$
[tester@x3 ~]$ cat -A bin/test
#!/bin/bash$
echo 'Hello.'$
[tester@x3 ~]$ bin/test
Hello.
[tester@x3 ~]$ strace bin/test
execve("bin/test", ["bin/test"], 0x7fff84f3fc70 /* 53 vars */) = 0
brk(NULL) = 0xaf8000
access("/etc/ld.so.preload", R_OK) = 0
openat(AT_FDCWD, "/etc/ld.so.preload", O_RDONLY|O_CLOEXEC) = 3
<snip rest>
[tester@x3 ~]$ bash --version
GNU bash, version 5.1.4(1)-release (x86_64-mageia-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Regards, Dave Hodgins
On Tue, 14 Dec 2021 23:39:37 -0500, hongy...@gmail.com <hongyi.zhao@gmail.com> wrote:
On Wednesday, December 15, 2021 at 10:40:51 AM UTC+8, David W. Hodgins wrote:work smoothly:
On Tue, 14 Dec 2021 20:12:40 -0500, hongy...@gmail.com <hongy...@gmail.com> wrote:
Strange.$ strace ./test.sh
execve("./test.sh", ["./test.sh"], 0x7ffc06199f90 /* 103 vars */) = -1 >ENOEXEC (Exec format error)
strace: exec: Exec format error
+++ exited with 1 +++
Additional information:
$ cat -A test.sh
$
#!/bin/bash$
echo 'Hello.'$
The culprit is the first empty line in the script. If I remove it, strace will
Ah. Yes. I didn't notice the leading blank line. The shebang must
be the first characters in the file.
#! = interpreted script
.ELF = Executable and Linking Format files
MZ = dos executable
etc
Regards, Dave Hodgins
On Wednesday, December 15, 2021 at 10:40:51 AM UTC+8, David W. Hodgins wrote:
On Tue, 14 Dec 2021 20:12:40 -0500, hongy...@gmail.com <hongy...@gmail.com> wrote:
Strange.$ strace ./test.sh
execve("./test.sh", ["./test.sh"], 0x7ffc06199f90 /* 103 vars */) = -1 ENOEXEC (Exec format error)
strace: exec: Exec format error
+++ exited with 1 +++
Additional information:
$ cat -A test.sh
$
#!/bin/bash$
echo 'Hello.'$
The culprit is the first empty line in the script. If I remove it, strace will work smoothly:
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 296 |
Nodes: | 16 (2 / 14) |
Uptime: | 25:38:06 |
Calls: | 6,646 |
Calls today: | 1 |
Files: | 12,193 |
Messages: | 5,327,801 |