Anda di halaman 1dari 12

II Year II SEMESTER

T
0

P
3

C
2

FREE OPEN SOURCE SOFTWARE (FOSS) LAB


Objectives:
To teach students various unix utilities and shell scripting
Programs:
1.
Session-1
a)Log into the system
b)Use vi editor to create a file called myfile.txt which contains some text.
c)correct typing errors during creation.
d)Save the file
e)logout of the system
Session-2
a)Log into the system
b)open the file created in session 1
c)Add some text
d)Change some text
e)Delete some text
f)Save the Changes
g)Logout of the system
2.
a)Log into the system
b)Use the cat command to create a file containing the following data. Call it mytable use tabs to separate the
fields.
1425
Ravi
15.65
4320
Ramu
26.27
6830
Sita
36.15
1450
Raju
21.86
c)Use the cat command to display the file, mytable.
d)Use the vi command to correct any errors in the file, mytable.
e)Use the sort command to sort the file mytable according to the first field. Call the sorted file my table
(same name)
f)Print the file mytable
g)Use the cut and paste commands to swap fields 2 and 3 of mytable. Call it my table (same name)
h)Print the new file, mytable
i)Logout of the system.

3.
1)

2)

a)Login to the system


b)Use the appropriate command to determine your login shell
c)Use the /etc/passwd file to verify the result of step b.
d)Use the who command and redirect the result to a file called myfile1. Use the more command to
see the contents of myfile1.
e)Use the date and who commands in sequence (in one line) such that the output of date will display
on the screen and the output of who will be redirected to a file called myfile2. Use the more
command to check the contents of myfile2.
a)Write a sed command that deletes the first character in each line in a

file.
b)Write a sed command that deletes the character before the last character in each line in a file.
c)Write a sed command that swaps the first and second words in each line in a file.
4. a)Pipe your /etc/passwd file to awk, and print out the home directory of each user.
b)Develop an interactive grep script that asks for a word and a file name and then tells how many lines
contain that word.
c)Repeat
d)Part using awk
5. a)Write a shell script that takes a command line argument and reports on whether it is directory, a file,
or something else.
b)Write a shell script that accepts one or more file name as arguments and converts all of them to uppercase,
provided they exist in the current directory.
c)Write a shell script that determines the period for which a specified user is working on the system.
6. a)Write a shell script that accepts a file name starting and ending line numbers as arguments and displays
all the lines between the given line numbers.
b)Write a shell script that deletes all lines containing a specified word in one or more files supplied as
arguments to it.
7. a)Write a shell script that computes the gross salary of a employee according to the following rules:
i)If basic salary is < 1500 then HRA =10% of the basic and DA =90% of the basic.
ii)If basic salary is >=1500 then HRA =Rs500 and DA=98% of the basic
The basic salary is entered interactively through the key board.
b)Write a shell script that accepts two integers as its arguments and computers the value of first number
raised to the power of the second number.
8. a)Write an interactive file-handling shell program. Let it offer the user the choice of copying, removing,
renaming, or linking files. Once the user has made a choice, have the program ask the user for the necessary
information, such as the file name, new name and so on.
b)Write shell script that takes a login name as command line argument and reports when that person logs
in
c)Write a shell script which receives two file names as arguments. It should check whether the two file
contents are same or not. If they are same then second file should be deleted.
9. a)Write a shell script that displays a list of all the files in the current directory to which the user has read,
write and execute permissions.
b)Develop an interactive script that ask for a word and a file name and then tells how many times that word
occurred in the file.
c)Write a shell script to perform the following string operations:
i)To extract a sub-string from a given string.
ii)To find the length of a given string.
10 .Write a C program that takes one or more file or directory names as command line input and reports the
following information on the file:
i)File type ii)Number of links iii)Read, write and execute permissions iv)Time of
last access
(Note : Use stat/fstat system calls)
11. Write C programs that simulate the following unix commands:
a)mv b)cp (Use system calls)
12. Write a C program that simulates ls Command
(Use system calls / directory API)

13. Do the following Shell programs also


1) Write a shell script to check whether a particular user has logged in or not. If he has
logged in, also check whether he has eligibility to receive a message or not
2) Write a shell script to accept the name of the file from standard input and perform the
following tests
on it
a) File executable b) File readable
c) File writable d) Both readable & writable
3) Write a shell script which will display the username and terminal name who login
recently in to the unix system
4) Write a shell script to find no. of files in a directory
5) Write a shell script to check whether a given number is perfect or not

echo Enter a number


