Difference between revisions of "Tutorial 12 - Shell Scripting - Part 2"

From CDOT Wiki
Jump to: navigation, search
Line 46: Line 46:
 
|colspan="1" style="padding-left:15px;" width="30%"|Brauer Instructional Videos:<ul><li>[https://www.youtube.com/watch?v=XVTwbINXnk4&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=6 Bash Shell Scripting - Part 2]</li></ul>
 
|colspan="1" style="padding-left:15px;" width="30%"|Brauer Instructional Videos:<ul><li>[https://www.youtube.com/watch?v=XVTwbINXnk4&list=PLU1b1f-2Oe90TuYfifnWulINjMv_Wr16N&index=6 Bash Shell Scripting - Part 2]</li></ul>
 
|}
 
|}
 +
 +
= KEY CONCEPTS =
 +
 +
====IF-ELIF-ELSE STATEMENT====
 +
 +
[[Image:if-elif-else.png|thumb|right|300px|Example of how an '''if-elif-else''' statement works.<br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]
 +
 +
The '''elif''' statement can be used to perform additional conditional tests of the previous test condition tests '''FALSE'''. This statement is used to make your logic control-flow statement to be more adaptable.<br><br>''How it Works:''<br>If the test condition returns a '''TRUE''' value, then the Linux Commands between<br>'''then''' and '''else''' statements are executed.<br><br>If the test returns a '''FALSE''' value, then '''a <u>new</u> condition is tested again''',<br>and action is taken if the result is '''TRUE''', then the Linux Commands between<br>'''then''' and '''else''' statements are executed. '''Additional elif statements''' can be used if additional conditional testing is required . Eventually, an action will be taken<br>when the final test condition is '''FALSE'''.<br><br>
 +
 +
''Example:''
 +
 +
<span style="font-family:courier;font-weight:bold;">num1=5<br>num2=10<br>if test $num1 –lt $num2<br>then<br> &nbsp;&nbsp;&nbsp;echo “Less Than”<br>elif test $num1 –gt $num2<br>then<br> &nbsp;&nbsp;&nbsp;echo “Greater Than”<br>else &nbsp;&nbsp;&nbsp;echo “Equal to”<br>fi</span><br><br>
 +
 +
====FOR LOOP USING COMMAND SUBSTITUTION====
 +
 +
Let’s issue the '''for''' loop with a '''list''' using '''command substitution'''.<br>In the example below, we will use command substitution to issue the ls command and<br>have that output (filenames) become arguments for the for loop.<br><br>
 +
 +
''Example:''
 +
 +
<span style="font-family:courier;font-weight:bold;">for x in $(ls)<br>do<br> &nbsp;&nbsp;&nbsp;echo “The item is: $x”<br>done</span><br><br>
 +
 +
====WHILE LOOP====
 +
[[Image:while-loop.png|thumb|right|170px|Example of how a '''while''' loop works.<br>(Image licensed under [https://creativecommons.org/licenses/by-sa/3.0/ cc])]]
 +
 +
''The '''while''' loop is useful to loop based on the result from a test condition or command result.<br>This type of loop is very useful for '''error-checking'''.<br><br><i>How it Works:</i><br>The condition/expression is evaluated, and if the condition/expression is '''TRUE''',<br>the code within … the block is executed.''
 +
This repeats until the condition/expression becomes '''FALSE'''.<br>Reference: https://en.wikipedia.org/wiki/While_loop<br><br>
 +
 +
''Example 1:''
 +
 +
<span style="font-family:courier;font-weight:bold">answer=10<br>read –p “pick a number between 1 and 10: “ guess<br>while test $guess –eq 10<br>do &nbsp;&nbsp;&nbsp;read –p “Try again: “ guess<br>done<br>echo “You are correct”</span><br><br>
 +
 +
 +
''Example 2:''
 +
 +
<span style="font-weight:bold;font-family:courier;">
 +
value=1<br>
 +
while [ $value -le 5 ]<br>
 +
do<br>
 +
&nbsp;&nbsp;echo "$value"<br>
 +
&nbsp;&nbsp;((value=value+1)) # could also use ((value++))<br>
 +
done<br>
 +
1<br>
 +
2<br>
 +
3<br>
 +
4<br>
 +
5
 +
</span>
 +
<br><br>
 +
 +
====EXIT &amp; BREAK STATEMENTS====
 +
<br>
 +
'''<span style="font-family:courier;font-weight:bold;">exit</span> Statement'''
 +
 +
The '''exit''' statement is used to '''terminate''' a shell script.<br>
 +
This statement is very useful when combined with logic in a shell script.<br>
 +
The exit command can contain an argument to provide the exit status of your shell script.
 +
 +
''Example:''
 +
 +
<span style="font-family:courier;font-weight:bold">if [ $# -ne 1 ]<br>then<br>  echo "USAGE: $0 [arg]"<br>  exit 1<br>fi<br></span>
 +
<br>
 +
'''<span style="font-family:courier;font-weight:bold;">break</span> Statement'''
 +
 +
The '''break''' statement is used to '''terminate a loop'''.<br>
 +
Although the loop terminates, the shell script will continue running.
 +
 +
''Example:''<br>
 +
<span style="font-family:courier;font-weight:bold">read -p "Enter a number: " number<br>while [ $number -ne 5 ]<br>do<br>   read -p "Try again. Enter a number: " number<br>   if [ $number -eq 5 ]<br>   then<br>     break<br>
 +
   fi<br>done<br></span>
 +
<br><br>
 +
 +
====START-UP FILES====
 +
 +
'''Shell configuration  (start-up) files''' are '''scripts''' that are run when you log in, log out, or start a new shell.<br>The start-up files can be used, for example, to '''set the prompt and screen display''', '''create local variables''',<br>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:<br>
 +
<ul><li>'''~/.bash_profile''' runs when you log in.<br></li><li>The '''~/.bashrc''' runs when you start an interactive Bash shell.</li></ul>
 +
<br>
 +
'''Logout Files'''
 +
 +
There is a file that '''resets or restores the shell environment''' (including shut-down of programs running in the shell) 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 14:05, 14 November 2021

Content under development

ADDITIONAL SHELL SCRIPTING

Main Objectives of this Practice Tutorial

  • Use the if-elif-else control flow statement in a shell script.
  • Use the for loop control using a list with command substitution.
  • Use the while loop in a shell script.
  • Use the exit and break statements in a shell script.
  • Explain how to configure and use a .bashrc start-up file.


Tutorial Reference Material

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


Control Flow Statements

Additional Statements

Startup Files


Brauer Instructional Videos:

KEY CONCEPTS

IF-ELIF-ELSE STATEMENT

Example of how an if-elif-else statement works.
(Image licensed under cc)

The elif statement can be used to perform additional conditional tests of the previous test condition tests FALSE. This statement is used to make your logic control-flow statement to be more adaptable.

How it Works:
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 again,
and action is taken if the result is TRUE, then the Linux Commands between
then and else statements are executed. Additional elif statements can be used if additional conditional testing is required . 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


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


WHILE LOOP

Example of how a while loop works.
(Image licensed under cc)

The while loop is useful to loop based on the result from a test condition or command result.
This type of loop is very useful for error-checking.

How it Works:
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 1:

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”



Example 2:

value=1
while [ $value -le 5 ]
do
  echo "$value"
  ((value=value+1)) # could also use ((value++))
done
1
2
3
4
5


EXIT & BREAK STATEMENTS


exit Statement

The exit statement is used to terminate a shell script.
This statement is very useful when combined with logic in a shell script.
The exit command can contain an argument to provide the exit status of your shell script.

Example:

if [ $# -ne 1 ]
then
  echo "USAGE: $0 [arg]"
  exit 1
fi

break Statement

The break statement is used to terminate a loop.
Although the loop terminates, the shell script will continue running.

Example:
read -p "Enter a number: " number
while [ $number -ne 5 ]
do
   read -p "Try again. Enter a number: " number
   if [ $number -eq 5 ]
   then
     break
   fi
done


START-UP FILES

Shell configuration (start-up) 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.
  • The ~/.bashrc runs when you start an interactive Bash shell.


Logout Files

There is a file that resets or restores the shell environment (including shut-down of programs running in the shell) 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 2: ADDITIONAL LOOPING STATEMENTS

INVESTIGATION 3: USING STARTUP FILES

LINUX PRACTICE QUESTIONS