Automated edition of /etc/rc.local [ SOLVED ]

Forum rules
Before you post please read this

Automated edition of /etc/rc.local [ SOLVED ]

Postby st0de on Mon Aug 22, 2011 9:35 pm

Hello everyone I am using Mint since a couple of month and I have some experience with bash scripting.

As you may know, /etc/rc.local ends with "exit 0". My challenge is to use a script that read rc.local and find where is "exit 0", then create a new line over it and put the code I want to be executed, saves the file and continue my script.

Do you think it is possible to create a script that would do this? How to do it?

Thanks for your help!


/st0de
Last edited by st0de on Tue Aug 23, 2011 9:56 am, edited 1 time in total.
st0de
Level 1
Level 1
 
Posts: 3
Joined: Mon Aug 22, 2011 9:19 pm

Linux Mint is funded by ads and donations.
 

Re: Automated edition of /etc/rc.local

Postby xenopeek on Tue Aug 23, 2011 7:37 am

Usually the exit 0 line is the last line in the script. So you can do:

Code: Select all
# Store all but the last line of /etc/rc.local in /tmp/rc.local
head -n -1 /etc/rc.local > /tmp/rc.local

# Add your line of text
echo "your line to add before exit 0" >> /tmp/rc.local

# Add the last line of /etc/rc.local
tail -n 1 /etc/rc.local >> /tmp/rc.local

# Backup /etc/rc.local
sudo mv /etc/rc.local /etc/rc.local.old

# Put the new rc.local in place, with your added line
sudo mv /tmp/rc.local /etc

If the exit 0 is not the last line, this gets a little bit more complicated. Let me know if that is needed, and I'll rework the above to handle exit 0 at any point in the file.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14996
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: Automated edition of /etc/rc.local

Postby st0de on Tue Aug 23, 2011 9:55 am

Thanks! That's exactly what I needed.

/st0de
st0de
Level 1
Level 1
 
Posts: 3
Joined: Mon Aug 22, 2011 9:19 pm


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 1 guest