shell中修改文件的内容,请高手来指教

eft21 2011-04-19 04:24:34
要求写一个shell,往/etc/crontab文件中加入一个定时任务:
* */1 * * * root /usr/sharescm/timingshutdown.sh

判断如果/etc/crontab中有改记录这一行,然后再重新写入

请问听懂了没?


我使用sed命令:sed -e '/timingshutdown.sh/d' /etc/crontab
输出内容:# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
00 04 * * * root /usr/sharescm/backupdb.sh
#*/5 * * * * root /usr/sharescm/fileshow.sh


但是vi查看/etc/crontab:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
00 04 * * * root /usr/sharescm/backupdb.sh
#*/5 * * * * root /usr/sharescm/fileshow.sh
[color=#FF0000]* */1 * * * root /usr/sharescm/timingshutdown.sh
[/color]
这段内容还存在,请各位大虾指教如何完成这个shell
...全文
233 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
eft21 2011-04-20
  • 打赏
  • 举报
回复
多谢了
steptodream 2011-04-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 eft21 的回复:]

找到答案了
可以这样:echo "* */1 * * * root /usr/sharescm/timingshutdown.sh" >> /etc/crontab

不知道这样是不是最好的方式
[/Quote]
这已经很好了 最不容易出错的方式 为什么不呢?
eft21 2011-04-19
  • 打赏
  • 举报
回复
找到答案了
可以这样:echo "* */1 * * * root /usr/sharescm/timingshutdown.sh" >> /etc/crontab

不知道这样是不是最好的方式
eft21 2011-04-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 eft21 的回复:]
引用楼主 eft21 的回复:
要求写一个shell,往/etc/crontab文件中加入一个定时任务:
* */1 * * * root /usr/sharescm/timingshutdown.sh

判断如果/etc/crontab中有改记录这一行,然后再重新写入

请问听懂了没?


我使用sed命令:sed -e '/timingshutdown.sh/d' /etc/……
[/Quote]
不好意思 是我搞错了 你说的是对的
.bak文件的产生是因为/etc/crontab是只读的 vi修改后产生了.bak文件
请问sed命令可以在该文件的最后追加这些内容吗:* */1 * * * root /usr/sharescm/timingshutdown.sh

我没找到相应的命令,或者有没有其他的命令可以再文件的最后加入一些内容
steptodream 2011-04-19
  • 打赏
  • 举报
回复
谢谢 这样这一行记录的确被删除了 但是产生了一个bak文件 不会影响定时运行功能吧
-----------
不会影响 那就是一个备份文件

不过在linux下 -i不加参数的话 按说不会产生bak文件啊 只有
sed -i.bak -e '/timingshutdown.sh/d' /etc/crontab
的情况下才应该产生.bak文件的
eft21 2011-04-19
  • 打赏
  • 举报
回复
[Quote=引用楼主 eft21 的回复:]
要求写一个shell,往/etc/crontab文件中加入一个定时任务:
* */1 * * * root /usr/sharescm/timingshutdown.sh

判断如果/etc/crontab中有改记录这一行,然后再重新写入

请问听懂了没?


我使用sed命令:sed -e '/timingshutdown.sh/d' /etc/crontab
输出内容:# /……
[/Quote]
谢谢 这样这一行记录的确被删除了 但是产生了一个bak文件 不会影响定时运行功能吧
steptodream 2011-04-19
  • 打赏
  • 举报
回复
我使用sed命令:sed -e '/timingshutdown.sh/d' /etc/crontab
换成
sed -i -e '/timingshutdown.sh/d' /etc/crontab

23,114

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