No #! Bash Script Execution

Source: Internet
Author: User
Tags printable characters

Some bash scripts are not properly written. They are not written at the beginning of the file #!, But it can be executed directly. However, if you look at the kernel code, the start of the shell script's loading function will be determined. If not #! Then an error is returned:
Static int load_script (struct linux_binprm * bprm, struct pt_regs * regs)
{
...
If (bprm-> Buf [0]! = '#') | (Bprm-> Buf [1]! = '! ') | (Bprm-> sh_bang ))
Return-enoexec;
...
}
Why don't I start the file when I write a bash script in Linux #! Can also be called successfully? The shell_execve function is called when executing a Keyboard Command in Bash. This function encapsulates many Logics:
Int shell_execve (command, argS, ENV)
{
Execve (command, argS, ENV );
If (errno! = Enoexec ){
// Non-File Format error. An error is returned.
} Else {
Int larray = array_len (ARGs) + 1;
Int I, should_exec = 0;
Int FD = open (command, o_rdonly );
If (FD! =-1 ){
Unsigned char sample [80]; // read the 80 bytes of the file header, and then determine the format
Int sample_len = read (FD, & sample [0], 80 );
... // If it is a null file, it will be returned correctly. Bash will tolerate this situation.
If (sample_len> 0 & sample [0] = '#' & sample [1] = '! ')
Return (execute_shell_script (...); // execute the shell script. The implementation process is almost the same as in the script_format structure object of the kernel.

Load_binary callback functions are identical, mainly used for non-recognition #! Operating System
Else if (check_binary_file (sample, sample_len ))
// If it is a binary file, Bash will not execute the command line by line and return an error
}
... // Bash will help you execute command:
/*
ARGs [0] = shell_name; // It is the full path of the shell.
ARGs [1] = command; // script to be executed
Execve (shell_name, argS, ENV); // re-Execute
*/
}
The following are the help functions and macro definitions on which the shell_execve function depends:
Bash determines whether it is a binary file based on the Character visibility. Because spaces, tabs, and other invisible characters, they do belong to the text ASCII code, therefore, the filter must be performed separately:
# Define isspace (C) (c) = ''| (c) = '/T' | (c) = '/N' | (c) ='/F ')
Text characters include letters, numbers, and other printable characters:
# Define isprint (C) (isletter (c) | digit (c) | ispunct (c ))
Judge whether all the 80 bytes read are text characters. If one is not, the file is regarded as a binary file, although the first 80 bytes of characters are text and cannot be guaranteed to be followed by text, Bash must make a trade-off and a hypothesis after all:
Int check_binary_file (sample, sample_len)
{
For (I = 0; I <sample_len; I ++ ){
If (sample [I] = '/N ')
Break;
If (! Isspace (sample [I]) &! Isprint (sample [I])
Return (1 );
}
Return (0 );
}
Therefore, if you execute an empty file, nothing will be output. If you execute a non-Linux executable binary file, such as a CER binary certificate, then the kernel will return enoexec, next Bash will try to execute it, and finally return an ex_binary_file error, print "cannot execute binary file", if the execution of a file header is not #! The kernel returns an enoexec error, but Bash can be successfully executed directly in subsequent shell_execve execution, however, if you add 80 or more bytes of text to the header of a binary file, Bash will execute it as a parent branch based on the shell_execve logic, and then it will make a big mistake, so sometimes the script is not written #! It can also be executed directly, but you need to know that Bash itself helps you execute it, instead of directly executing the kernel. At least the execution thread will first return an error from the kernel, and then continue the part following shell_execve, it does slightly affect efficiency, so it is best to write the standard #!, In addition, some shells do not intelligently re-interpret and execute commands like bash.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.