Difference between revisions of "Tutorial 10 - Shell Scripting - Part 1"

From CDOT Wiki
Jump to: navigation, search
(Tutorial Reference Material)
Line 54: Line 54:
  
 
|}
 
|}
 +
 +
= KEY CONCEPTS =
 +
A shell script is a '''file''' that contains '''Unix/Linux commands''' and '''reserved words''' to help '''automatic''' common tasks.
 +
 +
===Creating & Executing Shell Scripts===
 +
 +
It is recommended to '''plan''' out on a piece of paper the purpose of the shell script.<br>Once you have planned your shell script by listing the '''sequence of steps (commands)''',<br>you need to create a file (using a '''text editor''') that will contain your Linux commands.<br><br>'''NOTE:'''  Avoid using filenames of already existing Linux Commands to avoid confusion.<br>It is recommended to include a file extension that describes the type of shell for the shell script.<br><br>
 +
 +
'''Using a Shebang Line'''
 +
 +
[[Image:shebang.png|thumb|right|200px|The '''shebang line''' <u>must</u> appear on the '''first line''' and at the '''beginning''' of the shell script.]]Since Linux shells have evolved over a period of time, using a she-bang line '''forces''' the shell script<br>to run in a '''specific shell''', which could prevent errors in case an older shell does not recognize<br>newer features from more recent shells.<br><br>The '''she-bang''' line is a '''special comment''' at top of your shell script to run your shell script<br>in a specific shell.<br><br>
 +
'''NOTE:''' The '''shebang line''' <u>must</u> appear on the '''first line''' and at the '''beginning''' of the shell script,<br>otherwise, it will be treated as a regular comment and ignored.<br><br>
 +
 +
'''Setting Permissions / Running Shell Scripts'''
 +
 +
To run your shell script by name, you need to assign '''execute permissions''' for the user.<br>To run the shell script, you can '''execute''' the shell script using a ''relative'', ''absolute'', or ''relative-to-home'' pathname
 +
 +
''Examples:''<br><span style="font-family:courier;">'''chmod u+x myscript.bash<br>./myscript.bash<br>/home/username/myscript.bash<br>~/myscript.bash</span>
 +
'''
 +
<br><br>
 +
 +
===Variables / Parameters===
 +
 +
 +
'''Environment Variables'''
 +
 +
[[Image:environment.png|thumb|right|500px|Examples of using '''Environment''' and '''User Defined''' variables.]]Shell '''environment variables''' shape the working environment whenever you are logged in Common shell. Some of these variables are displayed via Linux commands in the diagram displayed on the right-side.<br><br>You can issue the pipeline command <span style="font-family:courier;font-weight:bold">set | more</span><br>to view all variables.<br><br>Placing a dollar sign "<span style="font-family:courier;font-weight:bold">$</span>" prior to the variable name will cause the variable to expand to the value contained in the variable.
 +
 +
 +
'''User Defined Variables'''
 +
 +
<b>User-defined variables</b> are variables that can be used in the shell script for '''customized''' purposes.
 +
<br><br>
 +
Data can be stored and removed within a variable using an '''equal sign''' (no spaces on either side of equal sign).<br><br>The '''read''' command can be used to prompt the user to enter data into a variable. The '''readonly''' command will prevent<br>the current value of the variable for the remainder of the execution of a shell script.<br><br>
 +
 +
'''Positional Parameters and Special Parameters'''
 +
 +
[[Image:positional.png|thumb|right|220px|Examples of using '''positional''' and '''special''' parameters.]]A '''positional parameter''' is a variable within a shell program; its value is set from arguments contained in a shell script or using the '''set''' command.
 +
Positional parameters are numbered and their values are accessed by using<br>a preceding "'''$'''" (eg. '''$1''', '''$2''', '''$3''', etc.). The positional parameter '''$0''' refers to<br>either the '''name of shell''' where command was issued, or '''filename of shell script''' being executed.<br>If using '''positional parameters''' greater than '''9''', then you need to include number within braces.<br><br>Examples: '''echo ${10}''', '''ls ${23}'''<br><br>
 +
 +
