image
Shell Scripting with Linux

Contents

Shell Scripting
Shell Scripting Part 1
Difficulty
Intermediate
Duration
16m
Students
487
Ratings
4.3/5
starstarstarstarstar-half
Description

In this course, you'll learn exactly what scripts are, the components that make up a script, how to use variables in your scripts, how to perform tests and make decisions, how to accept command line arguments, and how to accept input from a user. This course is part of the Linux Administration Bootcamp learning path, designed to get you up and running with Linux.

Learning Objectives

In this course, you'll learn how to:

  • Use variables in your scripts
  • Perform tests and make decisions
  • Accept command-line arguments
  • Accept input from a user

Intended Audience

  • Anyone with basic knowledge of Linux who wants to learn more
  • Professionals who want to learn more about Linux to enhance their career prospects

Prerequisites

This is an intermediate-level course so some knowledge of Linux is expected. If you're just starting out, then try our Linux Fundamentals course first.

Transcript

In this lesson, you'll learn exactly what scripts are. The components that make up a script. How to use variables in your scripts. How to perform tests and make decisions. How to accept command line arguments, and how to accept input from a user. A script is a command-line program that contains a series of commands. The commands contained in this script are executed by an interpreter. In the case of shell scripts, the shell acts as the interpreter and executes the commands listed in the script one after the other. Anything you can execute at the command line, you can put into a shell script. 

Shell scripts are great at automating tasks. If you find yourself running a series of commands to accomplish a given task, and you need to perform that task again in the future, you can and probably should create a shell script for that task. Let's look at a simple shell script. Before you can execute the shell script, make sure that it has the executable bits set. So, we can chmod at 755 for instance. If the script is in your path, you can simply type in the script name and it'll execute. If it's not in your path and you'll need to give it a relative or full path for your script.

In this case, we'll pretend that I'm sitting in my home directory and this is a script that I just created and I wanna execute it out of my home directory. My home directory is not in my path, so I'll do ./ in the script name to execute it. We'll notice that the first line of this group starts with a pound sign, then an exclamation mark, and then that is followed by the path to the bash shell program.

The number sign is very similar to the sharp sign used in music notation. Also, some people refer to the exclamation mark as a bang. So, pound sign exclamation mark can be spoken as sharp bang. The term shebang is an inexact contraction of sharp bang. When a script's first-line starts with a shebang what follows the shebang is used as the interpreter for the commands listed in the script. Here are three examples of shell scripts, each using a different shell program as the interpreter. When you execute a script that contains a shebang, what actually happens is that the interpreter is executed and the path used to call the script is passed as an argument to the interpreter. You can prove this by examining the process table.

Let's start this script called sleepy.sh in the background and look at the process table. You can see that what is actually running is /bin/bash space ./sleepy.sh. Let's use a full path to the script. In this case, /bin/bash has been executed with the path to sleepy.sh, which is /tmp sleepy.sh. Also, you can see that bash is executing the sleek command, which is the first and actually only command in this shell script. If you do not supply a shebang and specify an interpreter on the first line of the script, the commands in the script will be executed using your current shell. Even though this can work just fine under many circumstances, it's best to be explicit and specify the exact interpreter to be used with a script.

For example, there are features and syntax that work just fine with the bash shell that won't work with a SeaShell. Also, you don't have to use a shell as the interpreter for your scripts. Here's an example of a Python script named hi.py. If you're interested in Python programming and scripting, you can see my book 'Python Programming For Beginners.' You can find more information about it at linuxtrainingacademy.com/python-book.

Let's get back to shell scripting. You can use variables in your shell scripts. Variables are simply storage locations that have a name. You can think of variables as name value pairs. To assign a value to a variable use the syntax: VARIABLE_NAME=Value. Make sure not to use any spaces before or after the equal sign. Also, variables are case sensitive and by convention variable names are in all uppercase. To use a variable, simply proceed the variable name with a dollar sign. You can also enclose the variable name and curly braces and proceed the opening brace with a dollar sign. The curly-brace syntax is optional, unless you need to immediately precede or follow the variable with additional data.

Here's an example. Let's say you want to add the letters ING to the value that's stored in the MY_SHELL variable. To do this, you would use dollar sign, curly brace, the variable name, closing curly brace, and then I-N-G. If you do not encapsulate the variable name and curly braces, the shell will treat the additional text as part of the variable name. Since a variable would that name does not exist nothing is put in its place as in this example. You can also assign the output of a command to a variable, to do this enclose the command in parentheses and proceeded with a dollar sign.

In this example, the output of the command host name is stored in the variable server name. In this simple output the server name is linuxsvr. You can also enclose the command in back techs. This is an older syntax that is being replaced by the dollar sign parentheses syntax. However, you may see this in older scripts. Variable names can contain letters, digits, and underscores. They can start with letters or underscores, but can not start with a digit. Here are some examples of valid variable names followed by some examples of invalid variable names. The valid variable names have a mixture of uppercase and lowercase letters and the underscore and digits, all that is fine.

In the invalid case, you see a variable that starts with a digit, which is an invalid syntax where variable name. The other two examples contain dashes and add signs, which are not valid characters to be used in variable names. Scripts are designed to replace the need for a person to physically sit at a keyboard and type of series of commands. One, if you have a task you want to automate, but it requires different actions based on different circumstances. If you're typing in the commands, you can take the time to look at a status and then make a decision-based on that status.

In shell scripts, you can do the same thing. You can do this with tests. To create a test, place a conditional expression between brackets, the syntax is open bracket conditioned to test for and close bracket. You can task for several types of situations. For example, you can compare if strings are equal, if a number is greater than another, or if a file exists. This test checks to see if etc password exists. If it, does it returns true, said another way the command exits with a status of zero. If the file doesn't exist, it returns false. That is the command exits with a status of one. If you're using the bash shell, you can run the command help test to see the various types of tests you can perform. You can also read the man page for tests by typing in man test.

Here's some of the more common tests you can perform. Dash D test to see whether what follows is a directory, if it is a directory, it returns true. Dash E returns true if the file exists. Dash F returns true with a file exists and it is a regular file. For example, it's not assembling. R returns true if the file is readable by you. S returns true if the file exists and it's not empty. Dash W test to see if the file is writeable. Dash X test to see if the file is executable, if it is it returns true. Dash Z followed by a string will return true if the string is empty. However, -n will return true if the string is not empty.

If you place two strings on either side of an equal sign, then it will return true if those strings are equal. You can also test the two strings are not equal by using an exclamation mark before the equal sign. You could also do number comparisons like dash EQ the test if the numbers are equal, or NE for not equal. LT if the first number is less than the second number, LE if the first number is less than or equal to the second number, GT if the first number is greater than the second number, and GE will return true if the first number is greater than or equal to the second number.

About the Author
Students
14217
Courses
61
Learning Paths
13

Jason is the founder of the Linux Training Academy as well as the author of "Linux for Beginners" and "Command Line Kung Fu." He has over 20 years of professional Linux experience, having worked for industry leaders such as Hewlett-Packard, Xerox, UPS, FireEye, and Amazon.com. Nothing gives him more satisfaction than knowing he has helped thousands of IT professionals level up their careers through his many books and courses.

Covered Topics