The goal of this script is to automate tasks that are commonly carried out after the operating system has been installed. Before creating this script, we will first need to determine... what these tasks are and which commands can be used to accomplish them. After which, they can be combined and saved to a file, then run as a script.
We'll also need to create a few directories and files that tell the script which packages need to be installed or removed from the system, .bashrc alterations and configuration notes for packages that need to be configured manually in a GUI interface that cannot be done within the script itself.
These directories and files will be created on a USB drive called POST-INSTL. They will be used to indicate to the script which programs need to be installed or removed from the system. Each of these files will be text files which list these applications.
Use the following commands to create the required directories. The directoires will hold doumentation for applications, lists to indicate which applications to install/remove and for the script itself.
Commands
The following commands can be used
/media/$USER/POST-INSTL/ | path the the USB drive |
docs | hold documentation for packages/system |
lists | hold lists that will indicate which packages are to be removed or added |
scripts | hold the scripts |
system | hold documentation for system resources |
packages | hold documentation for installed packages |
Use the following commands to create the required files. These files will include the script itself and be used by the script to get the names of the packages to be installed/removed
Commands
The following commands can be used
/media/$USER/POST-INSTL/ | path the the USB drive |
post-install.sh | the script itself |
apt.list | list of repository packages to install |
bloat.list | list of packages to remove |
List of common tasks with their corresponding commands. After the commands for each of these tasks have been found and proven, the script can be written using these commmands.
The first thing the script will need to do is to get the path to the drive containing the files that will be copied to the new home directory. This can be done with a user input asking to provide the path. This path will then be stored in a variable called BACKUP.
Commands
The following commands can be used
/media/$USER/BACKUP/home/ | most likely path |
BACKUP | variable to store the path |
The following files are often changed or modified and should be backed up before doing so. It is typically good practice to do this, soon after the operating system has been installed.
Commands
The following commands can be used to back up these files. A file will be created with a .bak extension in the same directory containing the file. If these files are misconfigured in the future... they can easily be recoved by deleting the misconfigured copy and removing the .bak extension from the backup.
.bashrc | shell user interface configuration file |
.profile | profile configuration file |
sources.list | package manager configuration file |
This step is intended to remove programs that may be on the system by defualt that are not required. To remove them, a text file can be created containing a list of programs to be removed. When the script is run... it will read this list and remove all the programs on it.
Commands
The following commands can be used
POST-INSTL | name of the USB containing the bloat list |
/media/$USER/POST-INSTL/ | path to USB containing the bloat list |
bloat.list | name of the list |
Populate home directory from backup.
Commands
The following commands can be used
$BACKUP |
variable containing path to the backup drive obtained from user input in the 1st step |
/home/$USER/ | path to the new home directory |
Install packages from list
Commands
The following commands can be used
POST-INSTL | name of drive with script files and lists |
apt.list |
name of repository package list packages to be installed |
Install .deb packages from .packages directory
Commands
The following commands can be used
sudo dpkg -i | install .deb packages |
.packages |
name of directory holding .deb packages all packages in this directory will be installed |
Add to the file
Commands
The following commands can be used
POST-INSTL | name of USB drive |
bashrc_extras.txt |
text file containing .bashrc alterations contents of this file will be appended to ~/.bashrc |
Due to the fact most of these configurations are typically done through a GUI interface and cannot be done within the script... we can provide notes that may have been taken on how to configure these applications
Commands
The following commands can be used
ls | list storage of... |
POST-INSTL | name of USB drive |
packages | directory containing notes for installed packages |
Now that the system has been updated and all necessary packages have been installed... all that is left to do, is to clean up the system. This will remove any left over packages that are no longer required and older kernels that may have been replaced during the initial upgrade.
Commands
The following commands can be used
autoremove | remove unused packages and old kernels |
Now that all of the directories and files have been created and the commands have been found and proven for each required task... all that is left to do, is to write the script using these commands. The following script, can be copy and pasted into a text editor, saved, edited, then run.
To write the script... all that is left to do is to copy and paste all of the commands found above and add some fluff to make it presentale to the user (us) when the script is run. Shown below is an example. This example can be copy and pasted into a text editor and saved.
The very 1st line of the script points to where bash is installed on the system. If this is different in your case, the following command can be used to find this path.
Script example
example shown below
#!/usr/bin/bash # welcome echo "" echo "welcome to post-install.sh" echo "" sleep 2 # 0. get the backup path echo "Enter the backup path below [/media/$USER/BACKUP/home/]" read BACKUP sleep 2 echo "" echo "backup path provided: $BACKUP" sleep 1 read -p "<enter> to continue" clear echo "" # 1. backup '.bashrc', .'profile', 'sources.list' echo "backing up important dot-files..." sleep 2 sudo cp ~/.bashrc ~/.bashrc.bak sudo cp ~/.profile ~/.profile.bak sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak echo "" echo "done." read -p "<enter> to continue" clear echo "" # 2. remove system bloat echo "removing system bloat..." sleep 2 sudo apt purge -y $(cat /media/$USER/POST-INSTL/lists/bloat.list) echo "" echo "done." read -p "<enter> to continue" clear echo "" # 3. add home directories and dot files echo "adding home directories..." sleep 2 cp -r $BACKUP/. /home/$USER/ echo "" echo "done." read -p "<enter> to continue" clear echo "" # 4. install repository packages echo "adding repository packages..." sleep 2 sudo apt update sudo apt install -y $(cat /media/$USER/POST-INSTL/lists/apt.list) echo "" echo "done." read -p "<enter> to continue" clear echo "" # 5. install local packages echo "adding local packages..." sleep 2 sudo dpkg -i ~/.packages/*.deb sleep 2 echo "" echo "adding missing dependencies" echo "" sudo apt install -f echo "" echo "done." read -p "<enter> to continue" clear echo "" # 6. configure '.bashrc' echo "adding .bashrc configurations" sleep 2 cat /media/$USER/POST-INSTL/docs/system/bashrc_extras.txt >> ~/.bashrc echo "" echo "done." read -p "<enter> to continue" clear echo "" # 7. configure installed packages echo "configure installed packages..." sleep 2 ls /media/$USER/POST-INSTL/docs/packages/ echo "" echo "refer to documentation provided above" read -p "<enter> to continue" clear echo "" # 8. cleanup the system echo "cleaning up the system..." sleep 2 sudo apt autoremove echo "" echo "done." read -p "<enter> to continue" clear echo "" # exit echo "system configuration complete!" echo "" read -p "<enter> to exit" sleep 2 echo "goodbye" sleep 2 exit
Before the script is run, we will first need to edit the files that were created in prevous steps. Such as apt.list, bloat.list and bashrc_extras.txt. Examples shown below
lists/apt.list | list of repository packages to be installed |
lists/bloat.list | list of packages to be removed |
docs/system/bashrc_extras.txt | addional commands to be appended to the .bashrc |
docs/system | put system configuration notes here |
docs/packages | put package configuration notes here |
lists/apt.list
Example of file contents
lists/bloat.list
Example of file contents
docs/system/bashrc_extras.txt
Example of file contents
# prompt (variables) red=$(tput setaf 95) navajo=$(tput setaf 144) green=$(tput setaf 118) bold=$(tput bold) reset=$(tput sgr0) # prompt (construct) PS1="\n"; PS1+="\[${bold}\]"; PS1+="\[${red}\]fossman"; PS1+="\[${navajo}\]@"; PS1+="\[${red}\]\h"; PS1+="\[${navajo}\] [\w]"; PS1+="\n"; PS1+="\[${navajo}\] > "; PS1+="\[${reset}\]"; # Custom Variables STACK='/media/$USER/MAIN-STACK/' RESOURCES='/media/$USER/MAIN-STACK/Resources/' # Environment Variables (Bash and Python) export PATH=$PATH:~/Modules/Bash export PATH=$PATH:~/Modules/Python export PATH=$PATH:~/Modules/Python/Drivers
Finally... it's time to run the script.
Use the command shown below to launch the script. Due to the fact absolute paths and system variables were used in place of usernames, it can be run from anywhere in your terminal.
Run script
The following command can be used
POST-INSTL | name of USB drive containing script and files |
post-install.sh | name of script |
Post actions
Reboot when finished