Find the answer to your Linux question:
Results 1 to 7 of 7
When the shell creates a new process to execute a command, how does it know that the file is executable? If it is executable, how does it distinguish between a ...
  1. #1
    Just Joined!
    Join Date
    Aug 2008
    Posts
    49

    shell script v/s executable

    When the shell creates a new process to execute a command, how does it know that the file is executable?
    If it is executable, how does it distinguish between a shell script and a file produced by a compilation?
    what is the correct sequence for checking the above cases?

  2. #2
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    A file is executable if its executable flag is set.

    As far as I know, the shell calls exec or another function from the same family. The kernel then checks the header of the file (first 128 bytes) to see if it is a registered binary format (binfmt). If so, it invokes the necessary program to execute the file (e.g. virtual machine or emulator), or process it (linker). The header of the file may itself contain the name of the necessary program (#! notation).

  3. #3
    Just Joined!
    Join Date
    Aug 2008
    Posts
    49
    ya fine, if executable flag is set, we can execute.

    But basically my question is if i have written some file called "abc". contents are

    ls -l | wc -l

    Here i am not specifying anything like #!/bin/sh in the header. How it automatically recognize its a shell script file not executable.

    cc -o abc file.c

    Here i can understand that, "abc" is in ELF format. but in previous condition i didn't get?

  4. #4
    Linux Guru techieMoe's Avatar
    Join Date
    Aug 2004
    Location
    Texas
    Posts
    9,496
    Quote Originally Posted by dayananda.ms View Post
    ya fine, if executable flag is set, we can execute.

    But basically my question is if i have written some file called "abc". contents are

    ls -l | wc -l

    Here i am not specifying anything like #!/bin/sh in the header. How it automatically recognize its a shell script file not executable.
    Once again, the way the OS determines if something is executable is with the executable permissions flag. End of story. It doesn't matter if the file contains nothing but SPAMSPAMSPAMSPAMSPAM over and over, it will still try to execute it.
    Registered Linux user #270181
    TechieMoe's Tech Rants

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    Quote Originally Posted by dayananda.ms View Post
    But basically my question is if i have written some file called "abc". contents are

    ls -l | wc -l

    Here i am not specifying anything like #!/bin/sh in the header. How it automatically recognize its a shell script file not executable.
    I think that this might be a different question.

    Let's assume that I have this "abc" file with the contents that you mention. If I now run the command "./abc", I will get an error that the script is not executable.

    However, if I run the command "bash abc", it does work. Why is this? Because bash is an executable program, and we have given it a file to use as its input. Because we are not executing the "script" directly (but rather passing it as input to an interpreter), it appears that the script executes.

    Does this help?
    DISTRO=Arch
    Registered Linux User #388732

  6. #6
    Just Joined!
    Join Date
    Aug 2008
    Posts
    49
    Ya i understood, but to read executable header i can use "readelf". But to read shell script(execution flag set) header, what command to use?

    Somebody( burschik) said it will recognize through header?

  7. #7
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Yes, the kernel recognizes the binfmt by reading the header. I don't know whether there is a command line program for reading the headers of (potentially) executable files, but you can look at the kernel code, here, for example:

    LXR linux/fs/binfmt_script.c

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...