温馨提示×

怎么使用shell判断文件是否存在

小亿
84
2024-02-23 14:26:27
栏目: 编程语言

在shell脚本中可以使用-f选项判断文件是否存在,示例如下:

#!/bin/bash

file_path="/path/to/your/file"

if [ -f "$file_path" ]; then
    echo "File exists"
else
    echo "File does not exist"
fi

在上面的示例中,首先定义了文件的路径file_path,然后使用-f选项来判断文件是否存在,如果文件存在,则输出"File exists",如果文件不存在,则输出"File does not exist"。

0