Bash it! Bop it! Script it!

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/

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:

#! /bin/bash
# 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.

If you’re looking to learn more about how to write complex scripts, I highly recommend these two guides:
Advanced Bash-Scripting Guide
Writing Shell Scripts

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:
mkdir ~/bin
cd ~/bin

now, invoke your favorite text editor to open a new file, and start entering in the code:

nano sexrobot

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.
nano ~/.bash_profile

Go to the end of the file, and add the following line:
export PATH="$PATH:~/bin"

Reload the bash_profile settings:
. ~/.bash_profile

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:
cd /tmp

In Conclusions

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!

3 thoughts on “Bash it! Bop it! Script it!

  1. Hey there.

    Nice informative HowTo – and thanks for thinking to put the link to the advanced bash-scripting guide.

    Elsewhere you mentioned DreamHost (I am an ex-hostee of theirs too); I notice that you talked about SliceHost, but their costs are exorbitant compared with, say, HostUpon’s shared setup.

    I’ve never noticed any benefit from having VPS versus shared (I had a VPS server in the past… maybe my traffic was not enough to warrant it at the time), but $70 a month for bugger-all versus $4.95 a month for unlimited space AND bandwidth, I can’t see anything but upside for the shared solution.

    Do you have a take on this? My full site is almost entirely mySQL driven, with a DB approaching 300Mb – stock and commodities research is data-intensive – and PHP scripts for presentation and what-not < 20Mb.

    Like you, I am just an enthusiastic amateur when it comes to all this web admin malarkey; my blog is just a little bit of vanity publishing and the site is mostly to show off.

    Now I have to figure out how to write a bash script to run 6 PHP scripts successively rather than crontab-ing them, since

    (1) the overall job starts at 4:22 NY time and must be finishe by 4:30 NY time;
    (2) the scripts are hierarchical (script 2 and 4 require scripts 1 and 3 to have finished), and
    (3) two of them sometimes take up to 4 minutes to complete.

    Doubtless such a thing is child’s play for anyone with the least amount of scripting ability… it might even be as simple as

    wget -q /path/to/script1
    wget -q /path/to/script2
    wget -q /path/to/script3

    If not I am in trouble!



  2. Let me start by saying any help is greatly appreciated! I’m an admitted command line/batch file novice, but thought I would attempt what I imagined was going to be a relatively simple task. I’m running a program on a Windows 7 machine (runs at start up) which in order to function the way I need, requires me to open a command prompt and enter the string “notify C:\Users\Account\directory. I’ve been attempting to write a .bat file to also run at start up so as to automate the task for me. I have not been able to achieve my desired result. Thanks in advance! -Frank

Leave a Reply