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

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
st0de

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

Post by st0de »

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 LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29597
Joined: Wed Jul 06, 2011 3:58 am

Re: Automated edition of /etc/rc.local

Post by xenopeek »

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.
Image
st0de

Re: Automated edition of /etc/rc.local

Post by st0de »

Thanks! That's exactly what I needed.

/st0de
Locked

Return to “Scripts & Bash”