read no
i=1
ans=0
while [ $i -le 'expr $no / 2' ]
do
if [ 'expr $no % $i' -eq 0 ]
then
ans='expr $ans + $i'
fi
i='expr $i + 1'
done
if [ $no -eq $ans ]
then
echo $no is perfect
else
echo $no is NOT perfect
fi

6) Write a menu driven shell script to copy, edit, rename and delete a file
7) Write a shell script for concatenation of two strings
echo Enter first string:
read s1
echo Enter second string:
read s2
s3=$s1$s2
len=`echo $s3 | wc -c`
len=`expr $len - 1`
echo Concatinated stringis $s3 of length $len

3) Write a shell script which will display Fibonacci series up to a given number of argument
#!/bin/bash
c=0
a=1

b=1
read -p "Enter limit of fibonacci Series:" n
echo -n "$a "
echo -n "$b "
#Fibonacci series logic
while((c<n))
do
c=$((a+b))
echo -n "$c "
a=$b
b=$c
done
echo -e "\n"

9) Write a shell script to accept student number, name, marks in 5 subjects. Find total,
average and grade. Display the result of student and store in a file called stu.dat
Rules: avg>=80 then grade A
Avg<80&&Avg>=70
then grade B
Avg<70&&Avg>=60
then grade C
Avg<60&&Avg>=50
then grade D
Avg<50&&Avg>=40
then grade E Else grade
F
10) Write a shell script to accept empno,empname,basic. Find DA,HRA,TA,PF using
following rules. Display empno, empname, basic, DA,HRA,PF,TA,GROSS SAL and
NETSAL. Also store all details in a file called emp.dat
Rules: HRA is 18% of basic if basic > 5000
otherwise 550 DA is 35% of basic
PF
is
13% of
basic IT
is 14%
of basic
TA is
10% of
basic
11) Write a shell script to demonstrate break and continue statements

In the following shell script, the break command exits from a while loop when the variable a has
a value of 5 or greater:
#!/bin/sh
a=0

while [ $a -lt 10 ]
do
echo $a
if [ $a -eq 5 ]
then
break
fi
a=`expr $a + 1`
done

...and produces the following output:


0
1
2
3
4
5

This next example uses the form break n to break from a nested loop.
#!/bin/sh
for var1 in 1 2 3
do
for var2 in 0 1 2 3
do
if [ $var1 -eq 2 -a $var2 -eq 1 ]
then
break 2
else
echo "$var1 $var2"
fi
done
done

In the above script, the outer loop sets var1 to 1, then the inner loop sets var2 to the values 0, 1,
2, and 3, respectively. Then the outermost loop sets var1 to 2 and the inner loop sets var2 to the
values of 0 and 1 at which point the conditions are met to execute break 2, which terminates
both loops. It will produce the following output:
1
1
1
1
2

0
1
2
3
0

12) Write a shell script to satisfy the following menu options


a. Display current directory path
b. Display todays date
c. Display users who are connected to the unix system d. Quit
13) Write a shell script to delete all files whose size is zero bytes from current directory
14) Write a shell script to display string palindrome from given arguments

clear
echo "Enter a string to be entered:"
read str
echo
len=`echo $str | wc -c`
len=`expr $len - 1`
i=1
j=`expr $len / 2`
while test $i -le $j
do
k=`echo $str | cut -c $i`
l=`echo $str | cut -c $len`
if test $k != $l
then
echo "String is not palindrome"
exit
fi
i=`expr $i + 1`
len=`expr $len - 1`
done
echo "String is palindrome"

15) Write a shell script which will display Armstrong numbers from given arguments
echo "Enter a number: "
read c
x=$c
sum=0
r=0
n=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
n=`expr $r \* $r \* $r`
sum=`expr $sum + $n`
x=`expr $x / 10`
done
if [ $sum -eq $c ]
then
echo "It is an Armstrong Number."
else
echo "It is not an Armstrong Number."
f
------------------------------------------------------------------------------------------------------------OUTPUT
Enter a number:

153
It is an Armstrong Number.

16) Write a shell script to display reverse numbers from given argument list
clear
echo "Enter a number"
read n
sd=0
rev=0
while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
rev=$(( $rev *\ 10 + $sd ))
n=$(( $n / 10 ))
done
echo "Reverse number of entered digit is $rev"

17) Write a shell script to display factorial value from given argument list

echo "Total no of factorial wants"


read fact
ans=1
counter=0
while [ $fact -ne $counter ]
do
counter=`expr $counter + 1`
ans=`expr $ans \* $counter`
done
echo "Total of factorial is $ans"
------------------------------------------------------------------output
------------------------------------------------------------------Total no of factorial wants
5
Total of factorial is 120

