vvfgc BCA 4th sem Unix lab programs
4th sem UNIX lab programs
UNIX LAB Dept of BCA, VVFGC, Tumkur
1. Use of Basic UNIX Shell Commands:
((man, who, cat, cd, cp, ps, ls, mv, rm, mkdir, rmdir, echo, more, date, time, kill, history, chmod, chown, finger, pwd, cal, logout, shutdown) commands.
1) man
definition: an interface to the online reference manuals.
syntax: $man <command name>
example: man echo
output: ECHO(1) User Commands
NAME
Echo- display a line of text
SYNOPSIS
Echo [SHORT-OPTION]…[STRING]…
Echo LONG-OPTION
DESCRIPTION
Echo the STRING(s) to standard output.
-n do not output the trailing newline
-e enable interpretation of backslash escapes
2) who
definition: show who is logged on
syntax: $ who
example 1: $ who
output:
guest-FW5FLK :0 2020-02-14 19:20 (:0)
guest-FW5FLK pts/2 2020-02-14 19:22 (:0)
example 2: $ who
output:
guest-FW5FLK pts/2 2020-02-14 19:22 (:0)
3) cat
definition: cat command is used to create, concatenate and print on the standard output.
syntax: $ cat [-options]…[file]…
example 1: $cat >vvfgc.txt
vidyavahini first grade college, Tumkur
ctrl+D
example 2: $cat vvfgc.txt
output:
vidyavahini first grade college, Tumkur
example 3: $cat >bca.txt
Bachelor of Computer Applications
ctrl+D
example 4: $cat vvfgc.txt bca.txt
output:
vidyavahini first grade college, Tumkur
Bachelor of Computer Applications
4) cd
definition: change directory
syntax: $ cd <folder name>
example: $ cd folder1
output: ~/folder$
5) cp
definition: copying the contents from one file to another file.
syntax: $ cp sourcefile destinationfile
example :$ cp vvfgc.txt college
output:
vidyavahini first grade college, Tumkur
6) ps
definition: report a snapshot of the current processes.
syntax: $ ps [-options]
example 1: $ ps
output:
PID TTY TIME CMD
2013 pts/2 00:00:00 bash
2551 pts/2 00:00:00 ps
example 2: $ ps -f
description: full listing of the process with PPID
output:
UID PID PPID C STIME TTY TIME CMD
guest-F+ 2013 2001 0 19:22 pts/2 00:00:00 bash
guest-F+ 2551 2013 13 20:16 pts/2 00:00:00 ps –f
7) ls
definition: list the contents of directory
syntax: $ ls [-options]
example 1: $ ls
output:
bca Desktop Downloads folder pictures Templates
vvfgc.txt college
example 2: $ ls –l
description: long listing of directories and files
output:
-rw-rw-r- - 1 guest-UNIX guest-UNIX 34 Feb 14 19:44 bca
-rw-rw-r- - 1 guest-UNIX guest-UNIX 34 Feb 14 19:44 college
drw-rw-r- - 2 guest-UNIX guest-UNIX 34 Feb 14 19:44 Desktop
8) mv
definition: rename a file
syntax: $ mv sourcefile destinationfile
example: $ mv vvfgc.txt vvfgc1.txt
$ ls
output:
bca Desktop Downloads folder pictures Templates
vvfgc1.txt college
9) rm
definition: remove or delete a file
syntax: $ rm
example: $ rm vvfgc1.txt
10) mkdir
definition: used to make a directory
syntax: $ mkdir directory_name
example: $ mkdir college
$ ls
output:
bca Desktop Downloads folder pictures Templates
vvfgc1.txt college
11) rmdir
definition: used to remove a directory
syntax: $ rmdir directory_name
example: $ rmdir college
$ ls
output:
bca Desktop Downloads folder pictures Templates
vvfgc1.txt
12) echo
definition: displays a line of text
syntax: $ echo text
example: $ echo hello world
output:
hello world
13) more
definition: reads files and display the text one screen at a time.
syntax: $more [-option] filename
example 1: $more –p sample.txt
description: -p option clears the screen and displays the output in the new screen.
output:
vidyavahini First Grade college, Tumkur
example 2: $more –f sample.txt
description: -f option does not wrap the long lines and displays them as such.
output:
vidyavahini First Grade college, Tumkur
14) date
definition: prints the system date and time
syntax: $date
example: $date
output: wed Jan 22 09:26:55 IST 2020
15) time
definition: run programs and summarize system resource usage
syntax: $time command_name
example: $time ls
output:
real 0m0.000s
user 0m0.000s
sys 0m0.000s
16) kill
definition: Kill command is a built in command which is used to terminate process manually.
syntax: $kill [-option] PID
example1: $kill –L
description: It display all the available signals.
example2: $kill 6056
description: It kills the process with PID 6056
17) history
definition: history command shows all the last commands that have been recently used.
syntax: $ history
example: $history
output:
1 man more
2 ls
3 cat >sample.txt
4 cat sample.txt
5 more –d sample.txt
6 more –p sample.txt
7 kill –l
8 history
18) chmod
definition: changes the permissions of the files
syntax: $chmod category operator permission filename
example: $chmod ugo+rw file1
output:
$Ls –L file1
-rw-rw-rw- 1 linux linux 40 feb 19 09:16 file1
19) chown
definition: change file owner and group
syntax: $chown [-option] user[:group] file
example: $chown unix file1
output:
$Ls –L file1
-rw-rw-rw- 1 unix linux 40 feb 19 09:16 file1
20) finger
definition: finger command looks up and displays information about system users.
syntax: $finger [-option] user
example 1: $finger –s linux
description: displays the user’s login name, real name, terminal name ane write status.
example 2: $finger –L linux
description: produces multi line format displaying all of the information.
21) pwd
definition: print name of current/working directory
syntax: $pwd
example: $pwd
output:
/tmp/Linux
22) cal
definition: displays a calendar
syntax: $cal
example 1: $cal
description: displays current month calendar
output:
February 2020
Su Mo Tu We Th Fr Sa
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
example 2: $cal feb 2019
description: displays specified month calendar
output:
February 2019
Su Mo Tu We Th Fr Sa
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28
23) logout
definition: logout command allows the user to programmatically logout from session.
syntax: $ logout
example: $ logout
output:
no output screen, current user session will be logged out.
24) shutdown
definition: shutdown command in unix is used to shutdown the system in a safe way.
syntax: $ shutdown [-options] [time] [message]
example 1: $shutdown
output: shutdown will power off the machine
example 2: $shutdown +20
output:
system will shutdown in 20 minutes.
2. Write a Shell Program to check whether a given year is leap year or not.
clear
echo “enter a year”
read y
if [ `expr $y % 4` -eq 0 ]
then
echo “$y is a leap year”
else
echo “$y is not a leap year”
fi
output:
1) Enter a year
1996
1996 is a leap year
2) Enter a year
1997
1997 is not a leap year
3. Write a shell Script program to check whether the given number is even or odd.
clear
echo “enter a number”
read n
if [ `expr $n % 2` -eq 0 ]
then
echo “$n is an even number”
else
echo “$n is an odd number”
fi
output:
1) Enter a number
2
2 is an even number
2) Enter a number
3
3 is an odd number
4. Simple shell script for basic arithmetic operations.
clear
echo “enter two numbers”
read a b
sum=`expr $a + $b`
sub=`expr $a - $b`
mul=`expr $a \* $b`
div=`expr $a / $b`
mod=`expr $a % $b`
echo “sum=$sum”
echo “sub=$sub”
echo “mul=$mul”
echo “div=$div”
echo “mod=$mod”
output:
enter two numbers
12 24
sum=36
sub=-12
mul=288
div=0
mod=12
5. Write a shell script to print the multiplication table for the given number.
clear
echo “enter a number”
read n
echo “multiplication table of $n is”
i=1
while [ $i –Le 10 ]
do
echo “$n X $i = `expr $n \* $i` “
i=`expr $i +1`
done
output:
enter a number
7
7X1=7
7X2=14
7X3=21
7X4=28
7X5=35
7X6=42
7X7=49
7X8=56
7X9=63
7X10=70
6. Write a shell program to check whether a given string is a palindrome or not.
clear
echo “enter a string”
read str
i=1
len=`expr length $str`
hflen=`expr $len / 2`
while [ $i –Le $hflen ]
do
c1=`echo $str|cut –c $i`
c2=`echo $str|cut –c $len`
if [ $c1 != $c2 ]
then
echo “$str is not a palindrome”
exit
fi
i=`expr $i + 1`
len=`expr $len – 1`
done
echo “$str is a palindrome”
Output 1:
Enter a string
Hello
Hello is not a palindrome
Output 2:
Enter a string
Madam
Madam is a palindrome
7. Write a shell program to find the factorial of a given number.
clear
echo “enter a number”
read n
if [ $n –ge 0 ]
then
f=1
i=1
while [ $i –le $n ]
do
f=`expr $f \* $i`
i=`expr $i + 1`
done
echo “factorial of $n is $f”
else
echo “enter a positive number”
fi
output:
Enter a positive number
4
Factorial of 4 is 24
8. Write a shell program to count the number of vowels.
clear
echo “enter a string”
read str
len=$(expr length $str)
count=0
while [ $len –gt 0 ]
do
ch=$(echo $str | cut –c $len)
case $ch in
[aeiouaeiou] )
count=$(($count + 1))
;;
esac
len=$(( $len – 1 ))
done
echo $count
Output: Enter a string
Hello
2
9. Write a shell program to print a string in reverse order.
clear
echo “enter a string”
read str len=`expr length $str`
while [ $len –ge 1 ]
do
ch=`echo $str | cut –c $len`
strrev=$strrev$ch
len=`expr $len – 1`
done
echo “the entered string is $str”
echo “the reverse of the string is $strrev”
Output:
Enter a string
Hello
The entered string is hello
The reverse of the string is olleh
10.Write a shell script to count lines, words and characters in its input
clear
echo “enter a file name”
read fn
echo “number of lines”
wc –L $fn
echo “number of words”
wc –w $fn
echo “number of characters”
wc –c $fn
Output:
enter a file name
file1
number of lines
10 file1
number of words
38 file1
number of characters
156 file1
11.Write a shell script to compute gcd, lcm & of two numbers. Use the basic function to find GCD & LCM of N numbers.
gcdLcm()
{
if [ $1 –gt $2 ]
then
m=$1
n=$2
else
m=$2
n=$2
fi
rem=`expr $m % $n`
while [ $rem –ne 0 ]
do
m=$n
n=$rem
rem=`expr $m % $n`
done
gcd=$n
Lcm=`expr $1 \* $2 / $gcd`
echo “Lcm of $1 and $2 is : $Lcm”
echo “gcd of $1 and $2 is : $gcd”
}
clear
echo “enter two numbers”
read m n
gcdLcm $m $n
Output:
Enter two numbers
12 24
LCM of 12 and 24 is : 24
GCD of 12 and 24 is : 12
12.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.
clear
echo “enter basic salary”
read basic
if [ $basic –Lt 1500 ]
then
gross=$((basic+((basic/100)*10)+(basic/100)*90))
echo “the gross salary is $gross”
fi
if [ $basic –ge 1500 ]
then
gross=$(((basic+500)+(basic/100)*98))
echo “the gross salary is $gross”
fi
Output 1:
enter a basic salary
1300
the gross salary is 2600
Output 2:
enter a basic salary
10000
the gross salary is 20300
13.Write a shell program to display all the files in the current directory.
clear
echo “the List of files in the current directory is as follows”
for i in *
do
echo $i
done
Output:
The List of files in the current directory is as follows
Desktop
Documents
Downloads
allfiles
examples.desktop
gcdlcm
14.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.
clear
for i in *
do
if [ -w $i –a –r $i -a –x $i ]
then
echo “$i have read, write and execute permission”
fi
done
Output:
Desktop have read, write and execute permission
Documents have read, write and execute permission
Downloads have read, write and execute permission
15.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.
clear
echo “Lines between $2 and $3 in file $1 is”
head -$3 $1 | tail -$2
Output:
$sh Lab15 gcdLcm 2 4
If [ $1 –gt $2 ]
then