쉘 스크립트 헤더 (#! / bin / sh 대 #! / bin / csh)
모든 스크립트 파일이
#!/bin/sh
또는
#!/bin/csh
그게 필요합니까? 이것의 목적은 무엇입니까? 그리고 둘의 차이점은 무엇입니까?
이것은 다음과 같이 알려져 있습니다 Shebang.
http://en.wikipedia.org/wiki/Shebang_(Unix)
#! interpreter [optional-arg]
shebang은 스크립트에 실행 권한이있는 경우에만 관련됩니다 (예 : chmod u + x script.sh).
쉘이 스크립트를 실행할 때 지정된 인터프리터를 사용합니다.
예:
#!/bin/bash
# file: foo.sh
echo 1
$ chmod u+x foo.sh
$ ./foo.sh
1
이 #!행은 커널 (특히 execve시스템 호출 의 구현 )에이 프로그램이 해석 된 언어로 작성되었음을 알려줍니다 . 다음에 오는 절대 경로 이름은 인터프리터를 식별합니다. 기계어 코드로 컴파일 된 프로그램은 대부분의 최신 Unix에서 7f 45 4c 46( ^?ELF) 다른 바이트 시퀀스로 시작하여 이를 식별합니다.
프로그램 자체가 스크립트 가 아닌 한 , 뒤에 원하는 프로그램 에 대한 절대 경로를 넣을 수 있습니다 . 커널은 다음 호출을 다시 작성합니다.#!#!
./script arg1 arg2 arg3 ...
여기서 ./script,로 시작 말, #! /usr/bin/perl명령 줄은 실제로 것처럼
/usr/bin/perl ./script arg1 arg2 arg3
또는 #! /bin/sh이미 살펴본 것처럼를 사용하여에서 해석 할 스크립트를 작성할 수 있습니다 sh.
이 #!줄은 명령 줄 에서 스크립트 를 직접 호출하는 경우에만 처리됩니다 ./script. 파일도 실행 가능해야합니다 ( chmod +x script). 그렇게 sh ./script하면 #!라인이 필요하지 않으며 (존재하는 경우 무시 됨) 파일이 실행 가능하지 않아도됩니다. 이 기능 의 요점 은 어떤 언어로 작성되었는지 알지 않고도 통역 언어 프로그램을 직접 호출 할 수 있도록하는 것입니다. (Do- grep '^#!' /usr/bin/*실제로이 기능을 사용하는 많은 스톡 프로그램이 있다는 것을 알게 될 것입니다.)
이 기능을 사용하기위한 몇 가지 규칙은 다음과 같습니다.
- 은
#!맨 처음이해야 바이트 파일입니다. 특히 파일 은 ASCII 호환 인코딩 (예 : UTF-8은 작동하지만 UTF-16은 작동하지 않음) 이어야 하며 "바이트 순서 표시"로 시작 해서는 안됩니다. 그렇지 않으면 커널이이를#!스크립트. - 뒤의 경로
#!는 절대 경로 여야합니다 (으로 시작/). 공백, 탭 또는 개행 문자를 포함 할 수 없습니다. #!와 사이에 공백을 두는 것은 좋은 스타일이지만 필수는 아닙니다/. 여기에 두 개 이상의 공간을 두지 마십시오.#!줄 에 셸 변수를 넣을 수 없으며 확장되지 않습니다.- 하나의 공백으로 구분하여 절대 경로 뒤에 하나의 명령 줄 인수를 넣을 수 있습니다 . 절대 경로와 마찬가지로이 인수는 공백, 탭 또는 개행 문자를 포함 할 수 없습니다. 때로는 작업을 수행하는 데 필요하고 (
#! /usr/bin/awk -f) 때로는 유용합니다 (#! /usr/bin/perl -Tw). 불행하게도, 당신은 넣을 수없는 두 절대 경로 후에 이상의 인수를. - 어떤 사람들은
#! /usr/bin/env interpreter대신 을 사용하라고 말할 것입니다#! /absolute/path/to/interpreter. 이것은 거의 항상 실수입니다. 프로그램의 동작은$PATH스크립트를 호출하는 사용자 의 변수에 따라 달라집니다 . 그리고 모든 시스템이env애초에 그런 것은 아닙니다 . - 필요
setuid하거나setgid권한이없는 프로그램은 사용할 수 없습니다#!. 기계 코드로 컴파일해야합니다. (무엇인지 모르더라도setuid걱정하지 마십시오.)
Regarding csh, it relates to sh roughly as Nutrimat Advanced Tea Substitute does to tea. It has (or rather had; modern implementations of sh have caught up) a number of advantages over sh for interactive usage, but using it (or its descendant tcsh) for scripting is almost always a mistake. If you're new to shell scripting in general, I strongly recommend you ignore it and focus on sh. If you are using a csh relative as your login shell, switch to bash or zsh, so that the interactive command language will be the same as the scripting language you're learning.
This defines what shell (command interpreter) you are using for interpreting/running your script. Each shell is slightly different in the way it interacts with the user and executes scripts (programs).
When you type in a command at the Unix prompt, you are interacting with the shell.
E.g., #!/bin/csh refers to the C-shell, /bin/tcsh the t-shell, /bin/bash the bash shell, etc.
You can tell which interactive shell you are using the
echo $SHELL
command, or alternatively
env | grep -i shell
You can change your command shell with the chsh command.
Each has a slightly different command set and way of assigning variables and its own set of programming constructs. For instance the if-else statement with bash looks different that the one in the C-shell.
This page might be of interest as it "translates" between bash and tcsh commands/syntax.
Using the directive in the shell script allows you to run programs using a different shell. For instance I use the tcsh shell interactively, but often run bash scripts using /bin/bash in the script file.
Aside:
This concept extends to other scripts too. For instance if you program in Python you'd put
#!/usr/bin/python
at the top of your Python program
참고URL : https://stackoverflow.com/questions/10591086/shell-script-headers-bin-sh-vs-bin-csh
'program story' 카테고리의 다른 글
| Java에서 길이를 알 수없는 바이트 배열 (0) | 2020.11.11 |
|---|---|
| 간단한 '평균'함수를 좌절시키는 Haskell 유형 (0) | 2020.11.11 |
| SimpleDateFormat 클래스에서 사용할 수있는 날짜 형식은 무엇입니까? (0) | 2020.11.10 |
| 문자 뒤의 문자열 가져 오기 (0) | 2020.11.10 |
| 하위 폴더에있는 파일을 어떻게 감시합니까? (0) | 2020.11.10 |