• The exeve system call and the exec family of functions (was: Try to und

    From Helmut Waitzmann@21:1/5 to All on Tue Dec 14 22:47:55 2021
    "hongy...@gmail.com" <hongyi.zhao@gmail.com>:

    And strace can't trace the system calls and signals of this script:

    $ strace ./test.sh
    execve("./test.sh", ["./test.sh"], 0x7ffc7d8136c0 /* 102 vars */) = -1 ENOEXEC (Exec format error)
    strace: exec: Exec format error
    +++ exited with 1 +++

    An interpreter (here: shell) script is not a runable program
    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)