Linux Shell: 如何判断文件是否存在并给出提示 (封装成函数)
使用以下的shell脚本函数来判断某个文件是否存在:\n\nshell\ncheck_file_exists() {\n if [ -e "$1" ]; then\n echo "File $1 exists." \n else\n echo "File $1 does not exist." \n fi\n}\n\n\n这个函数接受一个参数作为文件路径,然后使用-e选项来判断文件是否存在。如果文件存在,则输出提示信息"File [文件路径] exists.",否则输出"File [文件路径] does not exist."。\n\n你可以将这个函数添加到你的shell脚本中,并在需要的地方调用它来判断文件是否存在。例如:\n\nshell\ncheck_file_exists "/path/to/file.txt"\n\n\n请将/path/to/file.txt替换为你想要检查的文件路径。
原文地址: https://www.cveoy.top/t/topic/qbIU 著作权归作者所有。请勿转载和采集!