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

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

    You can also group multiple commands in the one liner

    [ -f "filename" ] || ( echo test1 && echo test2 && echo test3 )

    or

    [ -f "filename" ] || { echo test1 && echo test2 && echo test3 ;}

    If filename doesn't exit, the output will be

    test1
    test2
    test3
    

    Note: ( ... ) runs in a subshell, { ... ;} runs in the same shell. The curly bracket notation works in bash only.

提交回复
热议问题