Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

bash - 如何判断Bash中是否不存在常规文件?(How do I tell if a regular file does not exist in Bash?)

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 $FILE does not exist."
fi

What's the correct syntax to use if I only want to check if the file does not exist?

(如果我只想检查文件是否存在,使用的正确语法是什么?)

#!/bin/bash

FILE=$1     
if [ $FILE does not exist ]; then
   echo "File $FILE does not exist."
fi
  ask by Bill the Lizard translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The test command ( [ here) has a "not" logical operator which is the exclamation point (similar to many other languages).

(测试命令( [这里)有一个“非”逻辑运算符,它是感叹号(类似于许多其他语言)。)

Try this:

(尝试这个:)

if [ ! -f /tmp/foo.txt ]; then
    echo "File not found!"
fi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...