"Bash 쉘스크립트 절대경로 얻기"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
잔글
8번째 줄: 8번째 줄:
{{소스헤더|일반형}}
{{소스헤더|일반형}}
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
SCRIPT_PATH=$(dirname $(realpath $0))
HERE=$(dirname $(realpath $0))
# 또는
# 또는
SCRIPT_PATH=$( cd "$(dirname "$0")" ; pwd )
HERE=$( cd "$(dirname "$0")" ; pwd )
</syntaxhighlight>
</syntaxhighlight>
{{소스헤더|심볼링링크라면 원래위치까지 추적}}
{{소스헤더|심볼링링크라면 원래위치까지 추적}}
<syntaxhighlight lang='bash'>
<syntaxhighlight lang='bash'>
SCRIPT_PATH=$(dirname $(realpath -s $0))
HERE=$(dirname $(realpath -s $0))
# 또는
# 또는
SCRIPT_PATH=$( cd "$(dirname "$0")" ; pwd -P )
HERE=$( cd "$(dirname "$0")" ; pwd -P )
</syntaxhighlight>
</syntaxhighlight>


23번째 줄: 23번째 줄:
root@localhost1:/tmp/aaa# cat test.sh  
root@localhost1:/tmp/aaa# cat test.sh  
#!/bin/bash
#!/bin/bash
SCRIPT_PATH=$(dirname $(realpath $0))
HERE=$(dirname $(realpath $0))
echo $SCRIPT_PATH
echo $HERE
</syntaxhighlight>
</syntaxhighlight>
<syntaxhighlight lang='console'>
<syntaxhighlight lang='console'>

2020년 12월 21일 (월) 11:38 판

1 개요

Bash 쉘 스크립트 위치 얻기
Bash 스크립트 폴더 확인
Bash 쉘스크립트 절대경로 얻기
  • 쉘스크립트 내부에서 자신이 있는 위치를 절대경로로 얻는 방법
  • 어느 위치에서 실행하든 쉘스크립트 파일의 절대경로를 얻을 수 있음
일반형
HERE=$(dirname $(realpath $0))
# 또는
HERE=$( cd "$(dirname "$0")" ; pwd )
심볼링링크라면 원래위치까지 추적
HERE=$(dirname $(realpath -s $0))
# 또는
HERE=$( cd "$(dirname "$0")" ; pwd -P )

2 실행 예시

root@localhost1:/tmp/aaa# cat test.sh 
#!/bin/bash
HERE=$(dirname $(realpath $0))
echo $HERE
root@localhost1:/tmp/aaa# ./test.sh 
/tmp/aaa
root@localhost1:/tmp/aaa# sh test.sh 
/tmp/aaa
root@localhost1:/tmp/aaa# bash test.sh 
/tmp/aaa
root@localhost1:/tmp/aaa# cd ..
root@localhost1:/tmp# aaa/test.sh 
/tmp/aaa
root@localhost1:/tmp# sh aaa/test.sh 
/tmp/aaa
root@localhost1:/tmp# bash aaa/test.sh 
/tmp/aaa
root@localhost1:/tmp# cd aaa/bbb/
root@localhost1:/tmp/aaa/bbb# ../test.sh 
/tmp/aaa
root@localhost1:/tmp/aaa/bbb# sh ../test.sh 
/tmp/aaa
root@localhost1:/tmp/aaa/bbb# bash ../test.sh 
/tmp/aaa

3 같이 보기

4 참고

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