【发布时间】:2016-05-30 04:57:21
【问题描述】:
尝试输出 cronjobs 列表,而不是用户列表。 crontab -l 的原始输出太脏了,我似乎无法清理它。我用sudo script.sh 或su 运行它,然后运行它。我也尝试过调用它sudo script.sh | grep -v no。我很困惑为什么这不起作用:
#!/bin/bash
#Trying to show all cronjobs but no extraneous info
#
# This shows "no crontab for USER" for every USER without
# a crontab - I only want to see actual cronjobs, not a long
# list of users without crontabs
echo "Here is the basic output that needs manipulation:
"
for USER in `cat /etc/passwd | cut -d":" -f1`; do
crontab -l -u $USER
done
#
# grep -v fails me
# (grep'ing the output of the script as a whole fails also)
echo "
trying with grep -v no on each line:
"
for USER in `cat /etc/passwd | cut -d":" -f1`; do
crontab -l -u $USER | grep -v no
done
echo "
maybe with quotes around the no:
"
for USER in `cat /etc/passwd | cut -d":" -f1`; do
crontab -l -u $USER | grep -v "no"
done
# string manipulation - I can't even get started
echo "
And here I try to put the commmand output into a string so I can manipulate it further, and use an if/then/fi on the product:
"
for USER in `cat /etc/passwd | cut -d":" -f1`; do
STRING="$(crontab -l -u $USER | grep -v no)"
echo "STRING: $STRING"
done
顺便说一句,有没有比在每行开头粘贴 4 个空格更简单的方法来让代码在此处正确格式化?我一定已经试验了 40 分钟。不抱怨,只是问。
【问题讨论】:
-
选择(绘制)整个代码块并按 ctrl-K 对其进行格式化。
标签: bash filter grep cron crontab