The '''shift''' command can be used with positional parameters to shift positional parameters<br>to the left by one or more positions.
 +
 +
There are a couple of ways to assign values as positional parameters:
 +
:*Use the '''set''' command with the values as argument after the set command
 +
:*Run a shell script containing arguments
 +
 +
 +
There are a group of '''special parameters''' that can be used for shell scripting.<br>A few of these special parameters and their purpose are displayed below:<br>'''$*''' , '''“$*”''' , '''"$@"''' , '''$#''' , '''$?'''
 +
<br><br>
 +
 +
=== Command Substitution / Math Operations ===
 +
<br>
 +
'''Command Substitution:'''
 +
 +
[[Image:for-command-substitution.png|thumb|right|300px|Example of how a '''for loop with command substitution''' works.]]
 +
<i>'''Command substitution''' is a facility that allows a command<br>to be run and its output to be pasted back on the command line as arguments to another command.</i> Reference: https://en.wikipedia.org/wiki/Command_substitution<br><br>
 +
 +
''Usage:''
 +
 +
<span style="font-family:courier"><b>command1 $(command2)</b><br>or<br><b>command1 `command2`</b></span><br><br>
 +
 +
''Examples:''
 +
 +
<span style="font-family:courier;font-weight:bold">file $(ls)<br>mail -s "message" $(cat email-list.txt) < message.txt<br>echo "The current directory is $(pwd)"<br>echo "The current hostname is $(hostname)"<br>echo "The date is: $(date +'%A %B %d, %Y')"<br>
 +
<br><br>
 +
'''Math Operations:'''
 +
[[Image:math-op.png|thumb|right|275px|Common Math Operator Symbols.]]
 +
In order to make math operations work, we need to convert numbers<br>stored as '''text''' into '''binary numbers'''.<br><br>We can do this by using 2 pairs of round brackets '''(( ))'''.<br><br>
 +
''Examples:''
 +
 +
<pre style="width:30%">num1=5;num2=10
 +
echo “$(($num1 + $num2))”
 +
15
 +
echo “$((num1-num2))”
 +
-5
 +
((product=num1*num2))
 +
echo “$product”
 +
50
 +
</pre>
 +
<br>
 +
 +
===Control Flow Statements===
 +
<br>
 +
<table align="right"><tr valign="top"><td>[[Image:test-1.png|thumb|right|140px|Examples of simple comparisons using the test command.]]</td><td>[[Image:test-2.png|thumb|right|140px|Examples of using additional comparisons using the test command.]]</td></table>
 +
'''Control Flow Statements''' are used to make your shell scripts<br>more '''flexible''' and can '''adapt''' to changing situations.<br><br>In order to use control flow statements, you need to test a condition to get<br>'''TRUE''' (zero value) or '''FALSE''' (non zero value). This can be done two ways:<ul><li>Run a command to get the exit status (<span style="font-family:courier;font-weight:bold;">$?</span>)</li><li>Use the '''test''' command</li></ul><br>Refer to the diagrams on the right to see how to use the test command.<br><br>
 +
 +
You CANNOT use the <span style="font-family:courier;font-weight:bold;">&lt;</span> or <span style="font-family:courier;font-weight:bold;">&gt;</span> symbols when using the test command since these are redirection symbols. Instead, you need to use '''options''' when performing numerical comparisons.
 +
Refer to the diagrams to the right '''test options''' and their purposes.
 +
<br><br>
 +
 +
'''Logic Statements'''
 +
 +
A '''logic statement''' is used to determine which Linux commands<br>are executed basedon the result of a condition:<br>'''TRUE''' (zero value) or '''FALSE''' (non-zero value).
 +
 +
[[Image:logic-1.png|thumb|right|210px|Example of using the '''if''' logic control-flow statement.]]
 +
<br>
 +
There are several logic statements, but we will just concentrate on the if statement.
 +
<pre style="width:20%">
 +
if test condition
 +
  then
 +
    command(s)
 +
fi
 +
