Lab Manual-CSI 310-CSE 4510 - 2
Lab Manual-CSI 310-CSE 4510 - 2
Variables.................................................................................................................... 17
Read ...........................................................................................................................18
Shell Arithmetic ........................................................................................................18
2
Expression Evaluation ........................................................................................................................ 19
If‐Else........................................................................................................................ 19
Comparison Rules .............................................................................................................................. 20
File Conditional ................................................................................................................................. 20
While ........................................................................................................................ 24
Example 01 (while) ............................................................................................................................. 24
Until ......................................................................................................................... 25
Example 01 (until) ............................................................................................................................... 25
Functions ................................................................................................................. 25
Example 01 (function) ....................................................................................................................... 26
Example 02 (function) ....................................................................................................................... 26
Example 03 (function) ........................................................................................................................27
Example 04 (function)........................................................................................................................27
Example 05 (function) ....................................................................................................................... 28
3
Lab 01: Basic Linux Shell Commands
Topics to Shell commands on manual, user, editor, view text, directory and file
cover permissions, directory and file operations, file view commands, standard
I/O/E, redirection, piping
Online N/A
Assignment N/A
COMMAND DETAILS
Introduction
Many people says that Linux is a command based operating system.
But it is not true. Linux is a GUI based OS with a Shell which is more powerful
than its counterpart in Windows OS.
Identity
Manual
For each command Linux contains manual. To view the manual : man name
man uname
Editors
To view files a large number of editors are available. They are:
kwrite
emacs
gedit
vi
To view : editorname filename
kwrite file.txt
4
User
In Linux, root is the most powerful user. But other users can be created easily.
Each linux user must be under certain group.
View Text
5
file or directory [name of file or directory to change]
chmod go+rw file1 file2 add read and write access for
group and others for files 'file1' and 'file2’
chmod a+rwx file1 add read, write and execute for everyone for
'file1‘.
chmod 555 file1
Directory Operations
6
‐v [print a message for each created directory
To delete an empty directory : rmdir
or rotates the stack making the new top of the stack the current working
directory
To moves to the directory at the top of the stack as well as to remove the topmost
entry : popd
Effect:
File Operations
To copy a file : cp
Copy source to destination or multiple sources to directory
‐u [ copy only when the src file is newer than the dest file or when the dest file
is missing
7
To remove a file or directory : rm
‐u [ move only when the src file is newer than the dest file or when the dest file
is missing
To concat files and print on the standard output : cat file1 file2 file3 …
8
File Viewing
To print the number of lines, words and bytes in files : wc file1 file2 file3 …
print byte, word, and newline counts for each file and a total line if more than one
file is specified.
Standard I/O/E
By default, three default files known as standard files are automatically opened when a
command is executed.
They are standard input (stdin) ,standard output (stdout) and standard
9
error (stderr).
For example, the command ls ‐a scans the current directory and collects a list of
all the files,
produces a human readable list, and outputs the result to the terminal window.
Redirection
Linux redirection features can be used to detach the default files from stdin,
stdout and
stderr and attach other files to them.
Input redirection:
< ‐ get input from file instead of the keyboard
Output redirection:
> ‐ send output to file instead of the terminal window
Append output:
>> ‐ command is used to append to a file if it already exists
Piping
The input of a command may come from the output of another command.
This is accomplished with the ‘ | ‘ pipe operator.
The input of a command may come from the output of another command.
This is accomplished with the ‘ | ‘ pipe operator.
10
Online See sample online 1
Assignment N/A
External Linux grep command
Link https://www.computerhope.com/unix/ugrep.htm
Regular expression
https://www.computerhope.com/unix/regex-quickref.htm
SAMPLE ONLINE: 01
Consider two task files provided you on elms. Perform following operations using those
files where required:
1. create a file (myfile) which is inside the folder (myfolder1) that is inside another folder
(myfolder2) in C drive.
2. combine taskfile1 and taskfile2 contexts in myfile and then show myfile content.
3. count the total number of lines, words, characters in taskfile1 and taskfile2.
4. count the total number of lines, words, characters in taskfile1 and taskfile2, and print
the info in myoutput.txt
5. sort the contexts of taskfile1 and taskfile2 in reverse alphabetic order
6. sort the contexts of taskfile1 and taskfile2, and then copy the 3-10 lines to myfile, then
show the content of myfile.
7. print your name in command prompt, and copy that name and today's date in
myoutput2.txt
8. print your name in command prompt but copy only today's date in myoutput3.txt
9. delete output.txt
10. delete myfolder1
Task file 01
August 25, 1991 Dear friend,
I am writing to you because she said you listen and understand
and didn’t try to sleep with that
person at that party even though you could have. Please don’t try
to figure out who she is because
then you might figure out who I am, and I really don’t want you to
do that. I will call people by
different names or generic names because I don’t want you to find
me. I didn’t enclose a return address
for the same reason. I mean nothing bad by this. Honest.
I just need to know that someone out there listens and
understands and doesn’t try to sleep with
people even if they could have. I need to know that these people
exist.
I think you of all people would understand that because I think
you of all people are alive and
appreciate what that means. At least I hope you do because other
people look to you for strength
11
and friendship and it’s that simple. At least that’s what I’ve
heard.
So, this is my life. And I want you to know that I am both happy
and sad and I’m still trying to
figure out how that could be.
I try to think of my family as a reason for me being this way,
especially after my friend Michael
stopped going to school one day last spring and we heard Mr.
Vaughn’s voice on the loudspeaker.
“Boys and girls, I regret to inform you that one of our students
has passed on. We will hold a
memorial service for Michael Dobson during assembly this Friday.”
I don’t know how news travels around school and why it is very
often right. Maybe it was in the
lunchroom. It’s hard to remember. But Dave with the awkward
glasses told us that Michael killed
himself. His mom played bridge with one of Michael’s neighbors and
they heard the gunshot.
Task file 02
Fat Kid Rules the World by K. L. Going
Nailed by Patrick Jones
How to Say Goodbye in Robot by Natalie Standiford
Rats Saw God by Rob Thomas
Nick and Norah's Infinite Playlist by David Levithan and Rachel
Cohn
Story of a Gril by Sara Zarr
I am the messenger by Zusak
Born to Rock by Korman
Smack by Burgess
Such a Pretty Girl by Weiss
Getting the Girl by Zusak
Hard Love by Wittlinger
Repossessed by Jenkins
You Don’t Know Me by Klass
Wasteland by Block
It’s Kind of a Funny Story by Vizzini
COMMAND DETAILS
Grep
12
grep matches a pattern in a given a list of files or standard input and
outputs only the matching lines.
grep pattern filename
grep abc file.txt
grep patterns are case sensitive by default.
Some options
‐i [ case insensitive search ]
‐c [count of total matches]
‐E [regular expressions can be provided as patterns]
‐n [display the line numbers of the matched lines]
Find
13
Resource Link N/A
SAMPLE ONLINE: 02
Obtain the file Grepdata.txt.
Once you have the file, write a series of grep statements that do the following:
1. Print all lines that contain a phone number with an extension (the letter x or X
followed by four digits).
2. Print all lines that begin with three digits followed by a blank. Your answer must
use the \{ and \} repetition specifier.
3. Print all lines that contain a date. Hint: this is a very simple pattern. It does not
have to work for any year before 2000.
4. Print all lines containing a vowel (a, e, i, o, or u) followed by a single character
followed by the same vowel again. Thus, it will find “eve” or “adam” but not
“vera”. Hint: \( and \)
5. Print all lines that do not begin with a capital S.
6. Write grep statements that use command-line options along with the pattern to
do the following:
7. Print all lines that contain CA in either uppercase or lowercase.
8. Print all lines that contain an email address (they have an @ in them), preceded
by the line number.
9. Print all lines that do not contain the word Sep. (including the period).
10. Print all lines that contain the word de as a whole word.
Save these statements in a file named in the form
lastname_firstname_grep1.sh
and email it to me. So, for example, if your name is Joe Doakes, your file would be
named doakes_joe_grep1.sh.
Your patterns should work in any generic file of this sort. They should not be dependent
upon the data in this particular file; if I add more lines of the same form to the file, your
patterns should still work.
Grepdata.txt file
Sep. 17, 2013
Esperanza High School Sep. 24, 2013
1830 N. Kellog Dr. Sonora High School
Anaheim, CA 92807-1281 401 S. Palm St.
Steve Marshal La Habra, CA 90631
714-555-7870 X7310 Carl Hohl (aka Krazy Rabbit)
[email protected] 562-555-9800
Brian Fortenbaugh
714-555-7870 x7309 Oct. 1, 2013
14
Lakewood High School El Dorado High School
440 Briercrest Ave. 1651 N. Valencia Ave.
Lakewood, CA 90713-2013 Placentia, CA 90631
Andy Miramontes Steve Lawson
562-555-1281 714-555-5350 x2134
[email protected]
Oct. 8, 2013
North Torrance High School Nov. 12, 2013
2013 W. 182nd Rosemead High School
Torrance, CA 90504 9063 E. Sepulveda Dr.
Don Henderson Rosemead, CA 91770
310-555-4412 Daren de Heras
[email protected]
Oct. 15, 2013
SAMPLE ASSIGNMENT: 01
Similar to online 1 and 2
We have seen some basic shell commands, it’s time to move on to scripts.
There are two ways of writing shell programs.
You can type a sequence of commands and allow the shell to execute
them interactively.
You can store those commands in a file that you can then invoke as a
program. This is known as Shell Script.
We will use bash shell assuming that the shell has been installed as /bin/sh and
that it is the default shell for your login.
15
WHY SHELL SCRIPT?
Shell script can take input from user, file and output them on screen.
Useful to create own commands.
Save lots of time.
To automate some task of day today life.
System administration part can be also automated.
chmod +x script_name
Execute your script
./script_name
#!/bin/bash
echo "Hello User"
echo "See the files in current directory"
ls
16
VARIABLES
In Linux (Shell), there are two types of variable:
System variables ‐ created and maintained by Linux itself.
echo $USER
echo $PATH
User defined variables ‐ created and maintained by user.
All variables are considered and stored as strings, even when they are assigned
numeric values.
Variables are case sensitive.
If you enclose a $variable expression in double quotes, it’s replaced with its
value when the line is executed.
If you enclose it in single quotes, no substitution takes place.
You can also remove the special meaning of the $ symbol by prefacing it with a \.
myvar=”Hello”
echo $myvar [ Hello]
echo “$myvar” [ Hello]
echo ‘$myvar’ [ $myvar ]
echo \$myvar [ $myvar ]
17
READ
To read user input from keyboard and store it into a variable use read
var1,var2,.....varn
#!/bin/bash
echo ‐n "Enter your
name:”
read name
SHELL ARITHMETIC
The expr command evaluates its arguments as an expression.
It is commonly used for simple arithmetic operations.
#!/bin/bash
expr 1 + 1
expr 1 ‐ 1
expr 1 \* 1
expr 1 / 1
var=`expr 1 + 1`
x=1
x=`expr $x + 1`
18
Expression Evaluation
IF‐ELSE
if [ conditiong1 ]; then
statement1
elif [ condition2 ]; then
statement2
else
statement3
fi
It is must to put spaces between the [ braces and the condition being checked.
If you prefer putting then on the same line as if, you must add a semicolon to
separate the test from the then.
19
Comparison Rules
File Conditional
20
#!/bin/bash
echo "Enter first number "
read num1
echo "Enter second number"
read num2
if [ $num1 ‐gt $num2 ] ; then
echo "$num1 is greater than $num2"
elif [ $num1 ‐lt $num2 ] ; then
echo "$num1 is less than $num2"
else
SAMPLE ASSIGNMENT: 02
Q1. How many vowels are there in the 7th line of file1? Write a shell script to get the
answer.
Q2. Write a shell script that takes input names of three text files, sorts lines of first and last
file in lexicographic order and the 2nd file in reverse lexicographic order. Your script should
print the first word and last word of each line from the combined file and counts total
number of words.
Q3. Write a shell script that takes two string in the command prompt, makes a folder with
the first argument, creates a text file within the folder and then takes three line as input and
appends it to the file.
Q4. Find all words of the file2 that has a repetition of character.
Q5. Write a shell script that asks you for a command name and generates a file in the home
21
CASE
case $var in
condition1) statement ;;
condition2) statement ;;
*) statement3
esac
You can put multiple statements between each pattern and the next, so a double
semicolon is needed to mark where one statement ends and the next pattern begins
Example 01 (case)
#!/bin/sh
echo “Is it morning? Please answer yes or no” read timeofday
case “$timeofday” in
esac
……………………………………………………………………………………………………………………………………………….
22
Example 02 (case)
#!/bin/sh
echo “Is it morning? Please answer yes or no” read
timeofday
case “$timeofday” in
yes | y | Yes | YES ) echo “Good Morning”;;
n* | N* ) echo “Good Afternoon”;;
*) echo “Sorry, answer not recognized”;;
esac
FOR
23
Example 01 (for)
#!/bin/bash
Example 02(for)
#!/bin/bash
for i in `ls`
do
echo $i
done
Example 03(for)
for(( i=0;i<=50;i++))
do
echo $i
done
WHILE
while condition do
statements
done
Example 01 (while)
#!/bin/bash
password="abc"
24
echo "Enter password"
read pass
while [ $pass != $password ]
do
UNTIL
until condition do
statements
done
Example 01 (until)
#!/bin/bash
password="abc"
echo "Enter password"
read pass
until [ $pass = $password ]
do
FUNCTIONS
Functions can be defined in the shell and it is very useful to structure the code.
To define a shell function simply write its name followed by empty parentheses and
enclose the statements in braces.
25
function_name () {
statements
}
Function must be defined before one can invoke it.
Example 01 (function)
#!/bin/sh
foo() {
Output
script starting
script ending
When a function is invoked, the parameters to the script [$*, $#, $1, $2] and so on
are replaced by the parameters to the function.
When the function finishes, they are restored to their previous values.
Example 02 (function)
#!/bin/bash
showarg()
a=1
26
for i in $*
do
a=`expr $a + 1`
done
showarg $*
echo "Total:$#"
Qee
Example 03 (function)
f(){ var="123“; }
f
echo $var
Example 04 (function)
f(){ echo "123"; }
result="$(f)"
27
Example 05 (function)
#!/bin/sh
yes_or_no()
{
echo "Is your name $* ?"
echo "Enter yes or no:"
read x
case “$x” in
y | yes ) return 0;;
n | no ) return 1;;
esac
}
if yes_or_no “$1”
then
echo “Hi $1, nice name”
else
echo “Never mind”
fi
……………………………………………………………………………………………………………………………………………….
f()
statements
28
The parameter must be passed every time a function is invoked either from main or from
any other functions
SAMPLE ASSIGNMENT: 03
Statement Implement FCFS scheduling algorithm and print performance
statistics (average turnaround time) after certain time interval.
29
Solution to sample assignment 03
FCFS.java
package fcfs;
import java.util.Comparator;
import java.util.PriorityQueue;
/**
* @param args the command line arguments
*/
static PriorityQueue<Process> processQueue = new PriorityQueue<Process>(10, new
Comparator<Process>() {
public int compare(Process process1, Process process2) {
return (int)(process1.getArrivalTime()-process2.getArrivalTime());
}
});
processQueue.add(new Process(1,3,2,globalTimer));
processQueue.add(new Process(2,6,3,globalTimer));
processQueue.add(new Process(3,1,4,globalTimer));
processQueue.add(new Process(4,4,5,globalTimer));
while(true){
if(checkIfNewProcessArrived()){
readyQueue.add(processQueue.poll());
}
}
if(!readyQueue.isEmpty())
runProcessInCpu();
else {
30
System.out.println("No process is in Ready Queue");
System.out.println("Global time: "+globalTimer.time);
globalTimer.time++;
}
}
}
if(!processQueue.isEmpty()){
if(processQueue.element().getArrivalTime()<globalTimer.time)
return true;
}
return false;
}
Process.java
package fcfs;
public class Process {
int id;
int arrivalTime;
int duration;
GlobalTimer globalTimer;
31
public int getId() {
return id;
}
GlobalTimer.java
package fcfs;
public class GlobalTimer {
int time;
this.time=initialTime;
}
32
SAMPLE ASSIGNMENT: 04
Statement Implement producer-consumer ipc problem
Discussion In computing, the producer–consumer problem (also known as the
bounded-buffer problem) is a classic example of a multi-process
synchronization problem. The problem describes two processes, the
producer and the consumer, who share a common, fixed-size buffer
used as a queue. The producer's job is to generate data, put it into the
buffer, and start again. At the same time, the consumer is consuming
the data (i.e., removing it from the buffer), one piece at a time. The
problem is to make sure that the producer won't try to add data into
the buffer if it's full and that the consumer won't try to remove data
from an empty buffer.
import java.util.Queue;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
33
}
@Override
public void run() {
int item;
while(true){
try {
item = rand.nextInt(50) + 1;
empty.down();
mutex.down();
buffer.add(item);
System.out.println(Thread.currentThread().getName()+" produced: " + item);
mutex.up();
full.up();
Thread.sleep(rand.nextInt(30) + 1);
} catch (InterruptedException ex) {
Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
Consumer.java
import java.util.logging.Logger;
while(true){
int item;
try {
34
full.down();
mutex.down();
item=buffer.remove();
System.out.println(Thread.currentThread().getName()+" consumed: " + item);
mutex.up();
empty.up();
Thread.sleep(rand.nextInt(10) + 1);
} catch (InterruptedException ex) {
Logger.getLogger(Producer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Semaphore.java
package producerconsumer;
35
ProducerConsumer.java
package producerconsumer;
import java.util.LinkedList;
import java.util.Queue;
/**
* @param args the command line arguments
*/
static int N =5;
public static Semaphore mutex =new Semaphore(1,1, "mutex");
public static Semaphore empty =new Semaphore(N,N, "empty");
public static Semaphore full =new Semaphore(0,N, "full");
public static Queue<Integer> buffer = new LinkedList<Integer>();
36
Lab 11: Presentation
Will be scaled to 10%
37