How To Write Linux Shell Scripts (Part 1)

Posted by jun auza On 2/01/2013
If you are a Linux user, I’m sure you must have come across the command line using your favorite terminal emulator. You enter a command in the BASH shell and it gets executed, thus giving you the desired output. As simple as that seems, remembering the huge number of commands that are there for Linux becomes a rather tedious tasks. Also, let’s say you needed to type a set of commands in succession, about thrice a day; it becomes even more tedious for the user. This is where shell scripts come in handy.


Introduction to Shell Scripting:

What are shell scripts?

Shell scripts are a collection of commands that you store in a file. Once you execute this file -- remember that everything in Linux (that is not a directory), even a program, is a file  -- the shell reads the commands in it and executes them. So, next time you have a set of commands in a file, simply executing the file in a shell will make those commands run one by one.

Why do we need shell scripts?

Shell scripts make our job easier. They save us valuable time and drastically reduce our effort. One of the best advantages of shell scripts is that they let us automate tasks in Linux. That is why you’ll see the most efficient system admins are the ones who do the least work.

Do I need to know programming to get started?


No, you don’t need to know programming to learn shell scripting. That said, a little background in coding would be great. And, it is important that you are familiar with the command line and know at least the basic commands used in BASH. (Bash is the shell, or command language interpreter, for the GNU operating system).


Getting started: Writing your first shell script

Now, open your favorite text editor. Here we’ll use gedit since it is the default text editor in Ubuntu. Type in the following code in the file:

#!/bin/bash
## Script that greets the user, prints the date
clear
echo “Hello World!”
echo "Welcome to shell scripting, $USER"
echo -n "Today is  ";date


Now save the file as helloworld.sh in your home directory.

Next, open up the terminal and type in the following commands:

$cd ~
$chmod +x helloworld.sh
$./helloworld.sh


Now, what you’ll see next is something like this:

“Hello World!”
Welcome to shell scripting,
Today is Wed Dec 19 09:45:29 IST 2012



Code Explained:

Now, let’s go back to the script once again.

In the first line, that is #!/bin/bash, you told the command line to use the BASH shell (http://www.gnu.org/software/bash/manual/html_node/What-is-Bash_003f.html ). !# is usually pronounced as shebang.

The clear command then clears the screen. (Same as Ctrl + L) on the command line.

echo “Hello World!”  - The echo command prints, whatever you type in quotes, to the command line. So, you’ll see Hello World! in the output.

In the next line, that is:

echo "Welcome to shell scripting, $USER"

We tell the script to print the phrase along with the user name. The $USER is a variable that stores the username. Don’t worry if you don’t know what variables are, we’ll come to that later. For now, just remember that $USER prints out the current user’s username. In fact, try typing the following command in the terminal right now:

$echo $USER

It will print out your username.

Finally, the last line prints out two commands at once.

echo -n "Today is  ";date

We tell echo to print “Today is “ in the terminal and then, on the same line we print today’s date. The semicolon in between ensures that both commands are run in succession. The -n argument, on the other hand, makes sure that the output of both the commands is printed on the same line. So, if you had not used -n, you would have got “Today is” and the date on different lines.

Now that you got a fair idea of how to write your first shell script, give yourself a pat on the back. Remember, every time you get stuck, try typing (or even copying and pasting) the commands in the shell script in the terminal and executing them one-by-one.

That’s it for today. We’ll continue our journey on writing shell scripts in part 2.


Written by: Abhishek, a regular TechSource contributor and a long-time FOSS advocate.

Bookmark and Share

Related Posts:



Archive


"Action is the real measure of intelligence" ~Napoleon Hill

Twitter

Tumblr

Google +