I would rather spend 3 hours writing a program to do a task than have to spend 3 minutes doing it myself more than once. Seriously. I’m that lazy. The only task I like to do over and over again is opening a Corona, taking a sip, filling it back up with tequila, and passing out in a pool of my own vomit. Or, as my kids like to call it, “Thursday.”
That kind of laziness means I use a lot of bash scripts to do regular tasks on my server. I have scripts to do automated backups, scripts to setup new virtual domains, scripts to prop up my fragile ego with repeated compliments, scripts to do just about every repeating task that goes into maintaining a barely functional webserver.
There are several tutorials out there for how to start writing scripts, but none of them have my flair for drunken bravado and outrageous sexual innuendo. So, I proudly present a very basic starter guide to writing a bash script:
How to Write A Bash Script
You know all those fancy commands you keep typing into the command line? Things like
cp -rv secretpr0nstash/*.avi /var/www/churchhats.com/
Well, those same commands can be stored in a file, and can be executed whenever you need to, by invoking the name of the file. Let’s start with a very basic file. It just has 3 lines:
# sexy robot script
echo "You are the sexiest robot"
The first line tells the system which shell to use when interpreting the commands that follow. The second line is a comment, to remind me in 6 months what the point of the script is. You can place these throughout the script to remind yourself why you did what you did when you wrote the thing. Finally, the third line is the actual command. It tells the system to output the given text back to the shell.
So, now what?
Where to Put It
Now, you need a place to put the script. I keep all of mine in a folder called “bin” inside of my user folder. To create the folder, type:
now, invoke your favorite text editor to open a new file, and start entering in the code:
Enter your code, save, exit, and TADA! you have your very own script to prop up your fragile ego with repeated compliments.
How to Make Go Go
Except, it still won’t run. Try it – type “sexrobot” into your command line. What happened? It mocked you, didn’t it. It told you that your crazy dreams of a sexy robot compliment did not exist.
Before we can invoke the command, we have to make it executable.
chmod 755 sexrobot
Now, I can execute the code. Try it again, with the whole location:
Now that it works, we can do the final step – add a line to our .bash_profile file, so that every time we log in, the shell goes into our bin folder to look for commands.
Go to the end of the file, and add the following line:
Reload the bash_profile settings:
If we did everything right, we should be able to execute our new script from any folder, just by typing the name of the file. Let’s try it:
Now that I don’t have to do all these repetitive server maintenance tasks, I can focus on more important things, like repetitive binge-drinking to drown my own sorrow.
Ha Ha! Alcoholism is funny!