bash - Shell 脚本“for”循环语法
问题描述
我已经完成了以下工作:
i=0
for log in $(ls -1dt import*.log)
do
((i++))
if [ $i -gt 3 ]; then
rm -rf $log
fi
done
它产生一个:"syntax error near unexpected token 'do' "
有人可以帮忙吗?
解决方案
无需解析即可执行相同操作的简单逻辑ls
-
stat -c "%Y %n" import*.log | # list last modification and filename
sort -rn | # sort reverse numeric on mod time
while read es fn; # read the timestamp and the filename
do if (( ++latest <= 3 )); # count each file found, for 1st 3
then continue; # do nothing, skipping them
else rm -f "$fn"; # -r for recurse only needed on dir's
fi;
done
推荐阅读
- python - 如何用 pyspellchecker 替换标记
- c - 串行设备读取功能中的 SegFault
- python - Python Matplotlib 散点图标注在绘图点
- android - 如何使 android:src 值成为从 Xamarin.Forms 中的 API 对象中提取的动态值?
- c - 我不能在 Clion 中包含 OpenGL
- python - 从 input() 输出后保持定义的变量
- python - ValueError:不是内置停止列表:印度尼西亚语
- android - 无法在同一个 wifi 上从 android 调用 HTTPS 到 Spring Boot 后端
- excel - 在 textjoint 列复制中查找特定值并将其粘贴到新工作表
- c# - 将变量定义为 ParentClass 并将其传递给接受 ChildrenClassA 和 ChildrenClassB 的函数