UNIX Tutorials, Tips, Tricks and Shell Scripts

UNIX Shell Scripting Tutorial - The Basics for Beginners...and Beyond!!!


Writing a Shell Script

A UNIX shell script is a human-readable text file containing a group of commands that could also be manually executed one-by-one at the UNIX operating system command prompt. They are often kept in a file (script) because they are too numerous to type in at the command prompt each time you want to perform a specific task, or because together they form a complex computer program.

[If the information in this UNIX shell scripting tutorial is too basic for you, check out our post 7 Key Concepts to Start with if you want to Learn UNIX Shell Scripting.]

The vi program, or any one of the many UNIX text editors out there, can be used to create a shell script and save it to disk for subsequent execution. Unlike other high-level programming languages, such as C or C++, shell scripts do not need to be compiled into a binary format for execution. Because of this, programming syntax errors are not revealed until the script is executed.

The following is an example of a basic shell script:

#!/bin/ksh

# This is a comment on a line by itself

clear # This comment is sharing the line with the clear command

echo "Text Line 1"

print "Text Line 2"

exit 0

The first line of this sample script is the shell definition line.




The Shell Definition Line

The shell definition line tells the system what program (shell) should be used to interpret the script's commands, and where the program (shell) is located. For the example shell script above, the shell definition line tells the system to use the Korn Shell (#!/bin/ksh) when executing this script.

Since shells may differ in syntax, a script executed successfully using one shell may fail when a different shell is used to run it. For example, a Korn Shell script (ksh) may not execute correctly if it's executed with the Bourne Shell (sh) because the latter does not understand all of the syntax used by the former.

This line MUST appear on the first line of a script. If it appears on any other line it will be treated as a comment.

There are a couple of things related to the shell definition line to consider if you would like to run your shell scripts on another version of the UNIX operating system. The first is that the other operating system may or may not support the shell definition line. The other is that the location of the program or shell (e.g., the /bin directory for /bin/ksh) may be stored in a different location on the other system.


### PLEASE READ AN IMPORTANT MESSAGE FROM OUR FOUNDER ###

The time has come to END the social experiment known as Facebook.

It is breaking our world and is hurting people.

(1) People are dying as the result of genocide (ie. the Rohingya in Myanmar), bullying and mental health problems enabled by Facebook

(2) People's behavior, including yours if you use Facebook, is being manipulated by Facebook's subjective Newsfeed algorithm

(3) Families, communities and countries are becoming more divided, contrary to Facebook's stated mission of building community

READ MORE >>


Shell Script Comments

Comments, or non-command code, in a shell script begin with the # (pound) character and can be used for different purposes. They may be used to document what the overall purpose of the script is, to describe what each line of program code does, to track modifications made to the script and who made them, or for a number of other purposes.

The comment character (#) may appear on any line of the script, may appear by itself on a line, or may follow actual program code. Both of these lines from the example shell script are valid program comments:

# This is a comment on a line by itself

clear # This comment is sharing the line with the clear command

As the text in the comment lines indicate, the first comment does not share the line with anything else, and the second comment shares a line with the UNIX clear command.


Displaying Output

The example shell script above shows two methods for displaying output to standard output:

echo "Text Line 1"

print "Text Line 2"

Many people are familiar with the echo command, but the print command may be new to them. The print command is the replacement for the echo command. You should use the print command when you are writing shell scripts because it is more powerful than the echo command, and its syntax has been standardized on multiple operating systems.

The print and echo commands are often used to display interactive messages to the person running the script, write informational or error messages to a log file, or to write data to a data file.




Exiting a Shell Script

All shell scripts should be terminated with the exit command:

exit 0

The word "should" is used here because a script will run successfully without including it, but it is good to get in the habit of including exit in your scripts.

You will notice that the exit command is passed an argument of 0 (the number zero). Most UNIX commands and programs will return a number, called the return code, to the parent process.

It is common practice to return a 0 (zero) if the command or program completed successfully, and a non-zero number if it did not. The non-zero number will in most cases be a 1 (the number one), but may be another non-zero number to give a more specific indication of why the command or program failed.

NOTE: If a number is not specified with the exit command, the return code of the last command executed in the script will be returned to the parent process. If 0 (zero) was not specified in the example script, the return code for the print command would be returned to the parent process.


How to Run a Shell Script

Different methods can be used to run (execute) a shell script. You can run the script in the current shell, or you can spawn (create) a new shell to run the script in.

If you run a script containing the exit command in the current shell, you will be logged out of the system when the script executes the exit command.

Before you can run the script, you need to make the shell script file an executable by using the UNIX chmod command. The following command will allow only the user (owner) of the script to execute it:

$ chmod u+x script1

If you wanted to allow everyone (all) to execute the script, you would use this command:

$ chmod a+x script1

After the script file has been made an executable with the chmod command, you can run the script in a new shell by giving the path to the script:

$ ./script1

This (./) would be the path to script1 if you are in the same directory as the script. You can optionally run the shell program and pass it the script name as an argument:

$ /bin/ksh script1

This command also indicates that we are in the same directory as script1 because the path to the script is not specified. If you were in a different directory than the script, you could use one of the following commands to run it:

$ /home/student1/script1

or

$ /bin/ksh /home/student1/script1

Remember that all of the methods used above to run the script will spawn (create) a new shell to run it in. Running this sample script will produce the following output:

Text Line 1
Text Line 2

Do you need to learn UNIX shell scripting and get practice writing & running scripts...on a REAL SERVER? If you are ready to move past the basics, either of these online courses is a good place to start...

UNIX and Linux Operating System Fundamentals contains a very good "Introduction to UNIX Shell Scripting" module, and should be taken if you are new to the UNIX and Linux operating system environments or need a refresher on key concepts.

UNIX Shell Scripting is a good option if you are already comfortable with UNIX or Linux and just need to sharpen your knowledge about shell scripting and the UNIX shell in general.

Both courses include access to an Internet Lab system for completing the course's hands-on exercises, which are used to re-enforce the key concepts presented in the course. Any questions you may have while taking the course are answered by an experienced UNIX technologist.

Thanks for reading, and happy scripting!!!