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

后端 未结 20 2803
深忆病人
深忆病人 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:32

    I prefer to do the following one-liner, in POSIX shell compatible format:

    $ [ -f "/$DIR/$FILE" ] || echo "$FILE NOT FOUND"
    
    $ [ -f "/$DIR/$FILE" ] && echo "$FILE FOUND"
    

    For a couple of commands, like I would do in a script:

    $  [ -f "/$DIR/$FILE" ] || { echo "$FILE NOT FOUND" ; exit 1 ;}
    

    Once I started doing this, I rarely use the fully typed syntax anymore!!

提交回复
热议问题