0% found this document useful (0 votes)
33 views26 pages

Laboratory Lecture 3

Uploaded by

ralphaurelio1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views26 pages

Laboratory Lecture 3

Uploaded by

ralphaurelio1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Operating System

(CPET 410L)

Professor: Michael C. Olivo


Shell Scripting
The echo Command

 The echo command is the Bash Shell


output mechanism:
 echo argument ... prints arguments to

standard output separated by spaces


 echo -n argument ... no trailing

newline after output


 echo -e argument ... interpret \char

escapes
 echo -E argument ... do not interpret \

char escapes
Special echo Characters
 Backslash character sequences have special
meaning
 \a Alarm - ring the terminal bell
 \b Backspace
 \c Print without trailing newline (same as print
-n)
 \f Form feed
 \n Newline
 \r Return
 \t Tab
 \v Vertical tab
 \\ Backslash
 \xxx Character with octal code xxx (up to
echo Examples
Example:
$ echo -e "Line 1\n\tLine2"
Output:
Line 1
Line2
The read Command
 To get input while a Shell Script is
running, use read:
 The read command allows you to prompt

for input and store it in a variable.


Syntax:
read variable …
Example:
read var1 var2
123 456 789
echo -e "var1 = $var1 \tvar2 = $var2“
Output:
var1 = 123 var2 = 456 789
The read Command
Example#2
$ read var1 var2
$ abc
$ echo -e "var1 = $var1 \tvar2 = $var2"
Output:
var1 = abc var2 =
Basic Shell Script
 Shell program interprets user
commands, which are either directly
entered by the user, or which can be read
from a file called the shell script or shell
program.
 Shell scripts are interpreted, not compiled.

The shell reads commands from the script


line per line and searches for those
commands on the system while a compiler
converts a program into machine readable
form, an executable file – which may then
be used in a shell script.
Basic Shell Script
 Apart from passing commands to the kernel,
the main task of a shell is providing a user
environment, whichcan be configured
individually using shell resource
configuration files.

Basic Format of Shell Script:

#!/bin/bash
<commands>
<system/user defined variable>
<statements>
<control flow>
#!Program1: Save as
userinfo.sh

clear
echo “Hello “, $USER
echo “Welcome to Linux Shell Script”
echo “Today is”; date
sleep 5
clear
Sample Program
Where:

The #!/bin/bash – this an optional


comment use to indicate will
interpret by the bash shell.
The echo command displays the
string, variables or arguments form
the parameters.
The sleep [delay in seconds]
command serves as a delay or pause
for the next instructions or
commands.
Sample Program
Output:
“Hello “, mcolivo
“Welcome to Linux Shell Script”
“Today is”
Wed Jan 4 06:59:57 PST 2017
Sample Program
Program#2:
Create a simple shell script that
accept first name,
middle name, and last name then
append the full
names (lastname, firstname,
middlename format)
together with the current date and
time of the
computer clock.
#!Program 2: Save as userinfo2.sh

clear
echo “My Personal Information”
echo “Enter your first name”
read fname
echo “Enter your middle name”
read mname
echo “Enter your lastname”
read lname
echo “Welcome $lname $fname $mname”
echo “Today is”
date
sleep 5
The Bash Shell let Command
let argument ...
-or-
(( argument ))

 The let built-in Shell command performs


long integer arithmetic
 approximately 10 times faster than expr
 Evaluates each argument as an
arithmetic expression
 No quotes for special characters, or
arguments with spaces or tabs
 in them, within $(( ))
The Bash Shell let Command
 Variables need no $
 The exit code is 0 (true) for non-zero, and 1

(false) for zero


 evaluations
let Arithmetic Operators
For simple arithmetic:
( ) overrides normal precedence rules
* multiplication
/ division
% remainder
+ addition
- subtraction (or unary minus)
= assignment
Example
$ z=2 ; y="z + 1"
$ x=$(( 3*y )) or let x=3*y
$ echo $x
Answer: 9
let Logical Operators
let Logical Operators
! logical negation
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
!= not equal to
&& logical "and“
|| logical "or”
let Logical Example
$ let 'p = 9 '
$ let 'p = p * 6'
$ echo $p
Answer: 54
$ let 'p > 0 && p <= 10'
$ echo $p
Answer: 1
$ q=100
$ let 'p < q || p == 5'
$ echo $q
Answer: 0
bc - Mathematics
 Linux provides the bc utility
 performs floating point arithmetic
 acts as a filter command or

interactively
 reads arithmetic expression strings

from standard input or specified file


 semicolons or new lines separate

expressions
 set the scale variable inside bc to

define the required number of decimal


places
bc – Mathematics Example

$ echo '1/4' | bc integer


division without a scale
Answer: 0
$ echo 'scale = 3 ; 1/4' | bc explicit
scale value set
Answer: 0.250
$ echo '5.5 * 2.2' | bc scale set
implicitly from input
Answer: 12.1
Sample Arithmetic Program
Create a shell program that will
compute and display the sum,
product, difference, quotient, and
modulus of two numbers.
clear
echo "Enter First Number "
read num1
echo "Enter Second Number "
read num2
sum=$(($num1 + $num2))
prod=$(($num1 * $num2))
dif=$(($num1 - $num2))
quo=$(($num1 / $num2))
mod=$(($num1 % $num2))
echo "The Sum of $num1 and $num2 is $sum"
echo "The Product of $num1 and $num2 is $prod"
echo "The Difference of $num1 and $num2 is $dif"
echo "The Quotient of $num1 and $num2 is $quo"
echo "The Modulus of $num1 and $num2 is $mod"
bc – Arithmetic Example
Create a program that will compute and display the
discount

echo "Enter the Price of the item "


read price
echo "What Percentage of discount is available "
read per
echo "Your Total Savings are "
savings=`echo "scale=3; (($price * $per) / 100)" | bc -l`
echo $savings
Activity
Activity#1

Create a shell program that will compute the


area of a circle, triangle, rectangle, and
pyramid.

Sample output:
Enter length:
Enter width:
The area of rectangle is:

Activity#2

Create a shell program that will convert the


feet to meter and vice versa

You might also like