How to find out which script title to use and why it is important?

I rarely have to write bash scripts for various unrelated purposes, and although I usually understand well what commands I want in a script, I often don’t know which header to use or why I use one when I find it. For example (s):

  • Standard shell script:
    • #!/bin/bash
  • Python:
    • #!/usr/bin/env python

The scripts seem to work fine without headers, but if the headers are standard, there is a reason for them and should not be ignored. If this has an effect, then it is a valuable tool that you can use to achieve more.

To a minimum, I would like to know which headers to use with MySQL scripts and what headers do with standard scripts, Python and MySQL. Ideally, I need a general list of headers or an understanding of how to create a heading based on which program is used.

+3
source share
2 answers

How the kernel does things

Simplified (bit), there are two ways that the kernel in a POSIX system knows how to execute a program. One, if the program is in binary format, which the kernel understands (for example, ELF ), the kernel can execute it "directly" (in more detail from the field). If the program is a text file starting with shebang, for example

#!/usr/bin/somebinary -arg

what-have-you, ​​ , ​​ :

/usr/bin/somebinary -arg "$0"

$0 script, . (, , # - , shebang .)

PATH env

PATH, , , , python script , python, ,

#!/usr/bin/python

env, , POSIX, , python PATH. ,

#!/usr/bin/env python

script , PATH.

BASH, SH Invocation

, . , /bin/sh , ​​ /bin/bash. bash POSIXLY_STRICT sh, /bin/sh, , plain-old-bash.

MySQL arg

shebang , , mysql - , mysql script.

#!/usr/bin/env mysql
use mydb;
select * from mytbl;

, ​​ mysql "$0". .my.cnf, mysql "$ 0" . :

#!/usr/bin/mysql -e
use mydb;
select * from mytbl;

, "$ 0" ( ).

, mysql script . sql mysql:

mysql < my_sql_commands
+7

http://mywiki.wooledge.org/BashGuide/Practices#Choose_Your_Shell

script #!, , "shebang". script , , , script - , script.

, " " - shebang, execve(), , (?) script. shebang, .

( : - script, sh yourscript bash yourscript, shebang script, , , , , ).

:

  • , , bash , #!/usr/bin/env bash
  • , , #!/bin/bash
  • script, POSIX sh, #!/bin/sh

shebang, , ( script) script , , shebang. myscript #!/usr/bin/env yourprogram, ./myscript foo bar, /usr/bin/env yourprogram myscript foo bar; yourprogram /usr/bin/env ( PATH) , myscript .

shebang , , , . http://www.in-ulm.de/~mascheck/various/shebang/

+4

All Articles