0% found this document useful (0 votes)
2 views9 pages

Operating System Laboratory Shell Scripting

This document outlines a laboratory exercise focused on basic shell scripting using Linux. It includes objectives, procedures, and examples for creating and executing shell scripts, modifying file permissions, and utilizing shell variables and arithmetic. Students are expected to write scripts that gather user input and perform calculations, while adhering to proper scripting conventions.

Uploaded by

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

Operating System Laboratory Shell Scripting

This document outlines a laboratory exercise focused on basic shell scripting using Linux. It includes objectives, procedures, and examples for creating and executing shell scripts, modifying file permissions, and utilizing shell variables and arithmetic. Students are expected to write scripts that gather user input and perform calculations, while adhering to proper scripting conventions.

Uploaded by

allysa vega
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Operating Systems

Laboratory Exercise 4
Basic Shell Scripting
Objectives:
At the end of the session, the students are expected to:
 become familiar with basic shell scripts
 use proper commands for shell scripts construction

Materials:

 1 PC with Linux

Procedures:

ASSUMPTIONS:
 user is already familiar with basic Linux commands
 user is already familiar with vi utility

STEPS:

1. It is mandatory that you use an editor program such as vi or pico. Most Linux variants make use of
vi as the primary editor.
2. Upon the completion of the script file, your task is to modify the permission of the file such that it
can be executed.
3. Lastly, execute your file.

TRY THIS:

1. Log on to your Linux account.


2. Once you are logged under your directory, create a file called myFirst using vi editor and type the
following inside vi.

$vi myFirst

#This is my first program


clear
echo “THIS IS EASY”
#echo “This command is not executed”

Note: The # symbol in shell scripting is used as a REMARK. This simply means that any
command or statement placed after the # symbol will be disregarded. The # symbol is
useful for inserting comments on the program.

Basic Shell Scripting *Property of STI


Page 1 of 6
Operating Systems

3. Once the file is saved, your next task is to modify the file access permission such that it can be
executed. The command is:

$ chmod 755 myFirst

This simply means that the owner of the file will have full access (Read, Write, and
Execute). Group and Other users will have an access right of Read and Execute.

Confirm that the permission has been changed by simply typing the command $ ls -l. This
command will give a long list of files in your current directory. The output should look
something like this:

-rwxr-xr-x 1 benz benz 81 Jul 12 00:49 myFirst

4. All that is left to do is to execute the script file. There are many ways on how the file can be
executed. These are the following:

$ bash myFirst
$ sh myFirst
$ ./myFirst

Your task is to create a new script file called myInfo. This file will simply print personal details of
yourself such as name, address, age, gender, phone number, mobile number, course, and year
level. You should format your output in the following manner.

Format:

Name: Peter Parker Age: 25


Address: New York Sex: Male
Phone Number: 1-800-258647 Course: Photography
Mobile Number: 0917-1234567 Year Level: 3

Be sure that the output follows the desired format.

What happens if the access permission was not changed and ./myInfo is executed?

What happens if the chmod 444 myInfo command is used for changing the access right of the file
when ./myInfo is executed?

Basic Shell Scripting *Property of STI


Page 2 of 6
Operating Systems

MORE ON SCRIPTING

Under shell scripting, it is possible that the user may use ordinary commands such as ls, cd, cal, who,
whoami, etc. that may be necessary for a certain script. Forinstance,

$ vi myInfo
#
#
# Script to print user information who currently login, current date # & time
#
clear
echo "Hello $USER"
echo "Today is `date`”
echo "Number of user login : `who | wc -l`”
echo " Calendar"
cal

