You are on page 1of 23

Ex.

no:01

Date: SHELL COMMANDS

AIM:
To perform a shell script to implement the file commands of rm, cp, cat, mv, cmp, we, split,
differ.

ALGORITHM:

STEP1: Start the terminals.


STEP2: Make the directory and call the directory.
STEP3: And give the appropriate command for rm, cp, cat, mv, cmp, we, split, differ.
STEP4: Get the input files from user.
STEP5: And run the shell commands.
STEP6: Display the outputs.
STEP7: Close the terminals.

PROGRAM:

REMOVE COMMAND (rm)


[student@localhost gopi]$ ls

array1.sh~ dat.sh dec.sh~ file2.txt mat.sh nav2.txt new file rec sum.sh~ test4.sh
test5.sh~ xaa

[student@localhost gopi]$ rm xaa


[student@localhost gopi]$ ls

array1.sh~ dat.sh dec.sh~ file2.txt mat.sh nav2.txt new file rec sum.sh~ test4.sh
test5.sh~ xab xae xah
[student@localhost gopi]$

COPY COMMAND (cp)


[student@localhost ~]$ cd gopi1
[student@localhost gopi1]$ ls
gd.txt gd.txt~ nav1.txt test2 test3

[student@localhost gopi1]$ cat gd.txt


good morning
have a nice day
happy morning
have a great day

[student@localhost gopi1]$ cat test2

[student@localhost gopi1]$ cp gd.txt test2


[student@localhost gopi1]$ cat test2
good morning
have a nice day
happy morning
have a great day
[student@localhost gopi1]$

CAT COMMAND
[student@localhost gopi1]$ cat>makenew.txt
sona college of arts and science
^Z
[4]+ Stopped cat > makenew.txt

[student@localhost gopi1]$ cat makenew.txt


sona college of arts and science
[student@localhost gopi1]$
COMPARE COMMAND (cmp)
[student@localhost gopi1]$ cat gd.txt
good morning
have a nice day
happy morning
have a great day

[student@localhost gopi1]$ cat test3


im gopinath
studying bsc cs
in sonacas
abcd
efgh
ijkl
mnop
qrst
uvw
xvy
[student@localhost gopi1]$

[student@localhost gopi1]$ cmp gd.txt test3


gd.txt test3 differ: byte 1, line 1
[student@localhost gopi1]$

DIFFERENT COMMAND (diff)


[student@localhost gopi]$ cat>nav1.txt
sona
college
of
arts
and
science
^Z
[1]+ Stopped cat > nav1.txt
[student@localhost gopi]$ cat>nav2.txt
sona
college
of
engineering
^Z
[2]+ Stopped cat > nav2.txt
[student@localhost gopi]$

[student@localhost gopi]$ diff nav1.txt nav2.txt


4,6c4
< arts
< and
< science
---
> engineering
[student@localhost gopi]$

RESULT:
Thus the shell script commands has been successfully executed and output has been displayed.
EX:02
SYSTEM CONFIGURATION
DATE:

AIM:
To perform a shell script to print the following system configuration: a) currently logged user
and his log name b) current shell , home directory , Operating System type , current path setting ,
current working directory c) show currently logged number of users, show all available shells d)
show CPU information like processor type , speed e) show memory information

ALGORITHM:
STEP 1: Start the terminal
STEP 2: Make directory
STEP 3: Call the directory
STEP 4: Create the file with .sh extension
STEP 5: Type the program in the shell file that has been created
STEP 6 : Run the file and print the output
STEP 7 : Stop the process`

PROGRAM:
echo "enter the string"
echo "a. logged user"
echo "b.cpu information"
echo "c.block device information"
echo "d. show memory information"
echo "e. sysyem name"
echo "f. current shell"
echo "g. current date and time"
read str
case "$str" in
"a") echo "username"
who
who am i ;;
"b") echo "cpu information"
lscpu;;
"c") echo "block device information"
lsblk;;
"d") echo "show memoru information"
free -m;;
"e") echo "system name"
uname;;
"f") echo "current shell"
ostype;;
"g") echo "current date and time"
date;;

OUTPUT:
esac

RESULT:
The program was successfully executed.
EX:03
PIPES, REDIRECTION AND TEE COMMANDS
DATE:

AIM:
To perform a shell script to implement the following: pipes, redirection and tee commands.

ALGORITHM:
STEP 1: Start the terminal
STEP 2: Make directory
STEP 3: Call the directory
STEP 4: Create the file with .sh extension
STEP 5: Type the program in the shell file that has been created
STEP 6 : Run the file and print the output
STEP 7 : Stop the process`

echo "a. redirection"


echo "b.pipe"
echo "c.tee"
read str
case "$str" in
"a") cat file1.txt
echo "hello how are you" > file1.txt
echo "find pa" >> file1.txt;;
"b")
cat file1.txt|file3.txt
;;
"c") cat g.sh|tee file2.txt ;;
esac
RESULT:
The program was successfully executed.
Ex.no:04

Date:
DISPLAYING CURRENT DATE, USER NAME, FILE LISTING, DIRECTORIES.

AIM:
To perform a shell script to displaying current date, user name, file listing, directories.

