"리눅스 sh 명령어로 bash 실행하기"의 두 판 사이의 차이

 
(사용자 2명의 중간 판 7개는 보이지 않습니다)
1번째 줄: 1번째 줄:
{{테스트|우분투 14.04}}
{{테스트|우분투 14.04}}
==개요==
;리눅스 sh 명령어로 bash 실행하기
;리눅스 sh 명령어로 bash 실행하기
* bash 문법으로 작성된 스크립트인데 sh로 돌려 오류가 발생하는 경우가 종종 있다.
* 이것을 적용하면 누가 실행하더라도 일관성 있게 bash로 실행되도록 강제할 수 있다.
* 물론 bash가 없는 환경에서는 안되고, bash를 사용할 수 있는 환경에서만 가능하다.
==문제상황==
<syntaxhighlight lang='console'>
jmnote@zetawiki:~$ cat test.sh
ARR=()
ARR+=("John Smith")
ARR+=("Jane Doe")
for VALUE in "${ARR[@]}"; do echo "[$VALUE]"; done
</syntaxhighlight>
<syntaxhighlight lang='console'>
jmnote@zetawiki:~$ bash test.sh
[John Smith]
[Jane Doe]
</syntaxhighlight>
<syntaxhighlight lang='console'>
jmnote@zetawiki:~$ sh test.sh
test.sh: 1: test.sh: Syntax error: "(" unexpected
</syntaxhighlight>


==방법==
==방법==
<source lang='bash'>
<syntaxhighlight lang='bash'>
#!/bin/bash
#!/bin/bash
if [ -z "$BASH_VERSION" ]; then
if [ -z "$BASH_VERSION" ]; then
     exec bash "$0" "$@"
     exec bash "$0" "$@"
    exit
fi
fi


# 여기부터 실제 스크립트 내용
# 여기부터 실제 스크립트 내용
</source>
</syntaxhighlight>
* 또는 축약형
* 또는 축약형
<source lang='bash'>
<syntaxhighlight lang='bash'>
#!/bin/bash
#!/bin/bash
if [ -z "$BASH_VERSION" ]; then exec bash "$0" "$@"; fi
if [ -z "$BASH_VERSION" ]; then exec bash "$0" "$@"; exit; fi


# 여기부터 실제 스크립트 내용
# 여기부터 실제 스크립트 내용
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==
25번째 줄: 48번째 줄:
* [[Bash 스크립트 root로 실행하게 하기]]
* [[Bash 스크립트 root로 실행하게 하기]]


==참고 자료==
==참고==
* http://stackoverflow.com/questions/19538669/run-bash-script-with-sh
* http://stackoverflow.com/questions/19538669/run-bash-script-with-sh


[[분류: bash]]
[[분류: bash]]

2021년 4월 25일 (일) 09:44 기준 최신판

1 개요[ | ]

리눅스 sh 명령어로 bash 실행하기
  • bash 문법으로 작성된 스크립트인데 sh로 돌려 오류가 발생하는 경우가 종종 있다.
  • 이것을 적용하면 누가 실행하더라도 일관성 있게 bash로 실행되도록 강제할 수 있다.
  • 물론 bash가 없는 환경에서는 안되고, bash를 사용할 수 있는 환경에서만 가능하다.

2 문제상황[ | ]

jmnote@zetawiki:~$ cat test.sh
ARR=()
ARR+=("John Smith")
ARR+=("Jane Doe")
for VALUE in "${ARR[@]}"; do echo "[$VALUE]"; done
jmnote@zetawiki:~$ bash test.sh
[John Smith]
[Jane Doe]
jmnote@zetawiki:~$ sh test.sh
test.sh: 1: test.sh: Syntax error: "(" unexpected

3 방법[ | ]

#!/bin/bash
if [ -z "$BASH_VERSION" ]; then
    exec bash "$0" "$@"
    exit
fi

# 여기부터 실제 스크립트 내용
  • 또는 축약형
#!/bin/bash
if [ -z "$BASH_VERSION" ]; then exec bash "$0" "$@"; exit; fi

# 여기부터 실제 스크립트 내용

4 같이 보기[ | ]

5 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}