Note: Use the ` (back quote) sign, not the ‘ (single quote) sign. Back quote is generally found with the
tilde (~) key or above the TAB key on a PC keyboard.

Exercise: Write down this script using vi and save the file as myInfo. Execute this script and write
down the result.

SHELL SYSTEM VARIABLES

Linux has many kinds of shell variables. For instance, the $USER contains the name of the current user in
a certain terminal. In other words, this command is also equivalent to whoami command.

Other shell variables include the following:

$BASH
$BASH_VERSION
$COLUMNS
$HOME
$LINES
$LOGNAME
$OSTYPE
$PATH
$PS1
$PWD
$SHELL
$USERNAME

Write down the result of each of the shell variables by typing

$ echo <shell variable>

For example, $ echo $USER.

Basic Shell Scripting *Property of STI


Page 3 of 6
Operating Systems

USER DEFINED VARIABLES (UDV)

In Linux, it is possible that the user may define its own variables. This may be necessary to simplify certain
commands or simply combine multiple commands into one command execution/call.

Warning: Do not modify system variables because this can sometimes create problems. Treat the
system variables as reserved words in programming.

To define UDV, use the following syntax:

Syntax:

variable name=value

'Value' is assigned to a given 'variable name' and value must be on right side of the = sign.

Examples:

This is ok:

$ no=10

Error, not ok, and the value must be on right side of the = sign:

$ 10=no

To define a variable called 'vech' having value Bus:

$ vech=Bus

To define a variable called n having value 10:

$ n=10

Basic Shell Scripting *Property of STI


Page 4 of 6
Operating Systems

CONVENTIONS IN NAMING VARIABLE NAMES

Variable names should begin with alphabetic characters or underscore ( _ ) followed by one or more
alphanumeric characters.

Examples:

HOME
SYSTEM_VERSION
vech
no

Do not put spaces on either side of the equal sign when assigning a value to a variable.

Examples:

$ no=10 <--- This is correct


$ no =10 <--- This is wrong
$ no= 10 <--- This is wrong
$ no = 10 <--- This is wrong

Variables are case-sensitive, just like commands and filename in Linux.

Example:

Assuming we have created the following variables:

$ no=10
$ No=11
$ NO=20
$ nO=2

The following commands will result to a different output:

$ echo $no <--- will print 10 but not 20


$ echo $No <--- will print 11 but not 20
$ echo $nO <--- will print 2 but not 20

Basic Shell Scripting *Property of STI


Page 5 of 6
Operating Systems

You can define NULL variable as follows (NULL variable is variable which has no value at the time
of definition):

$ vech=
$ vech=""

When trying to print the value of a NULL variable like this:

$echo $vech <--- nothing is shown since it is null

PRINTING SHELL VARIABLE VALUES

In order to print or view the value of a variable, follow this syntax:

$ echo $variable name

For example:

$var=27 <--- you have created a variable called var with a value 27

$echo $var <--- this command will print the current value of variable

Warning: Do not try $ echo var, as it will print var instead its value 27. You must use $ followed by
variable name.

Write down the command to:

Define variable x with value 10 and print it on screen.


Define variable xn with value BenZ and print it on screen.

Point out the errors, if there is any, in the following script.

$ vi myScript
#
#
# Script on script debugging
#
myname=Linus Torvalds
myos = TroubleOS
myno=5
echo "My name is $myname"
echo "My os is $myos"
echo "My number is myno, can you see this number"

Rewrite this script such that it does not result to any error.

Basic Shell Scripting *Property of STI


Page 6 of 6
Operating Systems

Laboratory Exercise 5
More on Shell Scripting
Objectives:
At the end of the session, the students are expected to:
 become familiar with basic shell scripts
 use proper commands for shell scripts construction

Materials:

 1 PC with Linux

Procedures:

ASSUMPTIONS:

 user is already familiar with basic Linux commands


 user is already familiar with vi utility

SHELL ARITHMETIC

Using the expr command, we can also execute mathematical statements in our scripts. Much like in
programming, we can add, subtract, divide, multiply, and even perform modulo.

Examples of expr command:

$ expr 1 + 3
$ expr 2 - 1
$ expr 10 / 2
$ expr 20 % 3
$ expr 10 \* 3
$ echo `expr 6 + 3`

Notes:

expr 20 %3 - remainder read as 20 mod 3 and remainder is 2


expr 10 \* 3 - multiplication uses \* and not * since it is wild card

For the $ echo `expr 6 + 3`, note the following points:

(1) Before the expr keyword, use the ` (back quote) sign, not the ‘ (single quote) sign. Back quote is
generally found with the tilde (~) key or above the TAB key on a PC keyboard.
(2) expr also ends with ` (back quote) sign.
(3) Here, expr 6 + 3 is evaluated to 9, then the echo command prints 9 as sum.
(4) Here, if you use double quote or single quote, it will NOT work.

More on Shell Scripting Page 1 of 3


Operating Systems

Example:

$ echo "expr 6 + 3" <--- It will print expr 6 + 3.


$ echo 'expr 6 + 3' <--- It will print expr 6 + 3.
$ echo `expr 6 + 3` <--- Observes expr first then the value is printed by the echo command.

Remember the following:

QUOTES NAME MEANING


“ Double quote Anything enclosed in double quotes removes the meaning of the
characters (except \ and $)
‘ Single quote Enclosed in single quotes remains unchanged
` Back quote To execute command

Experiment with the expr command. Try mixing multiple mathematical operations in one expr
command.

Convert the following mathematical statements in Linux using expr.

1. 3 * 5 + 2

2. 8 * ( 2 + 3) – 4

3. 4 + (-5 + 3) * 4

4. 7 % 2 * 4 + 3

5. 15 / 3 * 2 + 8

More on Shell Scripting Page 2 of 3


Operating Systems

READ STATEMENT

The read command is used to get input (data from user) from keyboard and store (data) to variable.

Syntax:

read variable1, variable2,...variableN

The following script first asks the user’s name and then waits for the user to enter name via keyboard.
After giving the name, you have to press ENTER key. The entered name through keyboard is stored
(assigned) to variable fname.

Sample script on read:

$ vi Greetings
#
#Script to read your name from keyboard
#
echo “May I know your name pls:”
read fname
echo “Hello $fname, Nice to meet you!”

Your task is to type this script and write down the output.

Write a script that will let the user enter two numbers. The script should be able to get the sum of
the numbers entered and print out the result. Name the file addUp.

Write a script that will let the user enter a number. The script should display the square of the
number entered. Name the file square.

Example:

Enter a number: 3
The square of 3 is 9

Write a script that will compute for the average score of a student. The script should let the user
enter the name of the student and 3 quizzes. The script will simply compute for the average score
and display the name along with the average.

Example:

Enter name of student: Benz Tanyag


Enter q1: 85
Enter q2: 92
Enter q3: 82
The average grade of Benz Tanyag is 86.

More on Shell Scripting Page 3 of 3

You might also like