微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

linux shell 判断文件是否存在并为空

目录

相关语法

判断文件是否存在

判断文件是否为空

实例


相关语法

[ -f FILE ]  如果 FILE 存在且是一个普通文件则为真。  

[ -s FILE ]  如果 FILE 存在且大小不为0则为真。  

判断文件是否存在

file="config.yaml"
if [ ! -f "$file" ] # not exist
then 
 echo "file not exist"
 # do something 
 # touch "$file"
else
 echo "file exist"
fi

判断文件是否为空

如果文件为空,则一直下载,直到文件不为空为止。

file="config.yaml"
while [ ! -s "$file" ] 
 do
  echo "redownloading...."
  wget -O config.yaml "https://www.thismiao.xyz/link/21fQ=info"
 done

实例

cd /home/vm/software/clash

file="config.yaml"


rm "$file"

if [ ! -f "$file" ] # not exist
then 
 echo "download the config file"
 while [ ! -s "$file" ] # -s if file empty
 do
  echo "redownloading...."
  wget -O config.yaml "https://www.thismiao.xyz/link/2?cl&log-level=info"
 done
fi

./clash -d .

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