❝ Emulate apt-get: create your own Super Cow Powers.
I'm not lazy. I can type. I just have better things to do with my time than type in trivial commands at the keyboard. That's where automation comes in. Well, not quite full automation. I'll settle for semi—automation.
I can go a number of ways here. There are many languages to choose that abstract the work suitable for multi-platforms, I can use existing open-source tools or write up the command line script I type in and use a operating / shell specific (a.k.a) throw away script. This is the path I choose.
Why semi-automate, why not do the lot? Well the extra effort to write the specific details to the nginx configuration file is not worth the pain of extra code.
What do we need to automate?
My trusty toolset today is making use of Bash shell scripting.  The task, setup of a nginx server. Here's what I want to do:
install nginx using apt-get
start the server
display the hostname
stop the server
IF first time install then
write out filepath of file to edit
write out nginx config file details:
http->server->location path for images
make directory given path and name
change ownership of directory to owner:group
start the server
reload the server
display server status
Once we have the requirements, turn them into a usable script. The way to think of this is, "automate the repeatable commands" and supply any of the changing arguments as variables. This statement is obvious, though how many times have you repeated simple tasks like this? Once, twice... ten?
The code below is basic. In fact its what I like to call, "toy code". The simple point I am trying to illustrate? A tiny bit of extra effort means you can do these routine tasks quicker and more accurately the next time.
It also saves on typing.
#====== start ======
#!/bin/sh #====== # name: si_nginx.sh # desc: semi installation of nginx with # minor editing if required #====== IS_FIRST_TIME=1 DEFAULT_CONFIG_PATH=/location/default/config/file WWW=/usr/share/nginx/www OWNER=www-data GROUP=www-data
# ------ setup, install ------
clear echo "start install" apt-get install nginx echo "server at http://",`hostname -I` echo "stop server" /etc/init.d/nginx stop
#------- config ------
if [ $IS_FIRST_TIME = 0 ]; then echo "edit time" echo "$ vim $DEFAULT_CONFIG_PATH" echo "====== multiple lines of config file here =====" echo "make directory $WWW" mkdir $WWW echo "set ownership of $WWW to $OWNER:$GROUP" chown $OWNER:$GROUP $WWW else echo ">>>>>> warning: NOT FIRST TIME SETUP? done this before? >>>>>>" fi
#------ status ------
echo "start, reload server" /etc/init.d/nginx/start /etc/init.d/nginx/reload echo "status report" systemctl status nginx.service echo "ready at http://" `hostname -I`
#===== end ======
You may notice the script runs without stopping, the first time through. A stop could be inserted. Running the script multiple times I didn't see it worth the extra effort. Also notice there is no mention of sudo. I run the sudo command on the actual script.
 Bash Intro HOWTO #11
[Last accessed: Tuesday 2nd February, 2016]