18) Write a shell script which will find maximum file size in the given argument list
19) Write a shell script which will greet you Good Morning, Good Afternoon, Good
Evening and Good Night according to current time
h=`date +%H`
if [ $h -lt 12 ]; then
echo Good morning
elif [ $h -lt 18 ]; then

echo Good afternoon


else
echo Good evening
fi

You could get the time like this:


TIME=$(date "+%H")
if [ $TIME -lt 12 ]; then
echo "Good morning"
elif [ $TIME -lt 18 ]]; then
echo "Good afternoon"
else
echo "Good evening"
fi

20) Write a shell script to sort the elements in a array using bubble sort technique
#!/bin/bash
echo "enter maximum number"
read n
# taking input from user
echo "enter Numbers in array:"
for (( i = 0; i < $n; i++ ))
do
read nos[$i]
done
#printing the number before sorting
echo "Numbers in an array are:"
for (( i = 0; i < $n; i++ ))
do
echo ${nos[$i]}
done
# Now do the Sorting of numbers
for (( i = 0; i < $n ; i++ ))
do
for (( j = $i; j < $n; j++ ))
do
if [ ${nos[$i]} -gt ${nos[$j]} ]; then
t=${nos[$i]}
nos[$i]=${nos[$j]}
nos[$j]=$t
fi
done
done
# Printing the sorted number
echo -e "\nSorted Numbers "

for (( i=0; i < $n; i++ ))


do
echo ${nos[$i]}
done

21) Write a shell script to find largest element in a array


max=0
for n in "${ar[@]}" ; do
((n > max)) && max=$n
done
echo $max

a=(10 30 44 44 69 12 11 100)
max=0
for v in ${a[@]}; do
if (( $v > $max )); then max=$v; fi;
done
echo $max

result in 100

22) Write an awk program to print sum, avg of students marks list
23) Write an awk program to display students pass/fail report
24) Write an awk program to count the no. of vowels in a given file
25) Write an awk program which will find maximum word and its length in the given input
File
26) Write a shell script to generate the mathematical tables.
i=1
echo enter value read a
while [ $i -le $a ] do
j=1
while [ $j -le 10 ] do
echo -e `expr $i \* $j` "\c\t" let j=j+1
done let i=i+1
echo -e "\n" done
27) Write a shell script to sort elements of given array by using selection sort.
#! /bin/bash
ar=(10 30 44 44 69 12 11)
IFS=$'\n'
echo "${ar[*]}" | sort -nr | head -n1

28) Write a shell script to search given number using binary search.
echo Enter array limit
read limit
echo Enter elements
n=1
while [ $n -le $limit ]
do
read num
eval arr$n=$num
n=`expr $n + 1`
done
echo Enter key element
read key
low=1
high=$n
found=0
while [ $found -eq 0 -a $high -gt $low ]
do
mid=`expr \( $low + $high \) / 2`
eval t=\$arr$mid
if [ $key -eq $t ]
then
found=1
elif [ $key -lt $t ]
then
high=`expr $mid - 1`

else
low=`expr $mid + 1`
fi
done
if [ $found -eq 0 ]
then
echo Unsuccessfull search
else
echo Successfull search
fi

29) Write a shell script to find number of vowels, consonants, numbers, white spaces
and special characters in a given string.

#!/bin/bash
# Shell program to count
# Vowels, blank spaces, characters, number of line and symbols
# A, E, I, O, U
# -------------------------------------------------------------------# This is a free shell script under GNU GPL version 2.0 or above
# Copyright (C) 2005 nixCraft project.
# Feedback/comment/suggestions : http://cyberciti.biz/fb/
#
------------------------------------------------------------------------# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
#
-------------------------------------------------------------------------

file=$1
v=0

if [ $# -ne 1 ]

then
echo "$0 fileName"
exit 1
fi
if [ ! -f $file ]
then
echo "$file not a file"
exit 2
fi

# read vowels
exec 3<&0
while read -n 1 c
do
l="$(echo $c | tr '[A-Z]' '[a-z]')"
[ "$l" == "a" -o "$l" == "e" -o "$l" == "i" -o "$l" == "o" -o "$l" == "u"
] && (( v++ )) || :
done < $file

echo "Vowels : $v"


echo "Characters : $(cat $file | wc -c)"
echo "Blank lines : $(grep

-c '^$' $file)"

echo "Lines : $(cat $file|wc -l )"

30) Write a shell script to lock the terminal.

Anda mungkin juga menyukai