Shell脚本 检查文件是否存在
时间:2020-03-05 15:31:43 来源:igfitidea点击:
此Shell脚本有助于检查指定文件在给定路径下是否存在。
该脚本将提示我们提供文件名和目录路径。
用于检查文件是否存在的Shell脚本
在编写shell脚本时,我们可能会发现自己需要根据文件是否存在执行操作。
以下脚本有助于检查文件是否在目录下。
#!/bin/bash
#We tell user that he need to enter filename
echo -n "Please enter file to check: "
#We write filename to variable file
read file
#We tell user that he need to enter path to file
echo -n "Please enter path to check: "
#We write path to variable path
read path
#we check if we have read permission on path
if [ -r $path ]
then
#if we have read permissions, we check if file exist
if [ -f ${path}/${file} ]
then
#if file exist, we tell user
echo "File ${path}/${file} exist"
#end of if loop
fi
#if we don't have read permissions on path
else
#we warn user that we don't have read permissions on path
echo "You don't have access to folder $path"
fi
脚本输出
test@server:~$ls /tmp haze-MsFUwz qtsingleapp-homeye-aeea-3e8 MozillaMailnews qtsingleapp-homeye-aeea-3e8-lockfile pulse-2L9K88eMlGn7 ssh-UP2NOLoESr17 pulse-PKdhtXMmr18n unity_support_test.0 pulse-SfiK5uhmdkQW test@server:~$./file_exist.sh Please enter file to check: unity_support_test.0 Please enter path to check: /tmp File /tmp/unity_support_test.0 exist

