[go: up one dir, main page]

0% found this document useful (0 votes)
30 views17 pages

Assign 2 ND

The document provides instructions for tasks to be completed in an OS lab session. It includes commands to use like echo, grep, tail and describes creating files, comparing files, searching files and writing shell scripts to perform operations on files.

Uploaded by

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

Assign 2 ND

The document provides instructions for tasks to be completed in an OS lab session. It includes commands to use like echo, grep, tail and describes creating files, comparing files, searching files and writing shell scripts to perform operations on files.

Uploaded by

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

Learn the following for the OS lab

The directory structure of UNIX, absolute path, relative path, hidden files

Special characters of the UNIX shell----wild card characters----*, ?, [a-z], ; etc...

Terminal control keys---CTRL-d, CTRL-h, etc...

Commands---echo, cmp, diff, wc, grep, tail, od, file, du,, sed,awk

Shell programming----positional parameters and arguments--for

The tasks for the lab session

1. Create a text file sourcefile using an editor and count the number of
characters,words and lines in it.

2. Display only the last 3 lines of the above file.

3. Copy the above file into another destinationfile and edit the second file by
inserting a few more words, lines in it.
4. Compare sourcefile with destinationfile using cmp and diff and note the
difference between the two commands.

5. Display the lines having a specific word in sourcefile

6. Display the lines not having a specific word in sourcefile

7. How to go back to your home directory from any other place in the directory
structure?

8. How to retrieve the previous Shell commands that you had used in the current
log-in session?
9. Display the calendar of a particular month.

10. Check how od works on sourcefile.

11. Create a executefile using an editor which the following shell commands in
sequence: Who, sort, calendar, cat, grep with appropriate parameters / arguments
( for example cat requires a file name …)

12. Find out the “type” of executefile.


13. Make executefile an executable file and execute it.

14. Check how od works on executefile.

15. Write a shell script which takes a sequence of filenames as arguments and does
the following on each of them:
Display the contents of the file; Count the number of characters, words, and lines
in it; Display only those lines that contains the word “the” in it; Display only the
last “5” lines of it
CODE:
Shsc1.sh:
#!/bin/bash
echo "Enter the filename:"
read filename
if [ -r "$filename" ]; then
o1=$(cat "$filename")
echo "The contents of the file are:"
echo $o1
else
echo "File not found"
fi
shsc2.sh:
#!/bin/bash
echo "Enter the filename:"
read filename
if [ -r "$filename" ]; then
o2=$(wc "$filename")
echo "The number of words,lines and characters are:"
echo $o2
else
echo "File not found"
fi
shsc3.sh:
#!/bin/bash
echo "Enter the filename:"
read filename
if [ -r "$filename" ]; then
o3=$(grep -i "the" "$filename")
echo "The lines having 'the' in the file are: "
echo $o3
else
echo "File not found"
fi

shsc4.sh:
#!/bin/bash
echo "Enter the filename:"
read filename
if [ -r "$filename" ]; then
o4=$(tail -5 "$filename")
echo "The last 5 lines of the file are:"
echo $o4
else
echo "File not found"
fi
16. Refine the above script to accept the word to be looked for and the number of
lines to be displayed.

Shsc5.sh:
#!/bin/bash

echo "Enter the filename:"


read filename

echo "Enter the word to be searched for:"


read word

echo "Enter the number of lines to be displayed:"


read num

if [ -r "$filename" ]; then
o1=$(grep -i "$word" "$filename")
o2=$(sed -n "${num}p" "$filename")
echo "The output is:"
echo "$o1"
echo "$o2"
else
echo "File not found"
fi
17. Modify the above script to also display the first ” n” lines of any file.

Shsc6.sh:
#!/bin/bash

echo "Enter the filename:"


read filename

echo "Enter the word to be searched for:"


read word

echo "Enter the number of lines to be displayed:"


read num

if [ -r "$filename" ]; then
if [ "$num" -lt 0 ]; then
o2=$(head "$filename" "$((-num))")
else
o2=$(sed -n "${num}p" "$filename")
fi

o1=$(grep -i "$word" "$filename")

echo "The output is:"


echo "$o1"
echo "$o2"
else
echo "File not found"
fi
Grep Exercises.

1) Create an employee database (.txt file) which contains employee personal record. The
personal record includes name, DOB ( DD/MM/YYYY), address, phone number.
a) Print all Employee record that contain a phone number with an
Extension

b) Print all Employee record that do not begin with a capital A.

c) Print all Employee record whose name ends with Kumar.