</pre>
 +
 +
Refer to the diagram to the right for using the '''if logic statement''' with the '''test''' command.
 +
 +
<br><br><br><br><br>
 +
'''if-else statement:'''
 +
<br>
 +
[[Image:logic-2.png|thumb|right|210px|Example of how an '''if-else''' control-flow statement.]]
 +
 +
Unlike using an ''if'' statement, an '''if-else''' statement take '''two different sets of actions'''<br>based on the results of the test condition.<br><br>
 +
 +
''Example:''
 +
 +
<pre style="width:20%">
 +
if test condition
 +
  then
 +
    command(s)
 +
  else
 +
    command(s)
 +
fi
 +
</pre>
 +
 +
 +
'''Loop Statements'''
 +
[[Image:loop-1.png|thumb|right|210px|Example of using the '''for''' looping control-flow statement.]]
 +
''A <b>loop statement</b> is a series of steps or sequence of statements executed repeatedly<br>zero or more times satisfying the given condition is satisfied.''<br>Reference: https://www.chegg.com/homework-help/definitions/loop-statement-3
 +
 +
There are several loops, but we will look at the '''for loop''' using a '''list'''.
 +
 +
<pre style="width:20%">
 +
for item in list
 +
do
 +
    command(s)
 +
done
 +
</pre>
 +
 +
Refer to the diagram above and to the extreme right side for an example using the '''for loop''' with a '''list'''.
  
 
= INVESTIGATION 1: CREATING A SHELL SCRIPT =
 
= INVESTIGATION 1: CREATING A SHELL SCRIPT =

Revision as of 13:35, 14 November 2021

Content under construction

INTRODUCTION TO SHELL SCRIPTING

Main Objectives of this Practice Tutorial

  • Plan and create a Shell Script
  • Explain the purpose of the she-bang line contained at the top of a shell script.
  • Set permissions and execute shell scripts.
  • Use environment and user-defined variables in shell scripts.
  • Use Command Substitution and Math Operations in shell scripts
  • Explain the purpose of the $? exit status and the test command.
  • Use if and if-else logic statements in shell scripts.
  • Use a for loop statement with a list in shell scripts.

Tutorial Reference Material

Course Notes
Linux Command/Shortcut Reference
Course Notes:


Shell Scripting

Variables

Commands / Techniques

Control Flow Statements

KEY CONCEPTS

A shell script is a file that contains Unix/Linux commands and reserved words to help automatic common tasks.

Creating & Executing Shell Scripts

It is recommended to plan out on a piece of paper the purpose of the shell script.
Once you have planned your shell script by listing the sequence of steps (commands),
you need to create a file (using a text editor) that will contain your Linux commands.

NOTE: Avoid using filenames of already existing Linux Commands to avoid confusion.
It is recommended to include a file extension that describes the type of shell for the shell script.

Using a Shebang Line

The shebang line must appear on the first line and at the beginning of the shell script.
Since Linux shells have evolved over a period of time, using a she-bang line forces the shell script
to run in a specific shell, which could prevent errors in case an older shell does not recognize
newer features from more recent shells.

The she-bang line is a special comment at top of your shell script to run your shell script
in a specific shell.

NOTE: The shebang line must appear on the first line and at the beginning of the shell script,
otherwise, it will be treated as a regular comment and ignored.

Setting Permissions / Running Shell Scripts

To run your shell script by name, you need to assign execute permissions for the user.
To run the shell script, you can execute the shell script using a relative, absolute, or relative-to-home pathname

Examples:
chmod u+x myscript.bash
./myscript.bash
/home/username/myscript.bash
~/myscript.bash


Variables / Parameters

Environment Variables

Examples of using Environment and User Defined variables.
Shell environment variables shape the working environment whenever you are logged in Common shell. Some of these variables are displayed via Linux commands in the diagram displayed on the right-side.

You can issue the pipeline command set | more
to view all variables.

Placing a dollar sign "$" prior to the variable name will cause the variable to expand to the value contained in the variable.


User Defined Variables

