shell清除文件内容脚本

时间:2023-03-09 09:26:03
shell清除文件内容脚本

先来代码:

[root@localhost Qingchu]# cat Qingchu.sh
#!/bin/bash
#描述: #作者:孤舟点点
#版本:1.0
#创建时间:-- ::
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH p=`pwd`
file=$p/qingchu.txt #判断文件是否存在
if [ -f "$file" ];then
ls -l $file
echo "$file文件已存在,删除重新创建此文件!"
rm $file
touch $file
echo "删除并重新建立$file文件成功!"
else
echo "$file文件不存在,新建此文件!"
touch $file
echo "$file文件新建成功!"
fi echo "$file文件信息为:"
ls -l $file
echo "" echo "给$file文件写入信息!"
top c -bn1 >$file
echo "$file文件的信息是:"
ls -l $file
echo ""
echo "清除$file文件内容!"
cat /dev/null > $file
echo "文件$file内容清除成功,清除内容后的文件大小为:"
ls -l $file
echo ""
echo "删除$file"
rm $file
echo "删除$file文件成功!"
echo "显示文件夹内容!"
ls -l $p
exit
[root@localhost Qingchu]#

运行结果:

shell清除文件内容脚本