d) Print all the Employee names

e) Print all the Mobile number and the land line number with name
f) Find the eldest and the youngest employee from the record set.
g) Find the age of all the employee.

SHELL SCRIPTS:

1. Accept a filename as input by displaying an appropriate prompt message.

Code:
#/bin/bash
read -p "Enter file name:" file
if [ -f "$file" ] ;then
echo "File exists"
else
echo "File does not exist"
fi

2. Display the content of the file if it exists else display an error message.

Code:
#/bin/bash
read -p "Enter file name:" file
if [ -f "$file" ] ;then
cat $file
else
echo "File does not exist"
fi
3. Check whether a number is prime or not.
Code:

#!/bin/bash
read -p "Enter a number:" number
isprime=true
for ((i=2;i<$number;i++))
do
if [ $((number%i)) -eq 0 ]
then
isprime=false
break;
fi
done
if [ "$isprime"=true ]
then
echo "$number is prime"
else
echo "Not a prime number."
fi

4. Factorial of a number.
Code:

#!/bin/bash
fact=1
read -p "enter a number:" num
for ((i=1;i<=$num;i++))
do
fact=$((fact*i))
done
echo "factorial:" $fact

5. Sum of digits of a number.

Code:
read -p "Enter a number:" number
sum=0
temp=$number
while [ $temp -gt 0 ]
do
digit=$((temp%10))
sum=$((sum+digit))
temp=$((temp/10))
done
echo "The sum of digits of the number: $sum"

6. Check whether the string is palindrome.

Code:
#!/bin/bash
read -p "Enter a string:" word
reverse=$(echo "$word" | rev )
if [ "$word" == "$reverse" ]
then
echo "The word $word is a palindrome"
else
echo "Not a palindrome."
fi

7. Count the number of characters ,words and lines in it and display each in seperate line.

Code:
#!/bin/bash
read -p "Enter the file name:" file
if [ -f $file ]
then
echo "No of words in the file:"
wc -w <$file
echo "No of characters in the file:"
wc -m <$file
echo "No of lines in the file:"
wc -l <$file
fi

8.modify the above scrip to accept one or more file to command line and repeat the
above.

Code:
#!/bin/bash
for file in "$@"
do
echo "No of words in the file $file :"
wc -w <$file
echo "No of characters in the file $file :"
wc -m <$file
echo "No of lines in the file $file :"
wc -l <$file
done

9. Accept the word as an input. Count and print the no of lines the word is present.
Code:

#!/bin/bash
read -p "Enter the file name:" file
read -p "Enter the word:" word
if [ -f $file ]
then
count=$(grep -c "$word" $file )
echo "The word $word is present in $count lines in the file $file."
else
echo "Error."
fi
10. Write a script to display current path.
Code:
#!/bin/bash
echo "printing the current working directory:"$PWD

11. Shell script that list the files by modification and addition time.

Code:

#!/bin/bash

echo "Files sorted by modification time:"


ls -lt

echo "Files sorted by creation time:"


stat -c "%y %n" * | sort -n
12. Average of n numbers.
Code:
#!/bin/bash

read -p "Enter the number of values: " num


declare -a numbers
for ((i=0; i<num; i++))
do
read num
numbers[$i]=$num
done
sum=0
for n in ${numbers[@]}
do
sum=$((sum+n))
done
avg=$(echo "scale=2; $sum/$num" | bc)

echo "Average: $avg"


13. Median of n numbers.
Code:

#!/bin/bash

read -p "Enter the number of values: " num


declare -a numbers
for ((i=0; i<num; i++))
do
read input_num
numbers[$i]=$input_num
done

sorted_numbers=($(printf "%s\n" "${numbers[@]}" | sort -n))


middle_index=$((num / 2))

if (( num % 2 == 0 )); then


median=$(echo "scale=2; (${sorted_numbers[middle_index - 1]} + $
{sorted_numbers[middle_index]}) / 2" | bc)
else
median=${sorted_numbers[middle_index]}
fi

echo "Median: $median"

14. Find the min and max of n numbers


Code:
#!/bin/bash

read -p "Enter the number of values: " num


declare -a numbers
for ((i=0; i<num; i++))
do
read input_num
numbers[$i]=$input_num
done
min=${numbers[0]}
max=${numbers[0]}
for num in "${numbers[@]}"
do
if (( num < min )); then
min=$num
fi

if (( num > max )); then


max=$num
fi
done

echo "Minimum: $min"


echo "Maximum: $max"

You might also like