User-defined variables are variables that can be used in the shell script for customized purposes.

Data can be stored and removed within a variable using an equal sign (no spaces on either side of equal sign).

The read command can be used to prompt the user to enter data into a variable. The readonly command will prevent
the current value of the variable for the remainder of the execution of a shell script.

Positional Parameters and Special Parameters

Examples of using positional and special parameters.
A positional parameter is a variable within a shell program; its value is set from arguments contained in a shell script or using the set command.

Positional parameters are numbered and their values are accessed by using
a preceding "$" (eg. $1$2$3, etc.). The positional parameter $0 refers to
either the name of shell where command was issued, or filename of shell script being executed.
If using positional parameters greater than 9, then you need to include number within braces.

Examples: echo ${10}, ls ${23}

The shift command can be used with positional parameters to shift positional parameters
to the left by one or more positions.

There are a couple of ways to assign values as positional parameters:

  • Use the set command with the values as argument after the set command
  • Run a shell script containing arguments


There are a group of special parameters that can be used for shell scripting.
A few of these special parameters and their purpose are displayed below:
$* , “$*” , "$@" , $# , $?

Command Substitution / Math Operations


Command Substitution:

Example of how a for loop with command substitution works.

Command substitution is a facility that allows a command
to be run and its output to be pasted back on the command line as arguments to another command.
Reference: https://en.wikipedia.org/wiki/Command_substitution

Usage:

command1 $(command2)
or
command1 `command2`


Examples:

file $(ls)
mail -s "message" $(cat email-list.txt) < message.txt
echo "The current directory is $(pwd)"
echo "The current hostname is $(hostname)"
echo "The date is: $(date +'%A %B %d, %Y')"


Math Operations:

Common Math Operator Symbols.

In order to make math operations work, we need to convert numbers
stored as text into binary numbers.

We can do this by using 2 pairs of round brackets (( )).

Examples:

num1=5;num2=10
echo “$(($num1 + $num2))”
15
echo “$((num1-num2))”
-5
((product=num1*num2))
echo “$product”
50


Control Flow Statements


Examples of simple comparisons using the test command.
Examples of using additional comparisons using the test command.
Control Flow Statements are used to make your shell scripts
more flexible and can adapt to changing situations.

In order to use control flow statements, you need to test a condition to get
TRUE (zero value) or FALSE (non zero value). This can be done two ways:
  • Run a command to get the exit status ($?)
  • Use the test command

Refer to the diagrams on the right to see how to use the test command.

You CANNOT use the < or > symbols when using the test command since these are redirection symbols. Instead, you need to use options when performing numerical comparisons. Refer to the diagrams to the right test options and their purposes.

Logic Statements

A logic statement is used to determine which Linux commands
are executed basedon the result of a condition:
TRUE (zero value) or FALSE (non-zero value).

Example of using the if logic control-flow statement.


There are several logic statements, but we will just concentrate on the if statement.

if test condition
  then
     command(s) 
fi

Refer to the diagram to the right for using the if logic statement with the test command.






if-else statement:

Example of how an if-else control-flow statement.

Unlike using an if statement, an if-else statement take two different sets of actions
based on the results of the test condition.

Example:

if test condition
  then
     command(s)
  else
     command(s) 
fi


Loop Statements

Example of using the for looping control-flow statement.

A loop statement is a series of steps or sequence of statements executed repeatedly
zero or more times satisfying the given condition is satisfied.

Reference: https://www.chegg.com/homework-help/definitions/loop-statement-3

There are several loops, but we will look at the for loop using a list.

for item in list 
do
    command(s) 
done

Refer to the diagram above and to the extreme right side for an example using the for loop with a list.

INVESTIGATION 1: CREATING A SHELL SCRIPT

INVESTIGATION 2: USING VARIABLES IN SHELL SCRIPTS

INVESTIGATION 3: COMMAND SUBSTITUTION / MATH OPERATIONS

INVESTIGATION 4: USING CONTROL FLOW STATEMENTS IN SHELL SCRIPTS

LINUX PRACTICE QUESTIONS