Page 1 of 1
creating scripts to automate setup of new images
Posted: Mon May 15, 2017 3:25 am
by chito
Hi I've been playing around with a few images trying to work out which one is right for me. One issue that this has created is having to manually make changes to files each time.
So I want to create a script that will make these changes for me.
for example, Im using HIfiBerry DAC which requires that I open /boot/config.txt (needs sudo)
find and edit the line dtparam=audio=on (id rather just comment it out)
replace it with dtoverlay=hifiberry-dacplus
Then I need to create the file /etc/asound.conf with the contents:
pcm.!default {
type hw card 0
}
ctl.!default {
type hw card 0
}
How could I create a shell script for this that will work on the raspberry pi?
Re: creating scripts to automate setup of new images
Posted: Mon May 15, 2017 10:28 am
by mfa298
sed is your friend for things like this. as an example (from memory)
Code: Select all
sudo sed -i -e '/^dtparam=audio/ s/^/# /' /boot/config.txt
this does an in place edit of the file (-i)
searches for a line starting dtparam=audio (/^dtparam=audio/)
and then does a search and replace (s) on that line replacing the start of the line (^) with a comment (# )
you could expand that with (again from memory)
Code: Select all
sudo sed -i -e '/^dtparam=audio/ {s/^/# /; a dtparam=hifiberry-dacplus}' /boot/config.txt
which should comment out the old line (as before) and then append (a) the new line after.
this form is probably also safe in that if you ran it a second time it wont make any changes.
for testing remove the sudo (means you cant write a new file) and the -i and it'll print out the new version on the console.
Re: creating scripts to automate setup of new images
Posted: Wed May 17, 2017 6:21 am
by chito
awesome! thanks heaps for your help. this is great. ill start experimenting with this tonight!
I read about sed (dont really know what it is) but I wasn't sure that would work with the pi. I read somewhere that shell scripts werent meant to edit files or something like that.
is sed another language? because i saw some similar code that used perl
how do i create the new file /etc/asound.conf with
Code: Select all
pcm.!default {
type hw card 0
}
ctl.!default {
type hw card 0
}
Re: creating scripts to automate setup of new images
Posted: Wed May 17, 2017 8:41 am
by jahboater
chito wrote:
I read about sed (dont really know what it is) but I wasn't sure that would work with the pi. I read somewhere that shell scripts werent meant to edit files or something like that.
"sed" is short for stream editor. Its quite hard to learn (as usual see "man sed"). Of course you can edit files in shell scripts by various means, but take care (and save a backup of the original file first).
chito wrote:how do i create the new file /etc/asound.conf with
Code: Select all
pcm.!default {
type hw card 0
}
ctl.!default {
type hw card 0
}
You don't need sed to create a new file.
One was is to simply copy in a pre-prepared file.
Another way is within the shell script itself using a feature oddly called a "here document"
Code: Select all
cat <<EOF >/etc/asound.conf
pcm.!default {
type hw card 0
}
ctl.!default {
type hw card 0
}
EOF
This must be run as root, so try this instead for the first line:-
Code: Select all
cat <<EOF | sudo tee /etc/asound.conf >/dev/null
Re: creating scripts to automate setup of new images
Posted: Wed May 17, 2017 9:21 am
by mfa298
jahboater wrote:This must be run as root, so try this instead for the first line:-
Code: Select all
cat <<EOF | sudo tee /etc/asound.conf >/dev/null
You should be able to shorten that slightly to
Code: Select all
sudo tee /etc/asound.conf << EOF >/dev/null
Re: creating scripts to automate setup of new images
Posted: Wed May 17, 2017 10:32 am
by jahboater
mfa298 wrote:You should be able to shorten that slightly to
Code: Select all
sudo tee /etc/asound.conf << EOF >/dev/null
Yes much better!
You can practice all this sort of thing on the command line of course, just to try it out.
Re: creating scripts to automate setup of new images
Posted: Wed May 17, 2017 10:42 am
by chito
thanks heaps!!!
this is exactly what i needed. with this i think i can make the other changes i need. its certainly heaps to go on.
really appreciate the help
Re: creating scripts to automate setup of new images
Posted: Wed May 17, 2017 3:18 pm
by mfa298
chito wrote:
this is exactly what i needed. with this i think i can make the other changes i need. its certainly heaps to go on.
The other powerful tool that can be useful is awk, for instance you could list the set of configured dtparams and their settings with something like:
Code: Select all
pi@pi2:~ $ awk -F= '/^dtparam/ {print $2, $3}' /boot/config.txt
spi on
audio on
That example sets the = as the field separator (-F), then searches for lines starting dtparam (/^dtparam/) and shows the 2nd and 3rd fields (print $2, $3), you could also print the whole line with $0.
Other generic tools you might find useful in a script are: tr, cut, head, tail, grep, sort, uniq
Re: creating scripts to automate setup of new images
Posted: Wed May 17, 2017 3:29 pm
by jbudd
You can see an example of a script to setup various options on a new Raspberry Pi or other single board computer at
https://bitbucket.org/snippets/scargill ... the-script.
It's a big shell script that does assorted updates, installations and setup functions. Not directly relevant to your HiFiBerry setup but it may be useful as a model
Re: creating scripts to automate setup of new images
Posted: Wed May 17, 2017 10:47 pm
by chito
thanks heaps that looks really really useful.
i have a few things i need to do to setup an image and the Hifiberry setup was a good starting point as it needs find and replace settings and creat a new file. 2 of the things i need to do alot
Re: creating scripts to automate setup of new images
Posted: Thu May 18, 2017 1:51 am
by chito
so what happens when the code to create a new file tries to create it and there's already a file with that name?
will it replace it? if that makes things easier. if not the instructions from hifiberry are to delete it and create a new one
Code: Select all
sudo tee /etc/asound.conf << EOF >/dev/null
pcm.!default {
type hw card 0
}
ctl.!default {
type hw card 0
}
EOF
Re: creating scripts to automate setup of new images
Posted: Thu May 18, 2017 1:56 am
by mfa298
chito wrote:so what happens when the code to create a new file tries to create it and there's already a file with that name?
will it replace it? if that makes things easier. if not the instructions from hifiberry are to delete it and create a new one
Code: Select all
sudo tee /etc/asound.conf << EOF >/dev/null
pcm.!default {
type hw card 0
}
ctl.!default {
type hw card 0
}
EOF
"man tee" would give you the answers for that one.
by default tee will overwrite the file, you could also supply the -a argument to it making it append to the file.
Re: creating scripts to automate setup of new images
Posted: Thu May 18, 2017 6:39 am
by jahboater
See also "man install" and "man test"
Use test like this (its also called "[" !!):-
Code: Select all
if [ -e /etc/asound.conf ] ; then
echo "File exists"
fi
Re: creating scripts to automate setup of new images
Posted: Fri May 19, 2017 12:59 pm
by chito
Ok so i tried it for the first time. created the a script called mysetup.sh
Code: Select all
sudo sed -i -e '/^dtparam=audio/ {s/^/# /; a dtparam=hifiberry-dacplus}' /boot/config.txt
cat <<EOF >/etc/asound.conf
pcm.!default {
type hw card 0
}
ctl.!default {
type hw card 0
}
EOF
I made it executable with
Code: Select all
sudo chmod 0744 /home/pi/mysetup.sh
ran the code with from within /home/pi
And i got the following errors
Code: Select all
sed: -e expression #1, char 0: unmatched `{'
: not founder.sh: 2: ./mysetup.sh:
Re: creating scripts to automate setup of new images
Posted: Fri May 19, 2017 1:20 pm
by jbudd
You could do it as two seperate sed commands:
Code: Select all
sed -i -e '/^dtparam=audio/ s/^/#/' -e '/^#dtparam=audio/a dtparam=hifiberry-dacplus '
Re: creating scripts to automate setup of new images
Posted: Fri May 19, 2017 1:37 pm
by chito
I get the following error now
Code: Select all
sed: no input files
: not founder.sh: 3: ./mysetup.sh:
Re: creating scripts to automate setup of new images
Posted: Fri May 19, 2017 1:42 pm
by jbudd
Did you just copy and paste my suggestion? Because that does not, as the error message hints, specify any input files. (I didn't want to edit my /boot/config.txt so I tried it on some other file)
Re: creating scripts to automate setup of new images
Posted: Fri May 19, 2017 1:59 pm
by chito
Yep sorry my oversight, so i added the file
Code: Select all
sudo sed -i -e '/^dtparam=audio/ s/^/#/' -e '/^#dtparam=audio/a dtparam=hifiberry-dacplus' /boot/config.txt
and the error is weird
Code: Select all
: No such file or directoryg.txt
: not founder.sh: 3: ./mysetup.sh:
no such directoryg.txt... whats directoryg.txt? something is going on with the filename
Re: creating scripts to automate setup of new images
Posted: Fri May 19, 2017 2:03 pm
by jbudd
Does the string "g.txt" exist anywhere in your script file? Possibly some control character has snuck in and the shell is misinterpreting it.
Re: creating scripts to automate setup of new images
Posted: Fri May 19, 2017 2:08 pm
by chito
this is the entire content of my script
Code: Select all
sudo sed -i -e '/^dtparam=audio/ s/^/#/' -e '/^#dtparam=audio/a dtparam=hifiberry-dacplus' /boot/config.txt
cat <<EOF >/etc/asound.conf
pcm.!default{
type hw card 0
}
ctl.!default {
type hw card 0
}
EOF
Re: creating scripts to automate setup of new images
Posted: Fri May 19, 2017 2:17 pm
by jbudd
In your script file, retype the first line, not copy and paste. Don't split it into multiple lines. Then delete the original line and see if it works.
If you can't find a bug in a shell script it can help to try and do less with each line of code. For example instead of two -e parameters, run sed twice, each time making only one edit. At least you should find out which bit is upsetting it.
Re: creating scripts to automate setup of new images
Posted: Fri May 19, 2017 2:39 pm
by chito
i can tell this is going to be one of those annoying problems that is going to be a pain to sort out
retyped the line, didnt work
the tried the following two lines and they both didnt work
Code: Select all
sudo sed -i -e '/^dtparam=audio/ s/^/#/' /boot/config.txt
Code: Select all
sudo sed -i -e '/^#dtparam=audio/a dtparam=hifiberry-dacplus' /boot/config.txt
they all give the same error
so then i created a test.txt in /home/pi. updated the line to
Code: Select all
sudo sed -i -e '/^dtparam=audio/ s/^/#/' -e '/^#dtparam=audio/a dtparam=hifiberry-dacplus' /home/pi/test.txt
and i get