Raspberry Pi – sed for customization

I like automation, and have a project to migrate a friend’s web site to WordPress. There are a lot of MP3 files to move, so I’d like to script it.

I prefer to not test in production (in other words I’m not insane when it comes to computer operations) but that means having a consistent development environment. The Raspberry Pi is a very capable piece of equipment for a very reasonable price. So I think I want to automate building a development environment there.

I’m going to go with the Raspberry Pi OS with desktop

The short form of installing this is:

  1. Download the correct image
    1. I went with Raspbian instead of OpenSUSE because I want sound to work. Apparently the sound driver for Raspberry Pi has not been merged into Linux officially. As soon as it is, then OpenSUSE will have the sound driver baked in. But until then, I want to go with a Linux distribution that adds in the drivers for the Raspberry Pi sound hardware.
  2. Copy the image to the Micro SD card
    1. Open a terminal prompt
    2. unzip -p 2021-05-07-raspios-buster-armhf.zip | sudo dd bs=4M of=/dev/sdc iflag=fullblock oflag=direct status=progress; sync
      1. This assumes that current working directory is the directory where the 2021-05-07-raspios-buster-armhf.zip file is.
      2. This comes from https://scribles.net/writing-raspbian-os-image-to-sd-card-on-linux/ – Thank you Max
      3. Note that the “out file” part of the DD command has a device specifier, and if you put in the wrong one, you can completely destroy your machine. Before you plug the Micro SD card in, do df -h and note the list of devices. None of the listed devices must be in your of=/dev/sdx option of the dd command.
      4. For example: I do df -h and I see a bunch of /dev/sda and a few /dev/sdb listed. This tells me that I must not use either of those for my outfile device. In my case, it happens to be /dev/sdc for my outfile device.
      5. My machine does not have a built in Micro SD slot. I bought an external USB dongle that lets me plug in memory cards. When I plug the dongle in, then /dev/sdc becomes an available device. This is correct – I don’t want dd overwriting any of my permanent devices.

After the image is copied to the Micro SD card, I can (software) eject the removable device, plug it into the Raspberry Pi and power on the Pi. After first boot, there are security updates to install; but after that I’ve got a virgin machine with bog standard software on it. Development is ready to begin!

First, create a couple of aliases

echo "..='cd ..'" > ~/.bash_aliases

echo "ll='ls -l'" >> ~/.bash_aliases

Finally, sed to edit /etc/inputrc

sudo sed --in-place=.original -e '/^# "\\e\[5/ s/# //' -e '/^# "\\e[\6/ s/# //' /etc/inputrc

Okay, so what does this do?

sed is the stream editor that let’s one search and replace inside files.

sudo is the command that grants permissions in Raspbian to edit files in /etc

--in-place=.original is the sed option to copy the file to a new name, first, before editing the file. The file will named the same as it was before, but with .original appended to the file name. I could just have easily used .backup or .bak

-e is the sed short form of --expression

It tells sed that what is between the single quotes is an expression to process. Note that the above command line has two expressions.

The two expressions are almost identical. Let’s break the first one down:

/^# "\\e\[5/ s/# //

First is a search, designated by the first /…/ pair. Second is a substitution, designated by the /…/…/ triplet (but the reason it is a substitution is because of the s in front of the triplet).

In this case, what we are searching for is:

^# "\\e\[5

The line in /etc/inputrc literally has this:

# "\e[5~": history-search-backward

You can see that the sed search expression ^# "\\e\[5 differs from the actual line by the leading carat character ^ and a couple extra backslashes.

That leading ^ (carat) is the instruction to sed that the match only works at the beginning of a line. First find the beginning of a line, then proceed.

The next few characters are just an exact match: # "

The line in inputrc has \e following the double quote mark; but, because this is going to processed on a command line (or by a shell script), we have to warn the command line interpreter that there is an actual backslash in the expression. Backslashes are normally an “escape” character on the command line, warning that “whatever you would normally do with the next character that you see, don’t do it. Even though the character is in the list of special characters that get special handling, just ignore that and deal with it as if it were not a special character”. Which is a long way of saying that to deal with \ in the data, we need to specify \\ in the script or command line.

Same thing applies with the [ character; it is normally a special character. The inputrc file has e[5 in the data, so we need e\[5 in the expression.

So the search part within the first two slashes matches when there is a beginning of a new line, a pound sign character, a space character, a (double) quote character, a backslash character, the letter e, an (opening) square bracket character, and the numeral 5.

The substitution part of the expression matches on the pound sign character and the space character, and replaces them with nothing. That’s what s/# // does: “s” in front of the slash triplet says we are going to do substitution, what to substitute are the quote and space, and what to substitute them with is the empty set: between the second and third slashes is nothing.

The second expression differs from the first by 6 instead of 5. This is from Changing the history search keystrokes

Easy button:

update_system_defaults.sh

(One should always look at the contents of the file instead of just blindly running it)

Leave a Reply