BSC CS Shell Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

S.

NO DATE PARTICULARS PAGE NO

1.
File Commands Implementation

2.
Printing System Configuration

3.
Pipes, Redirection and Tee Commands

4. Using Case statement

5.
Calendar Command

6.
Sum of Individual Digits

7. Greatest Number

8.
Palindrome Checking

9.
Printing Multiplication Table

10.
Compare Two Files
EX.NO: 1
FILE COMMANDS IMPLEMENTATION
DATE :

AIM:
To implement the following file commands:

- rm, cp, cat, mv, cmp, we, split, diff

PROCEDURE:

To delete an existing file (rm)


1. Input an existing file name.
2. Delete the file with ‘rm’ command.
3. Check the file is deleted.

To copy the file contents into another (cp)


4. Get an existing file name
5. Copy the file into a new file with ‘cp’ command
6. Check the contents are copied into new one.

To display the contents of a file (cat)


7. Get the file name to be displayed.
8. Display the file using ‘cat’ command.

To move the file into the sub-directory (mv)


9. Get the filename to be moved.
10. Move the file with specified sub-directory with ‘mv’ command.
11. Check the file is moved to the appropriate path.
To compare files with ‘cmp’ (Byte by byte comparison)
12. Read two files to be compared.
13. Compare two files with ‘cmp’ command.
14. ‘cmp’ will compare the files byte by byte.
15. If the contents are same print ‘ Both are same’
16. Print ‘ Files are different’ when the difference is found.

To count number of words in a file (wc)


17. Read the file name in which the number of words to be counted.
18. Use ‘wc’ to count and display the number of words.

To split the file content into ‘n’ lines (split)


19. Read the file name.
20. Using ‘split’ command and a number ‘n’ split the file content to n lines.
21. Display the contents

To find difference between two files with ‘diff’ (Line by line comparison)
22. Read two files.
23. Compare the contents of two files with ‘diff’. The differences between two files will be
displayed.
clear
echo -e "Enter the file name to delete: "
read DFILE
rm $DFILE

echo -e "Enter a text file to copy: "


read CFILE
cp $CFILE copied.txt

echo -e "Enter a text file to display: "


read DIFILE
cat $DIFILE

echo -e "Enter a text file to move to sub-directory:"


read MFILE
mv $MFILE "dhana/$MFILE"

echo -e "Enter two text files to compare:"


read FIRST
read SECOND
if [ -n "$(cmp $FIRST $SECOND)" ]
then
echo "Files are Different"
else
echo "Files are same"
fi
echo -e "Enter a text file to count words"
read WFILE
echo $(wc $WFILE)

echo -e "Enter a text file to split into 5 line: "


read SFILE
split -5 $SFILE

echo -e "Enter two files to find difference line by line: "


read FIRST
read SECOND
diff $FIRST $SECOND
OUTPUT :

Enter the file name to delete: file1.txt


Enter a text file to copy: file2.txt
Enter a text file to display: file3.txt
J.K.K.Nataraja College of Arts & Science
III B.Sc Computer Science
Enter a text file to move to sub-directory: sub.txt
Enter two text files to compare: file3.txt
file4.txt
Files are Different
Enter a text file to count words file3.txt
3 10 68 file3.txt
Enter a text file to split into 5 line: file4.txt
Enter two files to find difference line by line: file4.txt
file5.txt
3d2
< three
7d5
< seven
10c8
< ten
---
> 100
[root@localhost student]#

RESULT:
The basic file commands were embedded and the implemented.
EX.NO: 2
PRINTING SYSTEM CONFIGURATION
DATE :

AIM:
To print the system configuration using shell programming.

PROCEDURE:

1. Display the user and username using ‘USER’ and ‘USERNAME’ commands
2. To display the current working shell, give echo “$SHELL”
3. To see the home directory, give echo “$HOME” in the $prompt.
4. To display the OS that is currently installed, give $OSTYPE
5. To see the path, give $PATH
6. For printing the current working directory, give $PWD
7. For displaying the number of users currently logged in, give echo $users
8. To display all the available shell, give $(cat /etc/shells)
9. To get CPU information, give the command $(cat /proc/cpuinfo)
10. To display memory information, give the command $(cat /proc/meminfo)
clear
echo -e "User name: $USER (Login name: $LOGNAME)"

