Installation scripting guide
GSoC Week 6–7@SCoRe Lab
Interesting developments this week!! It started small and led to a completely different output.
As stated in the earlier blog, I had to write an installation script. Installation steps at this point of time are as follows:
- clone the repository
- create a virtual env
- install pip requirements
- copy service file in its directory
- get the service up and running
User would clone the directory and then run setup.bash. Lets start with scripting now. I would give you a gist of all the concepts used and combine them all to write and execute our script.
Building Blocks🧩
Unix has more than one possible shell: C Shell (csh), Bourne Shell (sh), Korn Shell(ksh), Bourne Again Shell (bash).
Executing v/s Sourcing a bash script
A
> ./myscript
B
> source myscript
The differences are:
When you execute the script you are opening a new shell, type the commands in the new shell, copy the output back to your current shell, then close the new shell. Any changes to environment will take effect only in the new shell and will be lost once the new shell is closed.
When you source the script you are typing the commands in your current shell. Any changes to the environment will take effect and stay in your current shell.
Read here for detailed reference.
.bashrc
The .bashrc file is a script file that’s executed when a user logs in. The file itself contains a series of configurations for the terminal session. This includes setting up or enabling: coloring, completion, shell history, command aliases, and more. The ~/.bashrc file determines the behavior of interactive shells. It gets executed every time a user starts up a fresh terminal session in interactive mode on their Linux system.
Location: ~/.bashrc
(~ Tilde indicates user home directory)
PS: You can find a skeleton of bashrc in /etc/skel/.bashrc. If different users want different bash configurations then you must put a .bashrc file in that users home folder.
Aliasing
A Bash alias is basically a shortcut you can set for a command. If you have to type some long command out often, you can shorten it or make it more memorable with a Bash alias.
$ alias start= “systemctl cnc-server start”
Now, you can just use the command start
whenever you want.
However, there’s a catch: this is only in your current shell session. Close your window, and this alias is gone.
If you plan on keeping that alias for the future, you will have to open up either your ~/.bash_profile
or ~/.bashrc
files with a text editor and save the aliases directly there. You can also organize the aliases in ~/.bash_aliases
and run that from ~/.bashrc
as follows:
if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
Putting it all together
Compilation was pretty easy, put all the commands that you would have entered while manual installation.
Additionally I used system aliasing to start, stop and view status of the service. I wrote an aliasing script and catted it to ~/.bash_aliases. Aliasing gave better accessibility, look and feel.
I had used function aliasing so I could accept arguments. Here is the code snippet of aliasing.
function cncserver {
if [ -z “$1” ];
then echo “Enter a command: start / stop”
elif [ “$1” = “start” ];
then systemctl start survey6_server
elif [ “$1” = “stop” ];
then systemctl stop survey6_server
elif [ “$1” = “status” ];
then systemctl status survey6_server
else echo “Command not supported”
fi
}
alias cncserver=cncserver
We’ll be packaging the project in the next blog :)
Happy Coding!