1. Home
  2. Training Library
  3. Programming
  4. Courses
  5. Shell Scripting, Succinctly

Shell Scripting Part II

Contents

keyboard_tab
Shell Scripting, Succinctly
1
Course Overview
PREVIEW49s

The course is part of this learning path

Bash Scripting and Shell Programming
course-steps
11
certification
1
Start course
Overview
Difficulty
Intermediate
Duration
17m
Students
30
Description

This course covers the most important aspects of shell scripting. 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. We'll also cover how to use if statements to make decisions in your scripts.

Learning Objectives

  • Obtain a solid understanding of shell scripting
  • Learn the components and variables that make up a script
  • How to perform tests and make decisions
  • How to accept command line arguments and input from users
  • Understand how to use if statements

Intended Audience

This course is intended for anyone who wants to learn the basics of shell scripting.

Prerequisites

To get the most out of this course, you should already have some basic knowledge of Linux.

Transcript

So now that you know how to determine if a certain condition is true or not, you can combine that with the If statement to make decisions in your scripts. The If statement starts with the word if, and is followed by a test. The following line contains the word then. Next is a command or series of commands that will be executed if the tested condition returns true. Finally, the if statement ends with fi, which is simply if spelled backwards. Here's an example script. Note that it's a best practice to enclose variables in quotes, to prevent some unexpected side effects when performing conditional tests. You can also perform an action if the condition is not true, by using an if/else statement. Simply insert the else key word and follow that by the commands you want to execute if the condition is not true. So let's update the script to perform an action if the statement is not true. Because my shell equals bash, evaluates to false, the statements following else were executed. You can also test for multiple conditions using elif. The word elif is a contraction for else/if. Like if, follow elif with a condition to test for. On the following line, use the word then. Finally, provide a series of commands to execute if the condition for the elif evaluates to true. Here is an updated version of the script using elif. If you want to perform an action on a list of items, use a for loop. The first line of a for loop starts with the word for, followed by a variable name and then a list of items. The next line contains the word do. Place the statements you want to execute on the following lines, and finally, in the for loop with the word done on a single line. Essentially what happens is that the first item in the list is assigned to the variable and the code block is executed. The next item in the list is then assigned to the variable and the commands are executed. This happens for each item in the list. Here's an example script that shows how a for loop works. You'll see that there are three items in the list. Red, green, blue. The for loop executes three times. The first time, red is assigned to the variable color and the echo color command is run. And the second time, green is assigned a color. And the third and final time, blue is assigned to the variable color and the command is executed. It's also a common practice for the list of items to be stored in a variable, as in this example. This script renames all of the files that end in jpg, by inserting today's date before the original file name. Here's what happens when you run this script. You'll see that the bear.jpg file becomes today's date-bear.jpg. today's date-bear.jpg. And this continues for every file that ends in jpg. Positional parameters are variables that contain the contents of the command line. The variables are $0 through $9. The script itself is stored in $0. The first parameter is stored in $1, the second in $2 and so on. Let's take this command line as an example. The contents of $0 are, script.sh. The contents of $0 are, script.sh. $1 contains parameter1, $2 contains parameter2 $1 contains parameter1, $2 contains parameter2 and $3 contains parameter3. This script called archive_user.sh accepts a parameter which happens to be a username. Anything that follows the pound sign is a comment. The only exception to this is the shebang on the first line. Everywhere else in the script where a pound sign is encountered, it marks the beginning of a comment. Comments are dutifully ignored by the interpreter, as they are for the benefit of us humans. Anything that follows the pound sign is ignored. If a pound sign starts at the beginning of a line, the entire line is ignored. If a pound sign is encountered in the middle of a line, only the information to the right of the pound sign is ignored. Here's what the output looks like when we execute the script. Instead of referring to $1 throughout the script, let's assign its value to a more meaningful variable name. In this case, let's assign it to the variable called USER. The output remains exactly the same. You can access all the positional parameters starting at $1 to the very last one on the command line, by using this special variable $@ sign. Here's how to update the script to accept one or more parameters. Now you can pass in multiple users to the script and the For Loop will execute for each user that you supplied on the command line. Here's what the script would look like if we passed in two users. In this case, Chet and Joe. If you want to accept standard input, use the read command. Remember that standard input typically comes from a person typing at the keyboard, but it can also come from other sources, like the output of a command in a command pipeline. The format for the read command is read-p, a PROMPT display and a variable name. This version of the archive user script asks for the user account. In this example, I ran the script and typed in the username, Mitch. Let's review. A first line and a shell script should start with a shebang and the path to the interpreter that should be used to execute the commands listed in the script. To assign a value to a variable, start with a variable name, followed by an equal sign then followed by the value. Do not use the space before or after the equal sign. You can access the value stored in a variable, by using dollar, variable name or dollar, curly brace, variable name, close curly brace. The latter form is required if you want to recede or follow the variable with additional data. To assign the output of a command to a variable and close the command in parentheses and precede it with a dollar sign. Perform tests by placing expressions in brackets. Tests are typically combined with if statements. Use if, if/else or if/elif/else statements to make decisions in your scripts. To perform an action or series of actions on multiple items, use a for loop. To access items on the command line, use positional parameters. The name of the program is represented by $0. The first parameter is represented by $1 and so on. To access all the items on the command line, starting at the first parameter or $1, use the special variable $@ sign. You can place comments in your scripts by using the pound sign. If you would like to accept user input in your scripts use the read command.

About the Author
Avatar
Jason Cannon
Founder, Linux Training Academy
Students
433
Courses
51
Learning Paths
5

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