UNIX Lab Manual
UNIX Lab Manual
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
2. Write a Shell Program to check whether a given year is leap year or not.
3. Write a shell Script program to check whether the given number is even or odd.
4. Write a shell program to check whether a given string is a palindrome or not.
5. Simple shell script for basic arithmetic operations
6. Write a shell program to count the number of vowels.
7. Write a shell script to print the multiplication table for the given number.
8. Write a shell program to find the factorial of a given number.
9. Write a shell script to count lines, words and characters in its input
10. Write a shell program to print a string in reverse order.
11. Write a shell program to display all the files in the current directory.
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.
13. Write a shell script to compute gcd lcm & of two numbers. Use the basic function to find gcd & LCM of
N numbers.
14. 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.
15. 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.
SSCASCW, Tumkur 1
UNIX
UNIX LAB MANUAL
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.
man ls will explain about the ls command and how you can use it.
man -k pattern command will search for the pattern in given command.
Who command
who command displays information about the current status of system.
who options file
Who as default prints login names of users currently logged in.
Options
-a use all options.
-b Report information about last reboot.
-d report expired processes.
-H print headings.
-p report previously spawned processes.
-u report terminal usage.
cat command
This command takes a copy of a file and sends it to the standard output (i.e., on terminal )
It is used either to read files or to concatenate together copies of several files and copy it to the
new file , also it is used to create files
general syntax
view file : $cat file_name
create file : $cat>file_name
copy file : $cat file_1 file_2 > new
edit file : $cat>>file_name
Example
$ cat file1 => it display the contents of file file1
$ cat file1 file2 > files => it concatenates file1 file2 into files
$ cat file1>>>file2 => it upends file1 at the end of file2
SSCASCW, Tumkur 2
UNIX
UNIX LAB MANUAL
Cd command
cd command is used to point the control to a given directory.
general syntax
$ cd dir_name
Example
$ cd => changes directory t your home directory
$ cd .. => it moves to the parent directory to your current directory
$ cd dir => it changes directory so that dir is your new current directory
PS command
ps command is probably the most useful command for systems administrators. It reports information on
active processes.
ps options
options.
Ls command
This command list names of the files in a directory it can be used to obtain information of
the files and the directories in side it.
General syntax
$ ls dir
$ ls -l dir
$ ls -a dir
Example
$ ls -l dir => long list of the names of files and directories within the directory dir
$ ls -a dir => list of names of the files and directory with in the directory dir along with the files
and directories whose name starts with .(dot)
SSCASCW, Tumkur 3
UNIX
UNIX LAB MANUAL
Mv command.
mv command is used to move a file from one directory to another directory or to rename a file.
Some examples:
Rm command.
To delete files use rm command.
Options:
Mkdir command
mkdir command is used to create new directory
general syntax
$ mkdir dir_name
example
$ mkdir UCST => it will make a new directory called UCST
Rmdir commands
It’s used to remove or delete empty directory
general syntax
$ rmdir dir_name
Example
$ rmdir UCST => it will remove the empty directory UCST
Echo command
echo command in shell programming.
SSCASCW, Tumkur 4
UNIX
UNIX LAB MANUAL
More command.
more command will display a page at a time and then wait for input which is spacebar. For example if
you have a file which is 500 lines and you want to read it all. So you can use more filename
Date command.
Date displays todays date, to use it type date at prompt.
Sun Dec 7 14:23:08 EST 1997
is similar to what you should see on screen.
Chmod command.
chmod command is used to change permissions on a file.
for example if I have a text file with calender in it called cal.txt.
initially when this file will be created the permissions for this file depends upon umask set in your profile
files. As you can see this file has 666 or -rw-rw-rw attributes.
ls -la cal.txt
In this line above I have -rw-rw-rw- meaning respectively that owner can read and write file, member of
the owner's group can read and write this file and anyone else connected to this system can read and
write this file., next ssb is owner of this file dxidev is the group of this file, there are 135 bytes in this
file, this file was created on December 3 at time16:14 and at the end there is name of this file. Learn
to read these permissions in binary, like this for example Decimal 644 which is 110 100 100 in binary
meand rw-r--r-- or user can read,write this file, group can read only, everyone else can read only.
Similarly, if permissions are 755 or 111 101 101 that means rwxr-xr-x or user can read, write and
execute, group can read and execute, everyone else can read and execute. All directories have d in front
of permissions. So if you don't want anyone to see your files or to do anything with it use chmod
command and make permissions so that only you can read and write to that file, i.e. chmod 600
filename.
Chown command.
chown command to change ownership of a file or directory to one or more users.
Syntax is
chown options newowner files
Options
SSCASCW, Tumkur 5
UNIX
UNIX LAB MANUAL
Finger command.
finger command.
Pwd command.
pwd command will print your home directory on screen, pwd means print working directory.
/u0/ssb/sandeep
Calendar command
calendar command reads your calendar file and displays only lines with current day.
For example in your calendar file if you have this
On dec 20th the first line will be displayed. you can use this command with your crontab file or in your
login files.
Shutdown command.
Shutdown command can only be executed by root. To gracefully bring down a system, shutdown
command is used.
options.
SSCASCW, Tumkur 6
UNIX
UNIX LAB MANUAL
2. Write a Shell Program to check whether a given year is leap year or not.
clear
echo “Enter a year”
read year
r=`expr $year % 4`
if [ $r –eq 0 ]
then
echo “Leap Year”
else
echo “Not a Leap Year”
fi
SSCASCW, Tumkur 7
UNIX
UNIX LAB MANUAL
5. Simple shell script for basic arithmetic operations.
Clear
echo “enter your choice ”
echo “1: Arithmetic Operations”
echo “2: Logical Operations”
read ch
case “$ch” in
1) echo “Arithmetic operations”
echo “enter two numbers”
read a b
echo “enter the arithmetic operation”
echo “ add: Addition ”
echo ”sub: Subtraction”
echo “mul: Multiplication”
echo “div: Division”
read op
case “$op” in
add) sum=`expr $a + $b`
echo “addition of $a and $b is $sum”
;;
sub) diff=`expr $a - $b`
echo “difference between $a and $b is $diff”
;;
mul) pro=`expr $a \* $b`
echo “product of $a and $b is $pro”
;;
Div)
div=`expr $a/$b`
echo “division of $a by $b gives $div”
;;
esac
SSCASCW, Tumkur 8
UNIX
UNIX LAB MANUAL
6. Write a shell program to count the number of vowels
clear
echo “Enter a String”
read str
i=1
v=0
len=`expr length
$str` while [ $i –le
$len ] do
ch=`expr $str | cut –c $i`
case “$ch” in
[aeiouAEIOU] )
v=`expr $v + 1`
esac
i=`expr $i + 1`
done
echo “Number of Vowels in given string $v”
9. Write a shell script to count lines, words and characters in its input
clear
echo “Enter a String”
read str
count=`expr $str |wc`
echo “String count is $count”
SSCASCW, Tumkur 9
UNIX
UNIX LAB MANUAL
10. Write a shell program to print a string in reverse order.
clear
echo “Enter a String”
read str
len=`expr length $str`
rstr=””
while [ $len –gt 0]
do
ch=`expr $str | cut –c $len`
rstr=$rstr$ch
len=`expr $len – l`
done
echo “Reverse of a String” $rstr
11. Write a shell program to display all the files in the current directory.
Clear
echo “current directory is”
pwd
echo “file in current directory”
for file in *
do
echo $file
done
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 the basic salary”
read bs
echo “basic salary=$bs”
if [ $bs -lt 1500 ]
then
hra=`expr $bs \* 10 / 100`
echo “hra=$hra”
da=`expr $bs \* 90 / 100`
echo “da=$da”
gs=`expr $bs + $hra + $da`
echo “gross salary = $gs”
SSCASCW, Tumkur 10
Department of BCA
else
hra=500
echo “hra=$hra”
da=`expr $bs \* 98 / 100`
echo “da=$da”
gs=`expr $bs + $hra + $da`
echo “gross salary =$gs”
fi
13. Write a shell script to compute GCD & LCM of two numbers. Use the basic function to find
GCD and LCM of N numbers.
echo "Enter the first number :"
read a
echo "Enter the second number : "
read b
if [ $a -gt $b ]
then
num=$a
den=$b
else
num=$b
den=$a
fi
r=`expr $num % $den`
while [ $r -ne 0 ]
do
num=$den
den=$r
r=`expr $num % $den`
done
gcd=$den
lcm=`expr $a \* $b / $gcd`
echo " The LCM of $a and $b is :
$lcm" echo " The GCD of $a and $b is :
$gcd"
SSCASCW, Tumkur 11