Shebang: Difference between revisions

From ICO wiki
Jump to navigationJump to search
No edit summary
No edit summary
Line 30: Line 30:
* SetUID ei puugi olla kasutatav.
* SetUID ei puugi olla kasutatav.


== Ajalugu ==
Shebangi tutvustati esmakordselt Belli laboratooriumis Dennis Ritchie poolt, mis leidis kasutust alates UNIXi versioonide 7 ja 8 vahelisel ajal. Shebang funktsionaalsus lisati ka Berkeley BSD operatsioonisüsteemile, mida tutvustati esmakordselt BSD 2.8 versioonil ning vaikimisi aktiveeriti versioonil BSD 4.2. Kuna AT&T Belli laborid ei avalikustanud enam uuemaid UNIXI versioone kui 8, siis seetõttu oli shebang laialdaselt tuntud BSD operatsioonisüsteemist.
Jaanuaril 1980 tutvustas Dennis Ritchie kerneli toega interpretaatori juhendit UNIXi versioonile 8.
Dennis Ritchie interpretaatori juhend:
<pre>
From uucp Thu Jan 10 01:37:58 1980
>From dmr Thu Jan 10 04:25:49 1980 remote from research
The system has been changed so that if a file being executed
begins with the magic characters #! , the rest of the line is understood
to be the name of an interpreter for the executed file.
Previously (and in fact still) the shell did much of this job;
it automatically executed itself on a text file with executable mode
when the text file's name was typed as a command.
Putting the facility into the system gives the following
benefits.
1) It makes shell scripts more like real executable files,
because they can be the subject of 'exec.'
2) If you do a 'ps' while such a command is running, its real
name appears instead of 'sh'.
Likewise, accounting is done on the basis of the real name.
3) Shell scripts can be set-user-ID.
4) It is simpler to have alternate shells available;
e.g. if you like the Berkeley csh there is no question about
which shell is to interpret a file.
5) It will allow other interpreters to fit in more smoothly.
To take advantage of this wonderful opportunity,
put
#! /bin/sh
at the left margin of the first line of your shell scripts.
Blanks after ! are OK.  Use a complete pathname (no search is done).
At the moment the whole line is restricted to 16 characters but
this limit will be raised.
</pre>
== Autor ==
== Autor ==
Nimi: Tanel Peep
Nimi: Tanel Peep

Revision as of 23:38, 30 November 2017

Shebang on sümbolite jada, mida kasutatakse skriptide alguses ning mis koosneb "trellidest" ehk numbrimärgist ja hüüumärgist (#!). Shebang on tuntud ka nimedega sha-bang, hashbang, pound-bang, ja hash-pling.

Shebang on kasutusel UNIX-tüüpi operatsioonisüsteemide käivitatavates failides. Kui käivitatavas tekstifailis ehk skriptis on kirjeldatud shebang siis kasutatakse shebangi järel kirjeldatud interpretaatorit kogu ülejäänud koodi käivitamiseks. Ehk siis shebang kirjeldab asukoha argumendi, et käivitada skript argumendis kirjeldatud programmis. Näiteks kui skriptis on shebangi järel kirjeldatud /bin/bash, siis programmi laadija kasutab argumenti /bin/bash ja teab, et antud skripti tuleb käivitada bash programmis, mis asub kataloogis /bin.

Üldjuhul ignoreerib interpretaator shebangi, sest numbrimärge(#) on enamus programmeerimis- ja skriptimiskeeltes tähistatud kui kommentaari. Samuti mitmed programmeerimiskeeled, mis ei kasuta numbrimärki kommentaarina, ignoreerivad numbrimärki ja saavad aru shebang funktsioonist.

Kui skriptifailides on shebang kirjeldamata siis üldjuhul kasutatakse interpretaatorina /bin/sh.

Süntaks

Shebang interpretaatori vorm on järgnev:

#!intepretaator [valikulised-argument]

Kus interpretaator tähistab täispikka kataloogi teekonda käivitatavale programmile. Valikuline-argument tähistab käivitatavale programmile kirjeldatud argumenti.

Näited

Mõned shebang näited:

  • #!/bin/sh - Faili käivitamine Bourne shelliga.
  • #!/bin/bash - Faili käivitamine Bash shelliga.
  • #!/usr/bin/perl -T - Faili käivitamine Perl'ga koos "tainted" argumendiga.
  • #!/usr/bin/env python - Faili käivitamine Python'ga, kasutades env'd.

Näidetes on näha, et shebang rida võib sisaldada argumente, mis edastatakse interpretaatorile

Omadused

"#!" võimaldab skripte kasutada nagu tavalisi käivitatavaid binaarfaile.

Mõned shebang omadused:

  • Interpretaatori nimi ei tohi olla tühi.
  • Shebang rea pikkus on väiksem kui maksimaalse faili teekonna pikkus.
  • $PATH keskkonnamuutujast ei otsita interpretaatorit.
  • Interpretaatorit saab kasutada nii täispika kui ka loogise teekonnana. (Loogiline teekond ei ole soovitatav)
  • Interpretaatorina ei tohiks kasutada skripte.
  • SetUID ei puugi olla kasutatav.

Ajalugu

Shebangi tutvustati esmakordselt Belli laboratooriumis Dennis Ritchie poolt, mis leidis kasutust alates UNIXi versioonide 7 ja 8 vahelisel ajal. Shebang funktsionaalsus lisati ka Berkeley BSD operatsioonisüsteemile, mida tutvustati esmakordselt BSD 2.8 versioonil ning vaikimisi aktiveeriti versioonil BSD 4.2. Kuna AT&T Belli laborid ei avalikustanud enam uuemaid UNIXI versioone kui 8, siis seetõttu oli shebang laialdaselt tuntud BSD operatsioonisüsteemist.

Jaanuaril 1980 tutvustas Dennis Ritchie kerneli toega interpretaatori juhendit UNIXi versioonile 8.

Dennis Ritchie interpretaatori juhend:

From uucp Thu Jan 10 01:37:58 1980
>From dmr Thu Jan 10 04:25:49 1980 remote from research
The system has been changed so that if a file being executed
begins with the magic characters #! , the rest of the line is understood
to be the name of an interpreter for the executed file.
Previously (and in fact still) the shell did much of this job;
it automatically executed itself on a text file with executable mode
when the text file's name was typed as a command.
Putting the facility into the system gives the following
benefits.

1) It makes shell scripts more like real executable files,
because they can be the subject of 'exec.'

2) If you do a 'ps' while such a command is running, its real
name appears instead of 'sh'.
Likewise, accounting is done on the basis of the real name.

3) Shell scripts can be set-user-ID.

4) It is simpler to have alternate shells available;
e.g. if you like the Berkeley csh there is no question about
which shell is to interpret a file.

5) It will allow other interpreters to fit in more smoothly.

To take advantage of this wonderful opportunity,
put

	#! /bin/sh

at the left margin of the first line of your shell scripts.
Blanks after ! are OK.  Use a complete pathname (no search is done).
At the moment the whole line is restricted to 16 characters but
this limit will be raised.

Autor

Nimi: Tanel Peep

Rühm: AK21

Aasta: 2017

Allikad