列出所有 cronjobs(如果有没有多余信息)
List all cronjobs if there are any without superfluous info
正在尝试输出 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 分钟。不抱怨,只求。
crontab 正在将 "no crontab for user" 连接到标准错误。要删除这些消息,您可以 运行
crontab -l -u $USER 2>/dev/null
在你的循环中。
我还建议将 USER
重命名为其他名称。 USER
变量名是保留的,应设置为您的用户(登录)名。通常,您应该使用小写的变量名,以避免这种名称冲突。
正在尝试输出 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 分钟。不抱怨,只求。
crontab 正在将 "no crontab for user" 连接到标准错误。要删除这些消息,您可以 运行
crontab -l -u $USER 2>/dev/null
在你的循环中。
我还建议将 USER
重命名为其他名称。 USER
变量名是保留的,应设置为您的用户(登录)名。通常,您应该使用小写的变量名,以避免这种名称冲突。