linux-tutorial/codes/shell/示例脚本/逻辑控制/检查文件或目录是否存在.sh

22 lines
509 B
Bash
Raw Normal View History

2019-05-10 11:17:57 +08:00
#!/bin/bash
2019-10-10 08:56:31 +08:00
2019-05-10 11:17:57 +08:00
# checking if a directory or a file exists
if [ -e $HOME ]
then
2019-10-10 08:56:31 +08:00
echo "OK on the directory.now to check the file"
#checking if a file exists
if [ -e $HOME/testing ]
then
#the file exists,append data to it
echo "Appending date to existing file"
date >> $HOME/testing
else
#the file is not exists,create a new file
echo "Creating a new file"
date > $HOME/testing
fi
2019-05-10 11:17:57 +08:00
else
2019-10-10 08:56:31 +08:00
echo 'Sorry. you do not have a $HOME directory'
2019-05-10 11:17:57 +08:00
fi