Tutorial

How To Write a Simple Shell Script on a VPS

Published on August 9, 2013
author

Henrique Pinheiro

How To Write a Simple Shell Script on a VPS

Introduction

This tutorial is aimed at teaching you how to write shell scripts for the most variety of purposes. Shell scripts can be used to run multiple commands, a single command with difficult and extensive arguments, or more user friendly interfaces for distributing your work. Essentially it makes your life easier by automating stuff you’d have to do manually without it.

Setting Up a Folder

Before you start writing your shell script, it’s a good practice to designate it a folder. It’s recommended that, for personal scripts, you do it over at ~/bin. To create that folder, run:

mkdir ~/bin

And to make scripts run from anywhere in the system, edit your /etc/profile by running:

sudo nano /etc/profile

Then add the following lines to the end of the file:

PATH=$PATH:$HOME/bin
export PATH

Remember to CTRL+O to save and CTRL+X to exit. You can make the changes go into effect by running:

source /etc/profile

If your Linux distribution doesn’t support the source command, you can also reboot the VPS by typing:

sudo reboot

Creating a File

To start your shell script, you’ll need to create an executable file. This can be easily achieved by running:

touch ~/bin/firstscript
chmod +x ~/bin/firstscript

Open the nano text editor to start adding commands:

nano ~/bin/firstscript

For the program loader to recognize this executable file as a shell script and run commands from the right directory, you have to add the following line to the top of the file:

#!/bin/sh

And you’re ready to add whichever Linux command you wish, such as:

clear
echo “Hello World!”

After saving (CTRL+O) and exiting (CTRL+X) nano, to run your script, simply type in:

firstscript

from anywhere in your system. The result should be something like this:

Test script running

Example Script

One of the main points of shell scripts are making shortcuts for repetitive tasks. For example, if you're moving a lot of files to your ~/backup folder, you could setup a script that would simply drop anything you specify. The way it would work is by typing:

filebackup file-name1 file-name2...

Then, when you need it, it’ll be there. Before we start coding, let’s take a look at what you’ll need to learn. Well written shell scripts are not hardcoded. That means, on this example's scope, if you want to change your backup folder, you can easily do so by only changing one of the first lines in the script. Yes, the variable that corresponds to it will only be referenced once, but it’ll really help you later if you get used to do it now. To test this, you won’t need to jump into the text editor, do it straight from the command line by typing:

testvariable=teststring

The “echo” command outputs text. By running:

echo $testvariable

You’ll be able to see the value you set for it, which in this case is “teststring”. Now you can start coding it by doing the usual.

touch ~/bin/filebackup
chmod +x ~/bin/filebackup
nano ~/bin/filebackup

Remember, any given line that starts with a ‘#’ is a comment. It won’t impact your program in any way, except when it’s followed by an exclamation point at the first line of your program, which then turns into a “shebang”, as explained earlier when the “#!/bin/sh” line was introduced. This is what the script could look like:

#!/bin/sh
#Backup script
#Description: makes a copy of any given file at the backup folder
#Author: Your Name
#Date: 8/10/2013

#Backup folder; set this variable to any folder you have write permissions on
BACKUPFOLDER=~/backup

#The script will make sure the folder exists
mkdir -p $BACKUPFOLDER

#Now the script will copy the given file to the folder
cp -a $@ $BACKUPFOLDER

Now, after you’ve saved (CTRL+O) and exited (CTRL+X), let’s review the code. The first few lines were only comments. We then specified with a BACKUPFOLDER variable where we wanted our files backed up. We proceeded to run “mkdir -p $BACKUPFOLDER”. What that does is that it creates the folder, but doesn’t give out any errors if it already exists. On the next command, the “cp” one, we placed every argument that proceeded the call for the script with "$@". Arguments on this context are all the filenames you place after the script is called to be backed up. Just after that there's the destination folder, in this case "$BACKUPFOLDER". You can now test your script by going to any folder on your system with a couple of files and running:

filebackup file1 file2

You can add as many files as you want to that line and they’ll all be copied to the backup folder.

Conclusion

Shell scripts are everywhere on Linux systems, and it’s for a reason. They’re extremely useful and possibilities are incredibly high. This tutorial only covers the basics, there is much more to learn.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products


Tutorial Series: An Introduction to Shell Scripting

This series covers all of the basics of writing shell scripts. It begins by introducing the reader to shell scripting and takes them all the way up to writing conditional statements.

About the authors
Default avatar
Henrique Pinheiro

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
4 Comments


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

I agree with dan, bash scripting is so useful. I started learning bash when i came across Centmin script. With what I learnt for bash scripting I now wrote and extended/modified version called Centmin Mod http://centminmod.com and have wrote countless bash scripts to automate mundane server administration tasks, from backing of data to installing scripts etc.

Bash scripting is a must learn skill !!! Will save you time in the long run !

Many people underestimate the power of bash, and often resort (as I used to) to using higher level languages - perl, python, php, for doing something that bash is more than capable of doing itself. cut, head, tail… sed and awk are so powerful - you just have to get around the quirks that they have with regex - some of it’s not exactly standard, or has other flags, options, or ways of functioning.

Since learning how to bash script, I find now I don’t have to inconvenience myself with having to use something like perl and using “system()” a lot, or backticks to capture the command’s output - bash has that all built in and is more than capable of doing everything perl is.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
August 24, 2013

Thanks for the feedback @Morthawt! :] Knowing how to write a bash script is really useful later on.

Very nice intro. I have not gotten into linux scripting yet as I am more of a windows user and as such I use Autoit. But some day I am going to need to script in Linux and this will be a nice foundation to have somewhere to begin from and not feel as “lost” when starting.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.