linux shell 脚本中 避免文件已存在多次追加的情况

 

001、为了避免脚本多次执行,生成结果多次追加,可以在追加语句的前面增加清空语句避免多次追加

复制代码
[root@pc1 test1]# ls
a.sh
[root@pc1 test1]# cat a.sh           ## 测试脚本
#!/bin/bash

> result.txt   ## 或者使用 rm -f result.txt    ## 在追加语句的前边增加清空语句
seq 2 >> result.txt
[root@pc1 test1]# bash a.sh        ## 测试脚本
[root@pc1 test1]# ls
a.sh  result.txt
[root@pc1 test1]# cat result.txt   ## 查看结果
1
2
[root@pc1 test1]# bash a.sh        ## 再次执行
[root@pc1 test1]# cat result.txt    ## 没有多次追加
1
2
复制代码

 

 。

 

posted @   小鲨鱼2018  阅读(43)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2021-03-06 python中LEGB原则
2021-03-06 python中内层函数可以调用外层函数的局部变量
2021-03-06 python中不能在外层函数以外调用内层函数
2021-03-06 python中函数后面的小括号的作用
2020-03-06 linux cut 命令的用法
点击右上角即可分享
微信分享提示