echo -e "Current shell: $SHELL"

echo -e "Home directory: $HOME"

echo -e "Operating System type: $OSTYPE"

echo -e "Current Path settings: $PATH"

echo -e "Current working directory: $PWD"

echo -e "Number of users currently logged in: $(echo $users | wc -l)"

echo -e "All available shells: $(cat /etc/shells)"

echo -e "CPU information: $(cat /proc/cpuinfo)"

echo -e "Memory information: $(cat /proc/meminfo)"


OUTPUT :

[root@localhost student]# sh two.sh

User name: root (Login name: root)


Current shell: /bin/bash

Home directory: /root


Operating System type: linux-gnu

Current Path settings:


/usr/share/Modules/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/root/bin

Current working directory: /root/student


Number of users currently logged in: 1

All available shells: /bin/sh


/bin/bash
/sbin/nologin
/usr/bin/sh
/usr/bin/bash
/usr/sbin/nologin

CPU information: processor : 0


vendor_id : GenuineIntel
cpu family 6
model 42
model name : Intel(R) Core(TM) i3-2370M CPU @ 2.40GHz

Memory information:
MemTotal: 6044640 kB
MemFree: 3108052 kB
MemAvailable: 4043060 kB
Buffers: 84856 kB
Cached: 1254832 kB
SwapCached: 0 kB

[root@localhost student]#

RESULT:

Thus the detailed system configuration was displayed.


EX.NO: 3
PIPES, REDIRECTION AND TEE COMMANDS
DATE :

AIM:
To implement pipe, redirection and tee command using shell programming.

PROCEDURE:

1. List the three choices for 1. pipe, 2. redirect & 3. tee operations.
2. Selection any one option.
3. If the option is 1, it is pipe operation.
a. List number of files in the directory and count the number of lines
4. If the option is 2, it is redirect operation
a. The result of list operation is redirected in to the new text file.
5. If the option is 3, tee operation.
a. With tee, command print the result in the screen and redirect it into the new file.
6. If the option is not from 1 to 3, display the message “Try again”
clear
echo "1.Pipe command"
echo "2.Redirect command"
echo "3.Tee command"
echo -e "Enter your choice: "
read CHOICE

case $CHOICE in
1) echo "Using pipe command in ls and wc"
echo "Total number of Files :"
ls | wc -l;;
2) echo "Using Redirect command to store the result in output.txt file"
ls -l >> output.txt ;;
3) echo "Using tee command to display the result in output screen as well as in
toutput.txt file"
ls -l | tee toutput.txt;;
*) echo "Wrong selection - try again";;
esac
OUTPUT:
[root@localhost student]# sh three.sh
1.Pipe command
2.Redirect command
3.Tee command
Enter your choice: 1
Using pipe command in ls and wc
Total number of Files : 10

[root@localhost student]# sh three.sh


1.Pipe command
2.Redirect command
3.Tee command
Enter your choice: 2
Using Redirect command to store the result in output.txt file

[root@localhost student]# sh three.sh


1.Pipe command
2.Redirect command
3.Tee command
Enter your choice: 3
Using tee command to display the result in output screen as well as in toutput.txt file
total 32
-rw-r--r--. 1 root root 68 Oct 9 07:14 file2.txt
-rw-r--r--. 1 root root 68 Oct 9 07:14 file3.txt
-rw-r--r--. 1 root root 49 Oct 9 07:14 file4.txt
-rw-r--r--. 1 root root 37 Oct 9 07:14 file5.txt
drwxr-xr-x. 3 root root 4096 Oct 9 07:09 one
-rw-r--r--. 1 root root 413 Oct 9 07:14 output.txt
-rw-r--r--. 1 root root 511 Oct 9 07:13 three.sh
drwxr-xr-x. 2 root root 4096 Oct 9 07:12 two

[root@localhost student]#

RESULT:
Thus the pipe, redirect and tee commands were implemented using shell program.
EX.NO: 4
USING CASE STATEMENT
DATE :

AIM:
To write a shell program for displaying current date, username, file listing and directories by
getting user choice.

PROCEDURE:

1. Select the option


2. If the option is 1, display the current date in the format of DD-Month-YYYY
3. If the option is 2, print the user name
4. If the option is 3, list the files
5. If the option is 4, list all the directories in the current directory
6. For other options, prompt as wrong choice.
clear
echo "1.Display current date"
echo "2.Display User name"
echo "3.Display file list"
echo "4.Display directory list"
echo -e "Enter your choice:"
read CHOICE

case $CHOICE in
1) echo "$(date "+%d-%B-%Y")";;
2) echo "$(whoami)";;
3) echo "$(ls | egrep -v '^d')";;
4) echo "$(ls -d */)";;
*) echo "Wrong choice - try again";;
esac
OUTPUT:

[root@localhost student]# sh four.sh


1.Display current date
2.Display User name
3.Display file list
4.Display directory list
Enter your choice: 1
09-October-2019

[root@localhost student]# sh four.sh


1.Display current date
2.Display User name
3.Display file list
4.Display directory list
Enter your choice: 2
root

[root@localhost student]# sh four.sh


1.Display current date
2.Display User name
3.Display file list
4.Display directory list
Enter your choice: 3
four.sh
res.txt

[root@localhost student]# sh four.sh


1.Display current date
2.Display User name
3.Display file list
4.Display directory list
Enter your choice: 4
one/
three/
two/

[root@localhost student]#

RESULT:
Thus the displaying current date, username, file listing and directories listing were done using
statement.
EX.NO: 5
CALENDAR COMMAND
DATE :

AIM:
To write a shell program to modify "cal" command to display calendars of the specified range of
months.

PROCEDURE:

1. Read the year.


2. Read the starting month and ending month to display the months in the specified year.
3. Using the loop, display all the dates of months in the specified year.
clear
cal
echo "Enter year to display calendar: "
read YEAR
echo "Enter starting month: "
read SMONTH
echo "Enter ending month: "
read EMONTH
for ((I=SMONTH;I<=EMONTH;I++))
do
cal $I $YEAR
done
OUTPUT:

[root@localhost student]# sh calendar.sh


October 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 29 30 31

Enter year to display calendar: 2018


Enter starting month: 05
Enter ending month: 07
May 2018
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 31

June 2018
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

July 2018
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 31

[root@localhost student]#

RESULT:
Thus the months with dates were displayed using ‘cal’ command.
EX.NO: 6
SUM OF INDIVIDUAL DIGITS
DATE :

AIM:
To write a shell program to find the sum of individual digits of a given number.

PROCEDURE:

1. Get a number, NUM. It must be greater than 0.


2. Initialize sum as 0.
3. Separate each digit from one’s place by doing modulo division of that number.
4. Add with SUM.
5. Divide the number by 10 to discard the number in the one’s place and assign it in the
number NUM.
6. If NUM is greater than 0, repeat the steps 3-5.
7. Print SUM
echo -e "Enter a number: "
read NUM
SUM=0
TEMP=$NUM
while(($NUM >0))
do
DIGIT=`expr $NUM % 10`
SUM=`expr $SUM + $DIGIT`
NUM=`expr $NUM / 10`
done
echo "The sum of $TEMP = $SUM"
OUTPUT:

root@localhost student]# sh sum.sh

Enter a number: 4567

The sum of 4567 = 22


[root@localhost student]#

RESULT:

Thus the sum of digits were found and displayed.


EX.NO: 7
GREATEST NUMBER
DATE :

AIM:
To write a shell program to find the greatest among the given set of numbers using
command line arguments.

PROCEDURE:

1. Find the total number of arguments passed.


2. Compare each number with its successive numbers.
3. Assign the larger in NUM.
4. Repeat the steps 2 & 3 until all the numbers are compared.
5. Print NUM as the largest.
echo "Total Number of Arguments Passed: $#"
NUM=0
for ARGS in "$@"
do
if [ $NUM -lt $ARGS ]
then NUM=$ARGS
fi

done
echo "Largest number is : $NUM"
OUTPUT:

[root@localhost student]# sh greatest.sh 11 54 7 75 43


Total Number of Arguments Passed: 5
Largest number is : 75
[root@localhost student]#

RESULT:

Thus the largest of ‘n’ numberswas found using command line arguments
EX.NO: 8
PALINDROME CHECKING
DATE :

AIM:
To write a shell program for palindrome checking.

PROCEDURE:

1. Get a number, NUM. It must be greater than 0.


2. Assign the initial value of NUM in NUMBER.
3. Initialize REV as 0.
4. Separate each digit from one’s place by doing modulo division of that number and assign in
DIGIT.
5. Divide the number by 10 to discard the number in the one’s place and assign it in the
number NUM.
6. Multiply REV with 10 and add it to DIGIT. Assign the result in REV.
7. If NUM is greater than 0, repeat the steps 3-5.
8. Compare the initial value of NUM (NUMBER) and REV.
9. If both are same, print the given number is a Palindrome.
10. If both are not same, print the given number is not a Palindrome.
clear
echo -e "Enter the number: "
read NUM
NUMBER=$NUM
REV=0
while [ $NUM -gt 0 ]
do
DIGIT=`expr $NUM % 10 `
NUM=`expr $NUM / 10 `
REV=`expr $REV \* 10 + $DIGIT`
done
echo $REV
if [ $NUMBER -eq $REV ]
then
echo "Number is a palindrome"
else
echo "Number is not a palindrome"
fi
OUTPUT:

[root@localhost student]# sh palindrome.sh


Enter the number: 1234
4321
Number is not a palindrome

[root@localhost student]# sh palindrome.sh


Enter the number: 12321
12321
Number is a palindrome
[root@localhost student]#

RESULT:

Thus the given number is checked whether it is palindrome or not.


EX.NO: 9
PRINTING MULTIPLICATION TABLE
DATE :

AIM:
To write a shell program for printing multiplication table of the given arguments using for
loop.

PROCEDURE:

Step 1: Get the number of the table and the terms in that table to be printed.

Step 2: Start a loop index from 1 and increase to the number of terms.

Step 3: Multiply the table number with the index and print the result.

Step 4: Increase index by 1.

Step 5: Repeat the steps 3 – 4, until the number of terms has been reached.
clear
echo "Total Number of Argument Passed: $#"
if [ $# -ne 2 ]
then
echo "Enter Multiplication table number and terms to be printed"
break
fi

for ((I=1;I<=$2;I++))
do
TEMP=`expr $I \* $1`
echo "$I X $1 = $TEMP"
done
OUTPUT:

[root@localhost student]# sh mul.sh 7 10

Total Number of Argument Passed: 2


1X7=7
2 X 7 = 14
3 X 7 = 21
4 X 7 = 28
5 X 7 = 35
6 X 7 = 42
7 X 7 = 49
8 X 7 = 56
9 X 7 = 63
10 X 7 = 70
[root@localhost student]#

RESULT:

Thus the multiplication table was printed using command line arguments
EX.NO: 10
COMPARE TWO FILES
DATE :

AIM:
To write a shell program to compare two files and if found equal deleting the duplicate file.

PROCEDURE:

Step 1: Prompt two file names, file1 and file2.

Step 2: Compare the contents of two files.

Step 3: If the contents are different, display both files are different.

Step 4: If the contents are same, remove the file2 by considering it as duplicate.
echo "Enter first file name "
read FILE1
echo "Enter second file name "
read FILE2

if [ $FILE1 == $FILE2 ]
then
echo "Two file are in same path - enter different path"
exit
fi

cmp $FILE1 $FILE2 > error

if [ -s error ]
then
echo "Files are different"

else
echo "Files are same"
rm -i $FILE2

fi
OUTPUT

[root@localhost student]# sh filediff.sh


Enter first file name : file2.txt
Enter second file name : file2.txt

Two file are in same path - enter different path

[root@localhost student]# sh filediff.sh


Enter first file name : file2.txt
Enter second file name : sub/file3.txt

Files are different

[root@localhost student]# sh filediff.sh


Enter first file name : file3.txt
Enter second file name : sub/file3.txt

Files are same


rm: remove regular file 'sub/file3.txt'? y

[root@localhost student]#

RESULT:
Thus the contents of the files were compared and the duplicate files were removed.

You might also like