INDEX
S.NO PRACTICALS SIGN
1. Shell Commands
2. Shell Commands For Processes
3. File Commands
4. Script that displays a simple Welcome Message
5. Shell script to display calendar, date and process status.
6. Shell script to print the multiplication table of any number.
7. Shell script to enter the age and declare it on the screen.
8. Shell script to find factorial of a given number.
9. Shell script to count the no. of words and chars in a file.
10. Shell script to print Fibonacci series.
11. Shell script to sort a given list of integers
12. Shell script to find whether the entered number is even or
odd.
13. Shell script to calculate simple interest.
14. Shell script to generate odd numbers between 1 and 50.
15. Shell script to accept a number from user, n and display
square of all numbers from 1 to n.
16. Shell script to generate the series 1,3,2,4,3,5,4,6……100.
17. Shell script to check if a given string is a palindrome or not
(without reversing the string).
18. Shell script to reverse a string.
1. SHELL COMMANDS
1) Exit
This command is used to exit from the shell.
2) Log out
This command is used to log out from the shell.
3) echo
This command is used to print text on the screen.
4) echo $PATH
This command gives the path of the shell.
5) Cal
This command shows the calendar of present month.
6) Cal 2009
This command shows the calendar of 2009.
7) Cal 2009 | move
This command shows the calendar of year 2009 pagewise and
we have to press enter to see next page. “ | ” is known as the pipe. It
transfer the output of Cal command to the move command.
8) Date
This command shows the current date.
a) Date +%m
This gives the number of current month.
b) Date +%n
This gives the name of the current month.
9) who
This command shows how many users are log in.
10) tty
This command shows the current terminal on which we are working
11) wall
This command broadcasts the message on all the terminals.
12) write system_number
This command send the message to a particular system specified by
system number.
2. Shell Commands For Processes
1) ps
This command list all the processes that are currently running giving
details such as process id, terminal, time and command.
2) pstree
This command lists all the processes in a tree structure.
3) pstree -p
This command in addition to displaying the processes in a tree structure
also displays process ID.
4) pgrep
This command gives ID of the mentioned process.
For eg pgrep date:- This will show the process ID of the process. If
we use option –l then in addition to PID it also shows process name.
5) top
This command displays all the top processes. It give more details than ps
command. It refreshes all the process after every five seconds.
6) kill PID
It simply kills a process by mentioning its ID.
7) kill % job_number
This command kills the job specified by the job_number.
8) kill all
This command uses the name of the process to kill the process instead of
PID.
9) pkill
This command kills a process by a small match.
For eg. Pkill -u user_1 user_2 process_name
This command kills the process specified by process_name of user_1 and
user_2.
10) skill
This command is used to send a particular signal.
skill _ STOP user_name :- This command stops all user processes
until we send continue signal.
11) ctrl c
This command interrupts the execution. Also known as break command.
12) jobs
It lists all the jobs running on the background and foreground in groups.
13) nice
This command is used by the administrator to set the priority of the
processes.
14) renice
It changes the priority of the processes.
15) &
This operator will not allow user to log out while the jobs are running. This
is shell operator used to run a process in background.
3.File Commands
1) Print Working Directory (pwd)
Checking the current working directory
2) Change Directory(cd)
Changing the current directory
3) Make Direcotry (mkdir)
Is use to create directory & subdirectories under the directory
4) Remove Directory (rmdir)
Removing drectory
5) ls
Display all files
6) cp
Copying a file
7) rm
Deleting file
8) mv
Moving & renaming files
9) more
Display data one screen full at a time
10) less
Similar to more but we can move upward also
11) head
Shows first ten lines of a file
12) tail
Shows last ten lines of a file
13) uptime
Display how long the system has been running how many users are
currently logged on, system load average for the percentage cpu
utilization, in last one 5, 15 minutes.
14) file
Displays the file type
15) Word Count (wc)
Counting the words , lines, characters.
16) Octal Display(od)
Displays the octal representation of the file content
17) Compare (cmp)
Comparing the two sorted files.
18) Common(comm.)
This command displays the common content of the file.
19) gzip and gunzip
Compressing and Decompressing the file
20) tar
The archival program with the following options:
a) tar-x : Extract files from archive
b) tar-t : Display files in archive
c) tar-c : Create the archive
21) zip and unzip
Creates and archive as well as compressing the file
22) Change Mode (chmod)
This command change the permission on the file
23) mount
Attachment of the file or device
24) umount
Deatchments of a file or device
4. Write a shell script that simply displays a Welcome message.
echo "WELCOME TO THE SHELL SCRIPTS"
OUTPUT:
$ ./test
WELCOME TO THE SHELL SCRIPTS
5. Write a shell script to display calendar, date and process status.
echo”User Calender”
cal mm yyyy
echo “DATE”
date
echo ”Process Ststus”
ps
OUTPUT
$ sh linux1.sh
User Calender
November 2009
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 30
DATE
Tue Nov 17 22:08:48 PST 2009
Process Status
PID TTY TIME CMD
3239 pts/0 00:00:00 bash
3585 pts/0 00:00:00 sh
3622 pts/0 00:00:00 ps
6. Write a shell script to print the multiplication table of any number.
echo “Enter the number: “
read num //cmd to read user input
count=1 //user defined variable
echo “The Table is : “
while[$count-le 10] //checking the value of count continues till 10
do
ans= ’expr $count \* $num’ //multiplying the required numbers
echo $num “*” $count “=” $ans
count ‘expr $count + 1’
done
OUTPUT:
$ ./prg1
Enter a number : 19
The Table is :
19 * 1 = 19
19 * 2 = 38
19 * 3 = 57
19 * 4 = 76
19 * 5 = 95
19 * 6 = 114
19 * 7 = 133
19 * 8 = 152
19 * 9 = 171
19 * 10 = 190
7. Write a shell script to enter the age and declare it on the screen.
echo “Enter your age: “;
read age
if [$age –le12]
then echo “You are a Child”
elif [$age –ge13 –a $age –le 19]
then echo “You are a Teenager”
elif [$age –ge 20 –a $age –le 30]
then echo “You are young”
elif [$age –ge 31 –a $age –le 60]
then echo “You are an Adult”
elif [$age –gt 60]
then echo “You are an Old Person”
fi //to end if
OUTPUT :
$ ./ prg2
Enter your age:
21
You are young
8. Write a shell script to find factorial of a given number.
echo “ Enter the number”
read num
count =1
ans=1
while[$count –le $num]
do
echo $count
ans= ‘expr $ans \* $count’
count=’expr $count +1’
done
echo “The factorial of ” $num “is: “ $ans
OUTPUT:
$ ./ prg3
Enter the number
6
The factorial of 6 is 720
9. Write a shell script to count he no. of words and chars in a file.
echo “Enter the data”
cat>file1
echo “No. of words: “
wc –w file1
echo “No. of characters”
wc-c file1
echo
OUTPUT:
$ ./ prg4 file1
Number of Words= 20
Number of Characters= 79
10. Write a shell script to print Fibonacci series.
echo “Enter the no. of terms required in the series”
read num
i=0; j=1;k=2
if [$num –le 1] ; then
echo “No. of terns should be atleast 2”
else
echo $i
echo $j
while [$k –lt $num]
do
k=`expr $k + 1`
r=`expr $i + 1`
echo $r
i=$j
j=$r
done
fi
OUTPUT:
$ ./ prg5
Enter the no. of terms required in the series 7
The series is :
0
1
1
2
3
5
8
11. Write a shell script to sort a given list of integers
echo “Enter the list of integers
cat>file1 // list of integers will be stored in a file
echo :Sorted List is :-“
sort –n file1 // cmd to sort the contents of the file given
OUTPUT:
$ ./ prg6
Sorted list is:-
6 10 13 14 17 19 14 19 25 28
12. Write a shell script to find whether the entered number is even or odd.
echo “Enter a number”
read n
if [ $n % 2 –eq 0]
then echo “Its an Even number’
else
echo “Its an Odd number”
fi
OUTPUT:
$ ./ prg7
Enter a number
43
Is an Odd Number
13. Write a shell script to calculate simple interest.
echo “Enter Principal”
read p
echo “Enter rate of interest “
read r
echo “Enter Time period(in years)”
read t
s= ‘expr $p \* $r \* $t’
s= ‘expr $s /100’
echo “Simple Interest is : “ $s
OUTPUT:
$ ./ prg8
Enter Principal
10000
Enter rate of interest
5
Enter Time period(in years)
3
Interest is: 1500
14. Write a shell script to generate odd numbers between 1 and 50.
i=1
while [ $i –le 50]
do
echo –n $i
i=’expr $i + 2’
done
OUTPUT:
$ ./ prg9
Odd numbers between 1 and 50:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
15. Write a shell script to accept a number from user, n and display square of all
numbers from 1 to n.
echo “Enter number: \c”
read n
i=1
while [ $i –le $n]
do
m=`expr $i \* Si`
echo “Square of “ $i “ is:-“ $m
i= `expr $i + 1`
done
OUTPUT
$ sh num.sh
Enter number: 9
Square of 9 is: 81
16. Write a shell script to generate the series 1,3,2,4,3,5,4,6……100.
i=1
j=3
while [$j –le 100]
do
echo $i
i=`expr $i + 1`
echo $j
j=`expr $j + 1`
done
OUTPUT:
$ ./ prg11
The Series is :
1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 10 12 11 13 12 14 13 15 14 16 15 17 16
18 17 19 18 20 19 21 20 22 21 23 22 24 23 25 24 26 25 27 26 28 27 29 28 30
29 31 30 32 31 33 32 34 33 35 34 36 35 37 36 38 37 39 38 40 39 41 40 42 41
43 42 44 43 45 44 46 45 47 46 48 47 49 48 50 49 51 50 52 51 53 52 54 53 55
54 56 55 57 56 58 57 59 58 60 59 61 60 62 61 63 62 64 63 65 64 66 65 67 66
68 67 69 68 70 69 71 70 72 71 73 72 74 73 75 74 76 75 77 76 78 77 79 78 80
79 81 80 82 81 83 82 84 83 85 84 86 85 87 86 88 87 89 88 90 89 91 90 92 91
93 92 94 93 95 94 96 95 97 96 98 97 99 98 100
17. Write a shell script to check if a given string is a palindrome or not (without
reversing the string).
echo "Enter the String :"
read str
len=`expr $str | wc -c`
len=`expr $len - 1`
mid=`expr $len / 2`
flag=1
m=1
while [ $m -le $mid ]
do
c=`echo $str | cut -c$len`
p=`echo $str | cut -c$m`
if [ $c != $p ]
then
flag=0
break
fi
m=`expr $m + 1`
len=`expr $len - 1`
done
if [ $flag -eq 1 ]
then
echo "The string is a Palindrome"
else
echo "The string is not a Palindrome"
fi
OUTPUT:
$ ./ prg12
Enter the String :
LEVEL
The string is a Palindrome
18. Write a shell script to reverse a string.
echo -n "Enter a string to reverse: "
read string
echo "$string"
len=`echo -n $string | wc -c`
echo "Number of character is: $len"
while [ $len -gt 0 ]
do
rev=$rev`echo -n $string | cut -c $len`
len=`expr $len - 1`
done
echo "The reverse string is:$rev"
OUTPUT:
$ ./ prg13
Enter a string to reverse: india
Number of character is: 5
The reverse string is : aidnI