怎样判断一个普通文件并不在bash存在? [英] How do I tell if a regular file does not exist in bash?

查看:136
本文介绍了怎样判断一个普通文件并不在bash存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的脚本,看看文件是否存在:

 #!/斌/庆典
FILE = $ 1如果[-f $ FILE]
然后
   回声文件$文件不存在。
其他
   回声文件$文件不存在。
科幻

什么是正确的语法使用,如果我只是想检查文件是否确实的不可以存在吗?

 #!/斌/庆典
FILE = $ 1如果[$文件不存在]
然后
   回声文件$文件不存在。
科幻


解决方案

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

 如果[! -f /tmp/foo.txt];然后
    回声找不到文件!
科幻

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

解决方案

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

这篇关于怎样判断一个普通文件并不在bash存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