LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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!

Notices


Reply
  Search this Thread
Old 03-22-2016, 01:26 PM   #1
iFunction
Member
 
Registered: Nov 2015
Posts: 187

Rep: Reputation: Disabled
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
 
Old 03-22-2016, 03:13 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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.
 
Old 03-22-2016, 05:36 PM   #3
iFunction
Member
 
Registered: Nov 2015
Posts: 187

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-23-2016, 08:34 AM   #4
iFunction
Member
 
Registered: Nov 2015
Posts: 187

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-23-2016, 10:46 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
See if this helps?
 
1 members found this post helpful.
Old 03-23-2016, 12:48 PM   #6
iFunction
Member
 
Registered: Nov 2015
Posts: 187

Original Poster
Rep: Reputation: Disabled
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
 
Old 03-23-2016, 01:34 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
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)
 
1 members found this post helpful.
Old 03-23-2016, 01:46 PM   #8
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,604

Rep: Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241Reputation: 1241
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.
 
1 members found this post helpful.
Old 03-23-2016, 02:51 PM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
Listen to jpollard ... his information is much better Should have thought of that one myself ..
 
1 members found this post helpful.
Old 03-23-2016, 07:20 PM   #10
iFunction
Member
 
Registered: Nov 2015
Posts: 187

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Which file export command edits netpumber Linux - General 2 07-02-2012 04:52 PM
file systems: external edits to an open file hydraMax Programming 5 05-25-2012 12:00 AM
SED edits configuration file lusiads Programming 7 09-12-2008 04:25 AM
Script edits KDE-Config? theonebeyond Programming 0 09-22-2004 02:53 AM
editing config files.. embarassingly basic question adhara Linux - Newbie 3 12-23-2003 08:27 AM


All times are GMT -5. The time now is 04:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration