Basic install file that also edits certain config files
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Basic install file that also edits certain config files
I have written a few scripts to customize the standard Raspbian OS and I want to put it all into one simple package. It mostly consists of just copying files to various locations which is the easy part, however, as I have got into this, it occurred to me that this was actually a much better way to create the desired result as opposed to just cloning an image I already have for a few reasons:
It would be easier for someone not so savvy to just copy the file to the desktop and double click, it would be much easier to store and keep hold of the file and prevent all sorts of unwanted rubbish being copied over.
Where I am getting a little lost is how to edit config files. There are two specific things that I do with config files, namely creating a desktop shortcut to open a Terminal window that is a little easier with the keyboards that are being used, and also adding a taskbar shortcut. Both are simply adding only a couple of lines of code into a file, but I don't know where to start when it comes to editing config files from a script.
Any advice on this would be very much appreciated as I am keen to learn. Currently I am just using a bash install script with "makeself", I can post what I have done, but it does not include any of the config file editing and the rest works fine. Some pointers of a more standard way to do this is what I am after.
Not knowing what changes you are making I would generally suggest sed (and -i option) for editing files with simple changes.
Of course depending on where the config files are you may need to concern yourself with whether or not you need to provide root access (with sudo or the like) to perform the changes.
Ok, getting really bogged down with this now. So basically, I have a config file, and I want to add all the text from a file 'taskbarMod' after line 69 but I can't for the life of me work it out now as apparently append doesn't support address ranges. I'm assuming that I have just got this wrong, but can someone help me out on this one please, as I am lost beyond belief. I've got this far:
sed -i '69 a #somehow adding the text from the other file called taskbarMod' panel.txt
I have now solved this and am quite pleased with the result, here is my complete install file, please have a look and let me know how I could improve it, though it works currently:
# Self installing script for small application that formats a USB thumb drive to FAT32.
# Five files should be in the directory, the script itself, the icon image, the .desktop file
# the config file modification, and this script also.
if [ ! -d "$DIRECTORY" ]; then
if [ ! -d "$ICON$ ]; then
sudo mkdir $ICON
sudo cp ./formatusb.sh $DIRECTORY
sudo cp ./formatusb.png $ICON
sudo cp ./formatusb.desktop $DESKTOP
sudo cp ./formatusb.desktop $TASKBAR
sudo sed -in '69 r ./taskbarMod.txt' $TASKBARMOD
There are a couple of obvious issues with this, in that it will only work with the current build of "Raspbian Jessie" as the lxpanel config file has moved since the last iteration of the OS, and also the fact that Raspbian lets you sudo without a password, but other than that it is functional, all I need to do now is make it so it can't be done twice.
1. Use mkdir -p and neither if is required as the directory will get made if it does not exist or move to next line in script if it does
2. sed -in is not doing what you think it is or you have an unusual way of renaming files In its current invocation it will change the file being worked on and make a backup called 'paneln'. I am guessing you were thinking you wanted to use the -n option to not show any output, however, when using -i it already provides this functionality as it is in
edit mode. If I am wrong on your intention here ... all good
3. As the user is executing the script and you use sudo for virtually all tasks, why not just have the user call the install using sudo? (you can perform a chown if you need to alter ownership)
4. Good habit to get into with shell scripts is to quote all variables unless you specifically know they need to be unquoted (IMHO)
The real problem with "after line 69" is that what happens if somebody adds a comment to the
config file... or a couple of blank lines...
It is for this reason that diff and patch are your friends. Instead of depending on the line number diff generates context relevent patches... and patch uses that context to locate the
correct location - even if it is in a different place in the file. As long as the context matches it still gets inserted in the right place.
Oh, thanks guys, that's awesome feedback, this is the first time I have done anything like this, so the fact that it worked pleased me, grail, you are absolutely spot on about the -in thing, I thought exactly that. I have heard about the quoting, thing, I will take that on board. As for the sudo thing, this is for a very specific task, it would only ever be used on a brand new install of Raspbian, and the people who are using it really aren't savvy enough to use the command line. I am going to study the tips you have all given me and try to make this a little more robust, as there are a couple more things I want to alter at the same time, like correct screen resolution, and install of GUI for the video player.