Shell脚本实战:-f参数精准判断文件类型(附真实案例)
touch real_file mkdir fake_file [ -f real_file ] && echo "真文件" # 会欢呼 [ -f fake_file ] || echo "冒牌货" # 会揭穿
mkdir twilight_zone && touch twilight_zone/{real_file,empty_dir}
[ -e twilight_zone/real_file ] && echo "-e点头" # 始终宽容
[ -f twilight_zone/empty_dir ] || echo "-f摇头" # 严格执法
[ -d twilight_zone/empty_dir ] && echo "-d微笑" # 确认身份
archive_log(){
local logfile=$1
while [ ! -f "${logfile}.complete" ]; do
sleep 5
[ -f "$logfile" ] || return 1
done
if [ -f "$logfile" -a -s "$logfile" ]; then
mv -- "$logfile" "${archive_dir}/$(date +%Y%m%d).log"
touch "${logfile}.fresh"
fi
}