How do I tell if a regular file does not exist in Bash?

后端 未结 20 2501
深忆病人
深忆病人 2020-11-22 10:57

I\'ve used the following script to see if a file exists:

#!/bin/bash

FILE=$1     
if [ -f $FILE ]; then
   echo \"File $FILE exists.\"
else
   echo \"File $         


        
20条回答
  •  逝去的感伤
    2020-11-22 11:23

    [[ -f $FILE ]] || printf '%s does not exist!\n' "$FILE"
    

    Also, it's possible that the file is a broken symbolic link, or a non-regular file, like e.g. a socket, device or fifo. For example, to add a check for broken symlinks:

    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
    

提交回复
热议问题