LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Basic install file that also edits certain config files (https://www.linuxquestions.org/questions/linux-newbie-8/basic-install-file-that-also-edits-certain-config-files-4175575605/)

iFunction 03-22-2016 12:26 PM

Basic install file that also edits certain config files
 
Hi there,

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.

Kind regards
iFunc

grail 03-22-2016 02:13 PM

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.

iFunction 03-22-2016 04:36 PM

Ah, ok, yes, both files need sudo, but as this is on Raspberry Pi's, this is not such an issue. Thank you, I shall do some research and see what comes up and will post my results.

iFunction 03-23-2016 07:34 AM

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:
Code:

sed -i '69 a #somehow adding the text from the other file called taskbarMod' panel.txt

grail 03-23-2016 09:46 AM

See if this helps?

iFunction 03-23-2016 11:48 AM

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:

Code:

#!/bin/bash

# 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.

DIRECTORY='/home/pi/bash_scripts/'
ICON='/usr/share/my_icons/'
DESKTOP='/home/pi/Desktop/'
TASKBAR='/usr/share/raspi-ui-overrides/applications/'
TASKBARMOD='/home/pi/.config/lspanel/LXDE-pi/panels/panel'

if [ ! -d "$DIRECTORY" ]; then
    mkdir $DIRECTORY
fi
if [ ! -d "$ICON$ ]; then
    sudo mkdir $ICON
fi

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
sudo reboot

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.

Thanks for all your pointers people

iFunk

grail 03-23-2016 12:34 PM

Tips would be:

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)

jpollard 03-23-2016 12:46 PM

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.

grail 03-23-2016 01:51 PM

Listen to jpollard ... his information is much better :) Should have thought of that one myself .. :banghead:

iFunction 03-23-2016 06:20 PM

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.

Thanks again

iFunk


All times are GMT -5. The time now is 10:47 PM.