How to add and delete contents in menu.lst in Linux
I was creating a RPM for Linux in one of my projects. I needed to add contents to the menu.lst which is found in /boot/grub/. The operating system that I'm using is Suse. There are two problems that needs to
be done. 1. Need to add content to the file 2. Remove the content from the file.
I use the bash script to do both.
To add content to the file:
cat /boot/grub/menu.tmp >> /boot/grub/menu.lst
wc -l /boot/grub/menu.lst > /boot/grub/menu.lstcount
The first line is to append the content in menu.tmp to menu.lst.
The second is count the number of lines in the menu.lst and save the result to menu.lstcount.
To delete the content of the file:
num=`grep -o ".*[0-9]" /boot/grub/menu.lstcount`
let begin=$num-3
mv /boot/grub/menu.lst /boot/grub/menu.orig
sed "$begin,$num"d /boot/grub/menu.orig > /boot/grub/menu.lst
The first line is retrieve the line number stored in menu.lstcount
Second line is to assign the difference of the line number retrieve in line 1 and save it to variable "begin"
Third line is to rename menu.lst to menu.orig
Fourth line is to delete the line starting from value of variable "begin" to value of variable "num" and save it to menu.lst
The downside of this solution is that if ever new contents is inserted so the line number will be messed up
ending up deleting the wrong contents.
be done. 1. Need to add content to the file 2. Remove the content from the file.
I use the bash script to do both.
To add content to the file:
cat /boot/grub/menu.tmp >> /boot/grub/menu.lst
wc -l /boot/grub/menu.lst > /boot/grub/menu.lstcount
The first line is to append the content in menu.tmp to menu.lst.
The second is count the number of lines in the menu.lst and save the result to menu.lstcount.
To delete the content of the file:
num=`grep -o ".*[0-9]" /boot/grub/menu.lstcount`
let begin=$num-3
mv /boot/grub/menu.lst /boot/grub/menu.orig
sed "$begin,$num"d /boot/grub/menu.orig > /boot/grub/menu.lst
The first line is retrieve the line number stored in menu.lstcount
Second line is to assign the difference of the line number retrieve in line 1 and save it to variable "begin"
Third line is to rename menu.lst to menu.orig
Fourth line is to delete the line starting from value of variable "begin" to value of variable "num" and save it to menu.lst
The downside of this solution is that if ever new contents is inserted so the line number will be messed up
ending up deleting the wrong contents.
Comments
Post a Comment