我使用以下脚本来查看文件是否存在:
#!/bin/bash
FILE=$1     
if [ -f $FILE ]; then
   echo "File $FILE exists."
else
   echo "File $FILE does not exist."
fi如果我只想检查文件是否不存在,使用的语法是什么?
#!/bin/bash
FILE=$1     
if [ $FILE does not exist ]; then
   echo "File $FILE does not exist."
fi 测试命令( [这里] 有一个 “not” 逻辑运算符,它是感叹号(类似于许多其他语言)。尝试这个: 
if [ ! -f /tmp/foo.txt ]; then
    echo "File not found!"
fiBash 文件测试
 -b filename - 阻止特殊文件
 -c filename - 特殊字符文件
 -d directoryname - 检查目录是否存在
 -e filename - 检查文件是否存在,无论类型如何(节点,目录,套接字等) 
 -f filename - 检查常规文件是否存在而不是目录
 -G filename - 检查文件是否存在并由有效组 ID 拥有
 -G filename set-group-id - 如果文件存在且为 set-group-id,则为 true 
 -k filename - 粘性位
 -L filename - 符号链接
 -O filename - 如果文件存在且为有效用户 ID 所有,则为 True 
 -r filename - 检查文件是否可读
 -S filename - 检查文件是否为套接字
 -s filename - 检查文件是否为非零大小
 -u filename - 检查文件 set-user-id 位是否已设置
 -w filename - 检查文件是否可写
 -x filename - 检查文件是否可执行
如何使用:
#!/bin/bash
file=./file
if [ -e "$file" ]; then
    echo "File exists"
else 
    echo "File does not exist"
fi使用!可以否定测试表达式 !操作者
#!/bin/bash
file=./file
if [ ! -e "$file" ]; then
    echo "File does not exist"
else 
    echo "File exists"
fi你可以用 “!” 来否定表达式:
#!/bin/bash
FILE=$1
if [ ! -f "$FILE" ]
then
    echo "File $FILE does not exist"
fi相关的手册页是man test或者等同于man [ - 或help test或help [用于内置 bash 命令。