Bash 파일 있는지 확인

1 개요[ | ]

Bash 파일 있는지 확인
  • 절대경로, 상대경로 모두 정상 작동함
myfile='/etc/hosts'
if [ -f $myfile ]; then
    echo "'$myfile' exists."
else
    echo "'$myfile' does NOT exist."
fi
myfile='/etc/hosts'
if [ ! -f $myfile ]; then
  echo "'$myfile' does NOT exist."
fi
[ -e /etc/hosts ] && echo 있다 || echo 없다
[ -e /etc/hosts ] && echo 있다
[ -e /etc/hosts ] || echo 없다

2 실행예시1: 절대경로[ | ]

testuser@zetawiki:~$ myfile='/etc/hosts'
testuser@zetawiki:~$ if [ -f $myfile ]; then
>     echo "'$myfile' exists."
> else
>     echo "'$myfile' does NOT exist."
> fi
'/etc/hosts' exists.
testuser@zetawiki:~$ myfile='/etc/asdfasdf'
testuser@zetawiki:~$ if [ -f $myfile ]; then
>     echo "'$myfile' exists."
> else
>     echo "'$myfile' does NOT exist."
> fi
'/etc/asdfasdf' does NOT exist.

3 실행예시2: 상대경로[ | ]

testuser@zetawiki:/etc$ myfile='hosts'
testuser@zetawiki:/etc$ if [ -f $myfile ]; then
>     echo "'$myfile' exists."
> else
>     echo "'$myfile' does NOT exist."
> fi
'hosts' exists.
testuser@zetawiki:/etc$ myfile='asdfasdf'
testuser@zetawiki:/etc$ if [ -f $myfile ]; then
>     echo "'$myfile' exists."
> else
>     echo "'$myfile' does NOT exist."
> fi
'asdfasdf' does NOT exist.

4 같이 보기[ | ]

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