Difference between revisions of "Tutorial12: Shell Scripting - Part 2"

From CDOT Wiki
Jump to: navigation, search
(Additional Loop Statements)
(Using Startup Files)
Line 92: Line 92:
 
===Using Startup Files===
 
===Using Startup Files===
 
   
 
   
x
+
Shell configuration files are scripts that are run when you log in, log out, or start a new shell. The start-up files can be used, for example, to set the prompt and screen display, create local variables, or create temporary Linux commands (aliases)�
 +
 
 +
The '''/etc/profile''' file belongs to the root user and is the first start-up file that executes when you log in, regardless of shell�
 +
 
 +
User-specific config start-up files are in the user's home directory: �'''~/.bash_profile''' runs when you log in '''~/.bashrc''' runs when you start an interactive sub-shell.
 +
 
 +
Logout Files
 +
 
 +
There are files that reset or restore the environment or properly shut-down running programs when the user logs out of their shell.�
 +
User-specific logout start-up files are in the user's home directory: ��'''~/.bash_logout'''
  
 
=INVESTIGATION 1: ADDITIONAL LOGIC STATEMENTS=
 
=INVESTIGATION 1: ADDITIONAL LOGIC STATEMENTS=

Revision as of 08:40, 27 July 2020

ADDITIONAL SHELL SCRIPTING


Main Objectives of this Practice Tutorial

  • Learn about additional logic control-flow statements if-else and if-elif-else
  • Learn additional uses of the for loop control-flow statement.
  • Learn the purpose of command substitution and how it can be used with control-flow statements
  • List and explain the purpose of several start-up files



Tutorial Reference Material

Course Notes
Linux Command/Shortcut Reference
YouTube Videos
Course Notes:


Additional Control Flow Statements Startup Files


Brauer Instructional Videos:

KEY CONCEPTS

Additional Logic Statements


Example of how an if-else statement works.
if-else Statements

If the test condition returns a TRUE value, then the Linux Commands between
then and else statements are executed.

If the test returns a FALSE value, then the the Linux Commands between
the else and fi statements are executed.

Example:

num1=5
num2=10
if test $num1 –lt $num2
then
   echo “Less Than”
else
echo    “Greater Than or Equal to”
fi


Example of how an if-elif-else statement works.
if-elif-else Statements

If the test condition returns a TRUE value, then the Linux Commands between
then and else statements are executed.

If the test returns a FALSE value, then a new condition is tested,
and action is taken if the result is TRUE . Eventually, an action will be taken
when the final test condition is FALSE

Example:

num1=5
num2=10
if test $num1 –lt $num2
then    echo “Less Than”
elif test $num1 –gt $num2
then
   echo “Greater Than”
else    echo “Equal to”
fi


Additional Loop Statements

Example of how a for loop with command substitution works.
Command Substitution

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

Examples:

command1 $(command2)
command1 [arguments from command2 output]


for Loop using Command Substitution

Let’s issue the for loop with a list using command substitution.
In the example below, we will use command substitution to issue the ls command and
have that output (filenames) become arguments for the for loop.

Example:

for x in $(ls)
do
   echo “The item is: $x”
done


Example of how a while loop works.
while Loop Statement

The condition/expression is evaluated, and if the condition/expression is true,
the code within … the block is executed. This repeats until the condition/expression becomes FALSE.
Reference: https://en.wikipedia.org/wiki/While_loop

Example:

answer=10
read –p “pick a number between 1 and 10: “ guess
while test $guess –eq 10
do    read –p “Try again: “ guess
done
echo “You are correct”


Using Startup Files

Shell configuration files are scripts that are run when you log in, log out, or start a new shell. The start-up files can be used, for example, to set the prompt and screen display, create local variables, or create temporary Linux commands (aliases)�

The /etc/profile file belongs to the root user and is the first start-up file that executes when you log in, regardless of shell�

User-specific config start-up files are in the user's home directory: �~/.bash_profile runs when you log in ~/.bashrc runs when you start an interactive sub-shell.

Logout Files

There are files that reset or restore the environment or properly shut-down running programs when the user logs out of their shell.� User-specific logout start-up files are in the user's home directory: ��~/.bash_logout

INVESTIGATION 1: ADDITIONAL LOGIC STATEMENTS


In this section, you will learn how to ...



Perform the Following Steps:

  1. x

In the next investigation, you will ...

INVESTIGATION 2: ADDITIONAL LOOPING STATEMENTS

In this section, you will learn how to ...


Perform the Following Steps:

  1. x

In the next investigation, you will ...

INVESTIGATION 3: USING STARTUP FILES

In this section, you will learn how to ...


Perform the Following Steps:

  1. x

LINUX PRACTICE QUESTIONS

The purpose of this section is to obtain extra practice to help with quizzes, your midterm, and your final exam.

Here is a link to the MS Word Document of ALL of the questions displayed below but with extra room to answer on the document to simulate a quiz:

https://ict.senecacollege.ca/~murray.saul/uli101/uli101_week12_practice.docx

Your instructor may take-up these questions during class. It is up to the student to attend classes in order to obtain the answers to the following questions. Your instructor will NOT provide these answers in any other form (eg. e-mail, etc).


Review Questions:

  1. x
  2. x
  3. x
  4. x
  5. x
  6. x
  7. x
  8. x