LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Desktop (https://www.linuxquestions.org/questions/linux-desktop-74/)
-   -   Bash sciript to edit crontab (https://www.linuxquestions.org/questions/linux-desktop-74/bash-sciript-to-edit-crontab-659793/)

pdonaghy 08-01-2008 12:37 PM

Bash sciript to edit crontab
 
Hi,

I want to write a bash script that edits a crontab.

For example the crontab may have mutiples entries;

***** /etc/service/start -c
***** /etc/test/test

I want to for example edit the line

***** /etc/service/start -c
to
***** /etc/service/start

and save it.

Thanks

colucix 08-01-2008 01:08 PM

You can install a new crontab reading it from a file or from standard input. To modify an existing crontab you can retrieve it using option -l, then apply your changes and re-install it again. For example:
Code:

crontab -l | sed 's%/etc/service/start -c%/etc/service/start%' | crontab -
you have only to think about the editing command (sed) to apply the changes you have in mind.

david1941 08-01-2008 01:22 PM

If you just want to edit the crontab, look up man crontab. crontab -e at the command line edits the users crontab in an editor and when one exits the editor it updates the crontab. Using this command, you preserve the permissions of the user (including root, if so invoked).

Dave

pdonaghy 08-04-2008 03:30 AM

Thanks Guys.

This works a treat;

crontab -l | sed 's%/etc/service/start -c%/etc/service/start%' | crontab -

If I want to say though replace /etc/service/start -c anything as in (.*) with /etc/service/start. So replace /etc/service/start -c and anything after that.

I'm not having much luck in getting my expression right though

chrism01 08-12-2008 12:24 AM

Here's an example:

>echo '/etc/serve/start -c'|sed 's:/etc/serve/start -c.*:/etc/service/start:'

/etc/service/start


All times are GMT -5. The time now is 08:39 PM.