BSC CS Shell Programming
BSC CS Shell Programming
BSC CS Shell Programming
1.
File Commands Implementation
2.
Printing System Configuration
3.
Pipes, Redirection and Tee Commands
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:
PROCEDURE:
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
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)"
Memory information:
MemTotal: 6044640 kB
MemFree: 3108052 kB
MemAvailable: 4043060 kB
Buffers: 84856 kB
Cached: 1254832 kB
SwapCached: 0 kB
[root@localhost student]#
RESULT:
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]#
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:
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]#
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:
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:
RESULT:
AIM:
To write a shell program to find the greatest among the given set of numbers using
command line arguments.
PROCEDURE:
done
echo "Largest number is : $NUM"
OUTPUT:
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:
RESULT:
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 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:
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 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
if [ -s error ]
then
echo "Files are different"
else
echo "Files are same"
rm -i $FILE2
fi
OUTPUT
[root@localhost student]#
RESULT:
Thus the contents of the files were compared and the duplicate files were removed.