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

잔글 (Jmnote 사용자가 Bash 쉘 스크립트 위치 얻기 문서를 Bash 쉘 스크립트 절대경로 얻기 문서로 옮겼습니다)
 
(사용자 2명의 중간 판 29개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==개요==
;Bash 쉘 스크립트 위치 얻기
;Bash 쉘 스크립트 위치 얻기
;Bash 스크립트 폴더 확인
;Bash 쉘스크립트 절대경로 얻기
* 쉘스크립트 내부에서 자신이 있는 위치를 절대경로로 얻는 방법
* 어느 위치에서 실행하든 쉘스크립트 파일의 [[절대경로]]를 얻을 수 있음
* 어느 위치에서 실행하든 쉘스크립트 파일의 [[절대경로]]를 얻을 수 있음
<source lang='bash'>
 
script_dir=$(cd "$(dirname "$0")" && pwd)
{{소스헤더|일반형}}
</source>
<syntaxhighlight lang='bash'>
HERE=$(dirname $(realpath $0))
# 또는
HERE=$( cd "$(dirname "$0")" ; pwd )
</syntaxhighlight>
{{소스헤더|심볼릭 링크라면 원래위치까지 추적}}
<syntaxhighlight lang='bash'>
HERE=$(dirname $(realpath -s $0))
# 또는
HERE=$( cd "$(dirname "$0")" ; pwd -P )
</syntaxhighlight>


==실행 예시==
==실행 예시==
<source lang='cli'>
<syntaxhighlight lang='console'>
root@zetawiki:~/test# cat hello.sh
root@localhost1:/tmp/aaa# cat test.sh  
script_dir=$(cd "$(dirname "$0")" && pwd)
#!/bin/bash
echo $script_dir
HERE=$(dirname $(realpath $0))
</source>
echo $HERE
<source lang='cli'>
</syntaxhighlight>
root@zetawiki:~/test# sh hello.sh
<syntaxhighlight lang='console'>
/root/test
root@localhost1:/tmp/aaa# ./test.sh
root@zetawiki:~/test# sh ./hello.sh
/tmp/aaa
/root/test
root@localhost1:/tmp/aaa# sh test.sh  
root@zetawiki:~/test# cd /tmp
/tmp/aaa
root@zetawiki:/tmp# sh /root/test/hello.sh
root@localhost1:/tmp/aaa# bash test.sh
/root/test
/tmp/aaa
root@zetawiki:/tmp# sh ~/test/hello.sh
</syntaxhighlight>
/root/test
<syntaxhighlight lang='console'>
</source>
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
</syntaxhighlight>
<syntaxhighlight lang='console'>
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
</syntaxhighlight>


==같이 보기==
==같이 보기==
*[[리눅스 $0]]
* [[Bash 쉘스크립트 폴더명 얻기]]
*[[리눅스 dirname]]
* [[Bash 쉘스크립트 위치로 이동]]
*[[리눅스 pwd]]
* [[Bash 쉘스크립트 상대경로 얻기]]
*[[리눅스 cd]]
* [[리눅스 $0]]
* [[리눅스 realpath]]
* [[리눅스 dirname]]
* [[리눅스 pwd]]
* [[리눅스 cd]]
* [[절대경로]]
* [[PHP 스크립트 절대경로 얻기]]
 
==참고==
* https://stackoverflow.com/questions/4774054/reliable-way-for-a-bash-script-to-get-the-full-path-to-itself


[[분류: Bash]]
[[분류: Bash]]

2023년 10월 11일 (수) 00:49 기준 최신판

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 }}