Raspberry PI OS setup bash script example
I like working with Raspberry PI single board computers, not so much when it comes to making hardware projects but actually using them as a replacement for what would otherwise be an energy hogging desktop computer when it comes to getting work done.
Anyway when it comes to using a Raspberry PI in general I often find myself re-imaging sd cards a lot, and each time I do so I need to setup everything the way that I want it again which can get a bit annoying. I have to set a background image that pertains to a certain use case for the OS image of the sd card so I know right away what kind of setup I am dealing with for example. That is that I like to have a single background image, or set of background images that I would want to have on the screen to let me know that the current sd card that I am using is an OS image that is setup for getting work done rather than doing something fun, or experimental. Another thing that I often like to change is the value of the \$PS1 variable that is set in a bashrc file, so I have a custom command prompt each time I open a new terminal window as I do not like the default one with a clean Raspberry PI OS install.
In this post then I will be going over some bash scripts that have to do with a setup script that will automate this process of setting up a new sd card just the way that I like it. There is a main setup.sh file that can be called that will run over each part script that preforms some kind of task such as copying over and setting a background image, or backing up and creating a custom bashrc file at the home path. However if I do things the way I that they should be done each part script should also work on its own if I just want to do one little thing in this collection of setup scripts.
My Raspberry PI OS bash script setup file, and what to know first
This is a post on the state of a bash script that I use to setup a Raspberry PI OS image on an sd card after imaging it from one of the image options that can be found on the Raspberry PI foundation website. I assume that you know enough to get that far at least when it comes to getting started with a clean OS Image. This post is also not a getting started type post with bash scripts in general as well, I have wrote a post to that effect before hand. This is then a slightly more advanced post then when it comes to what the next step is once one starts to learn the basics of bash scripts which is of course to start using bash to automate work that would otherwise have to be done manually, such as all the various actions that need to be preformed to set up an OS image the way that one likes it, which is what this post is about.
- Full source and other assets are up on Github
All of the scripts as well as additional assets and notes can be found in a folder in my demos Linux Github repository.
1 - The main setup.sh script
This is the state of the main setup.sh script that I have at the root of the project folder which I would call in order to run the full setup process. When calling the setup script from the command line the first argument that I give is the setup type which I default to ‘work’. The other options are ‘exper’ which means experimental as in setting up and OS image to try something out that might end up messing up the OS image such as building and installing custom software, and ‘play’ which means installing software like retroarch.
|
|
2.1 - The background image part1.sh
This is the part script that has to do with copying over a background image that is in the project folder, over to the home folder location. On top of that the only other thing this script does is also set that image that gets copied over to the current desktop background image, and also set any other additional values such as the wallpaper mode such as ‘center’ or ‘fit’.
Copying over the image file from the project folder to a home folder location is simple enough I just need to use the Linux cp command to do so and there is not much to wrote about when it comes to using that one. I just need to have the path to the source image as the first argument, and then the location to copy to as the second argument. Things might just get a little tricky when it comes to setting the image that is copied to the home folder as the background image from the command line. In Raspberry PI OS the pcmanfm command is what I can use to set that image as the wallpaper, while I am at it I can also use this command to set the wallpaper mode as well. There are a few options for this, but for my set of setup scripts I just went with fit mode. The main options for pcmanfm are –set-wallpaper which is what to use to set the location of a file to use for the wallpaper, and then there is also the –wallpaper-mode option to set the mode to something like the ‘fit’ mode.
|
|
2.2 - The basrc file script part2.sh
For part2 of my setup scripts there is the question of the bashrc file which is one of many little files throughout a Raspberry PI OS image that I would like to customize at least a little. This is a script is used for a wide range of things that have to do with setting up a terminal session such as how to format the bash prompt, setup aliases for lengthy commands that I type often, and much more. A default bashrc file is setup for the pi user in a clean Raspberry PI OS image, so part of the process of writing my own bashrc file would involve renaming that file to something like .bashrc_backup before writing over the file. So then the general idea of this script is to just backup any bashrc file that might all ready be there, and then create a copy of a source file writing over the bashrc file with that source.
|
|
I opted to use the Linux cat command with redirection over the cp command for writing over the bashrc file as I was thinking in terms of doing something more than just copying the file. However as of this writing I am not doing anything more than that, so I could change that back to just using the cp command if I can not think of
3 - Conclusion
The setup script thus far seems to work okay when it comes to the parts that I have authored thus far. Of course I have plans for additional parts, and in time I might want to make this a stand alone repository rather than a part of my demos Linux repository.