Shell Scripting Part 2
Shell Scripting Part 2
7h 28m

In this course, you will learn how to install a Linux system and connect to it, whether that be on Mac or Windows.


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, The contents of $0 are, $1 contains parameter1, $2 contains parameter2 $1 contains parameter1, $2 contains parameter2 and $3 contains parameter3. This script called 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
Learning Paths

A world-leading tech and digital skills organization, we help many of the world’s leading companies to build their tech and digital capabilities via our range of world-class training courses, reskilling bootcamps, work-based learning programs, and apprenticeships. We also create bespoke solutions, blending elements to meet specific client needs.