ALGORITHM:
STEP1: Start the terminals.
STEP2: Make the directory for ex2 mkdir ex2.

STEP3: Call the directory ex2 for ex: cd ex2.


STEP4: Create the file with .sh extension.

STEP5: Use the switch case to display the requirements of shell.


STEP6: Get the input from the user using read command.

STEP7: And mention the command in each appropriate case.

STEP8: Close the case by esac.

PROGRAM:
echo "enter the string"
echo "a. today date"
echo "b. username"
echo "c. calender"
echo "d. list"
read str
case "$str" in
"a") echo "today date"
date;;
"b") echo "username"
who;;
"c") echo "calender"
cal;;
"d") echo "list of files"
ls;;
esac
OUTPUT:

RESULT:
Thus the shell script program has been successfully executed and output has been
displayed.
EX:05
CALENDER
DATE:

AIM:
To perform a shell script to modify "cal" command to display calendars of the specified range
of months.
ALGORITHM:
STEP 1: Start the terminal
STEP 2: Make directory
STEP 3: Call the directory
STEP 4: Create the file with .sh extension
STEP 5: Type the program in the shell file that has been created
STEP 6 : Run the file and print the output
STEP 7 : Stop the process`

PPROGRAM:
echo "enter the string"
echo "a. calender"

read str
case "$str" in
"a") echo "calender"
cal;;
esac
OUTPUT:

RESULT:
The program was successfully executed.
EX:06
SUM OF INDIVIDUAL DIGITS
DATE:

AIM:
To find the sum of the individual digits of a given number.

ALGORITHM:
STEP 1: Start the terminal
STEP 2: Make directory
STEP 3: Call the directory
STEP 4: Create the file with .sh extension
STEP 5: Type the program in the shell file that has been created
STEP 6 : Run the file and print the output
STEP 7 : Stop the process`

PROGRAM:
echo "enter a number "
read n
sd=0
sum=0
while [ $n -gt 0 ]
do
sd=$(($n%10))
n=$(($n/10))
sum=$(($sum+$sd))
done
echo " sum is $sum "
OUTPUT:

RESULT:
The program was successfully executed.
EX:07
GREATEST NUMBER
DATE:

AIM:
To find the greatest among the given set of numbers using line arguments

ALGORITHM:
STEP 1: Start the terminal
STEP 2: Make directory
STEP 3: Call the directory
STEP 4: Create the file with .sh extension
STEP 5: Type the program in the shell file that has been created
STEP 6 : Run the file and print the output
STEP 7 : Stop the process

PROGRAM:

echo " enter the a"


read a
echo "enter b"
read b
if [ $a -gt $b ]
then
echo "a is greater"
else
echo "b is greater"
fi
OUTPUT:

RESULT:
The program was successfully executed.
EX:08
PALINDROME
DATE:

AIM:
To check whether a given number and character is a palindrome or not.

ALGORITHM:
STEP 1: Start the terminal
STEP 2: Make directory
STEP 3: Call the directory
STEP 4: Create the file with .sh extension
STEP 5: Type the program in the shell file that has been created
STEP 6 : Run the file and print the output
STEP 7 : Stop the process

PROGRAM:

A) NUMBER
echo " enter the three digit number "
read num
s=0
rev=""
temp=$num
while [ $num -gt 0 ]
do
s=$(($num%10))
num=$(($num /10))
rev=$(echo ${rev}${s})
done
if [ $temp -eq $rev ]
then
echo "number is palindrome "
else
echo "number is not a palindrome "
fi
B) STRING
echo " enter a sring "
read s
echo $s>temp
rvs="$(rev temp)"
if [ $s == $rvs ]
then
echo " it is a palindrome"
else
echo " it is not a palindrome"

fi
OUTPUT

RESULT:
The program was successfully executed
EX:09
MULTIPLICATION
DATE:

AIM:
To print the multiplication table of argument in loop.

ALGORITHM:
STEP 1: Start the terminal
STEP 2: Make directory
STEP 3: Call the directory
STEP 4: Create the file with .sh extension
STEP 5: Type the program in the shell file that has been created
STEP 6 : Run the file and print the output
STEP 7 : Stop the process

PROGRAM:

echo " enter a number "


read n
echo " enter a range "
read r
i=0
while [ $i -le $r ]
do
echo "$n*$i=` expr $n\*$i`"
i=`expr $i + 1`
done
OUTPUT

RESULT:
The program was successfully executed.
EX:10
FILE COMPARISION
DATE:

AIM:
To perform a shell script to compare two files and if found equal delete the duplicate file.

ALGORITHM:
STEP 1: Start the terminal
STEP 2: Make directory
STEP 3: Call the directory
STEP 4: Create the file with .sh extension
STEP 5: Type the program in the shell file that has been created
STEP 6 : Run the file and print the output
STEP 7 : Stop the process

PROGRAM:

echo " enter file :"


read file1
echo " enter file :"
read file2
cmp $file1 $file2 > newfile
x=' wc newfile | cut -d" " -f2 '
if [ ' $x -eq 0 ' ]
then
rm -i $file2
fi
output
enter file:
file1
enter file2:
file2
cmp:eof on file1

RESULT:
The program was successfully executed.

You might also like