다음 스크립트를 사용하여 파일이 있는지 확인했습니다.
#!/bin/bash
FILE=$1
if [ -f $FILE ]; then
   echo "File $FILE exists."
else
   echo "File $FILE does not exist."
fi파일이 존재 하지 않는지 확인하고 싶을 때 올바른 구문은 무엇입니까 ?
#!/bin/bash
FILE=$1
if [ $FILE does not exist ]; then
   echo "File $FILE does not exist."
fi답변
테스트 명령 ( [여기) (다른 많은 언어와 유사) 느낌표입니다 “없습니다”논리 연산자가 있습니다. 이 시도:
if [ ! -f /tmp/foo.txt ]; then
    echo "File not found!"
fi답변
배쉬 파일 테스트
-b filename-특수 파일 차단 
-c filename-특수 문자 파일 
-d directoryname-디렉토리 존재 
-e filename여부 확인-유형 (노드, 디렉토리, 소켓 등)에 관계없이 
-f filename파일 존재 여부 
-G filename확인- 디렉토리가 아닌 일반 파일 존재 여부 확인-파일이 존재하고 소유하고 있는지 확인 유효 그룹 ID- 
-G filename set-group-id파일이 존재하고 set-group-id 이면 True- 
-k filename고정 비트 
-L filename-기호 링크 
-O filename-파일이 존재하고 유효 사용자 ID가 소유하는 
-r filename경우 True-파일을 읽을 수 
-S filename있는지 확인-파일이 소켓 
-s filename인지 확인- 파일이 소켓 인지 확인 파일 크기가 0이 아닌 
-u filename경우-파일 set-user-id 비트가 설정되어 
-w filename있는지 확인-파일이 쓰기 
-x filename가능한지 확인-파일이 실행 가능한지 확인
사용하는 방법:
#!/bin/bash
file=./file
if [ -e "$file" ]; then
    echo "File exists"
else
    echo "File does not exist"
fi 테스트 발현 은 USING 의해 무효화 될 수 !연산자
#!/bin/bash
file=./file
if [ ! -e "$file" ]; then
    echo "File does not exist"
else
    echo "File exists"
fi 답변
“!”로 표현식을 부정 할 수 있습니다.
#!/bin/bash
FILE=$1
if [ ! -f "$FILE" ]
then
    echo "File $FILE does not exist"
fi관련 매뉴얼 페이지입니다 man test동등, 또는 man [– 또는 help test또는 help [를 위해 내장 된 bash는 명령.
답변
[[ -f $FILE ]] || printf '%s does not exist!\n' "$FILE"또한 파일이 깨진 심볼릭 링크이거나 비정규 파일 (예 : 소켓, 장치 또는 fifo) 일 수 있습니다. 예를 들어 깨진 심볼릭 링크를 확인하려면 다음을 수행하십시오.
if [[ ! -f $FILE ]]; then
    if [[ -L $FILE ]]; then
        printf '%s is a broken symlink!\n' "$FILE"
    else
        printf '%s does not exist!\n' "$FILE"
    fi
fi답변
단일 명령을 실행해야하는 경우 약어를 사용할 수 있습니다.
if [ ! -f "$file" ]; then
    echo "$file"
fi에
test -f "$file" || echo "$file"또는
[ -f "$file" ] || echo "$file"답변
POSIX 셸 호환 형식으로 다음과 같은 단일 라이너를 선호 합니다.
$ [ -f "/$DIR/$FILE" ] || echo "$FILE NOT FOUND"
$ [ -f "/$DIR/$FILE" ] && echo "$FILE FOUND"스크립트에서와 같이 몇 가지 명령의 경우 :
$  [ -f "/$DIR/$FILE" ] || { echo "$FILE NOT FOUND" ; exit 1 ;}일단이 작업을 시작하면 더 이상 완전 형식의 구문을 거의 사용하지 않습니다!
답변
파일 존재를 테스트하기 위해 매개 변수는 다음 중 하나 일 수 있습니다.
-e: Returns true if file exists (regular file, directory, or symlink)
-f: Returns true if file exists and is a regular file
-d: Returns true if file exists and is a directory
-h: Returns true if file exists and is a symlink아래의 모든 테스트는 일반 파일, 디렉토리 및 심볼릭 링크에 적용됩니다.
-r: Returns true if file exists and is readable
-w: Returns true if file exists and is writable
-x: Returns true if file exists and is executable
-s: Returns true if file exists and has a size > 0스크립트 예 :
#!/bin/bash
FILE=$1
if [ -f "$FILE" ]; then
   echo "File $FILE exists"
else
   echo "File $FILE does not exist"
fi