0% found this document useful (0 votes)
49 views23 pages

Working With Basic Command

The document contains summaries of 13 Linux script examples that perform various tasks using basic Linux commands like date, cal, ls, who, tty, cmp, tr, cat, grep, wc, expr and conditionals. The scripts work with files and directories, perform operations like copying, renaming, comparing, appending files, calculating average, factorial, swapping values and finding the biggest of three numbers. Each example provides the script code, output and a result statement indicating the program was successfully executed.

Uploaded by

Rajes Wari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views23 pages

Working With Basic Command

The document contains summaries of 13 Linux script examples that perform various tasks using basic Linux commands like date, cal, ls, who, tty, cmp, tr, cat, grep, wc, expr and conditionals. The scripts work with files and directories, perform operations like copying, renaming, comparing, appending files, calculating average, factorial, swapping values and finding the biggest of three numbers. Each example provides the script code, output and a result statement indicating the program was successfully executed.

Uploaded by

Rajes Wari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Ex.

no:1
Date: 05/07/18
Working with Basic Command
*******************************************************************************
*

AIM:
To write a linux script to work with Basic Command

CODING:

clear
echo "working with basic command"
echo "**************************"
echo "calendar"
echo "********"
cal
echo "date"
echo "****"
date
echo "user name"
echo "*********"
who
echo "terminal type"
echo "*************"
tty

OUTPUT:

system1@system1-Veriton-Series:~$ cd chitra
system1@system1-Veriton-Series:~/chitra$ sh 1.sh
working with basic command
**************************
calendar
********
October 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
date
****
Mon Oct 1 16:10:15 IST 2018
user name
*********
system1 :0 2018-10-01 16:02 (:0)
system1 pts/0 2018-10-01 16:09 (:0)
terminal type
*************
/dev/pts/0
RESULT:

1
Thus the program was succesfully executed.
Ex.no:2
Date: 05/07/18
Working with LS Command
*******************************************************************************
*

AIM:
To write a linux script to work with LS Command
CODING:
clear
echo "working with ls command"
echo "***********************"
echo "list of file"
echo "************"
ls
echo "list of file with more information"
echo "**********************************"
ls -l
echo "list of file with time orter"
echo "****************************"
ls -t
echo "list of file with used order"
echo "****************************"
ls -u
echo "list of file in reverse order"
echo "*****************************"
ls -r
OUTPUT:

list of file with time order


****************************
2.sh 21.sh~ 18.sh 15.sh 14.sh~ 11.sh 9.sh~ test 4.sh 1.sh
spirel.docx 20.sh 18.sh~ 15.sh~ 13.sh 11.sh~ 8.sh 6.sh 4.sh~
22.sh 20.sh~ 17.sh 16.sh 13.sh~ 10.sh 8.sh~ 6.sh~ 3.sh
22.sh~ 19.sh 17.sh~ 16.sh~ 12.sh 10.sh~ 7.sh 5.sh 3.sh~
21.sh 19.sh~ 30.sh 14.sh 12.sh~ 9.sh 7.sh~ 5.sh~ 2.sh~
list of file with used order
****************************
2.sh 21.sh~ 18.sh~ 16.sh 13.sh~ 10.sh~ 7.sh~ 4.sh
spirel.docx 20.sh 17.sh 16.sh~ 12.sh 9.sh test 4.sh~
1.sh 20.sh~ 17.sh~ 30.sh 12.sh~ 9.sh~ 5.sh 3.sh
22.sh 19.sh 2.sh~ 14.sh 11.sh 7.sh 6.sh 3.sh~
22.sh~ 19.sh~ 15.sh 14.sh~ 11.sh~ 8.sh 6.sh~
21.sh 18.sh 15.sh~ 13.sh 10.sh 8.sh~ 5.sh~
list of file in reverse order
*****************************
test 7.sh~ 4.sh~ 2.sh 20.sh 17.sh~ 14.sh~ 11.sh~
spirel.docx 7.sh 4.sh 22.sh~ 1.sh 17.sh 14.sh 11.sh
9.sh~ 6.sh~ 3.sh~ 22.sh 19.sh~ 16.sh~ 13.sh~ 10.sh~
9.sh 6.sh 3.sh 21.sh~ 19.sh 16.sh 13.sh 10.sh
8.sh~ 5.sh~ 30.sh 21.sh 18.sh~ 15.sh~ 12.sh~

2
8.sh 5.sh 2.sh~ 20.sh~ 18.sh 15.sh 12.sh
RESULT:

Thus the program was succesfully executed.


Ex.no:3
Date: 13/07/18
Counting Number of Files and Directories
*******************************************************************************
*

AIM:
To write a linux script to Count Number of Files and Directories

CODING:

clear
nf=`ls -l | grep -c "^-"`
nd=`ls -l | grep -c "^d"`
echo "number of files:$nf"
echo "number of directories:$nd"

OUTPUT:

number of files:46
number of directories:0

3
RESULT:

Thus the program was succesfully executed.

Ex.no:4
Date: 23/07/18
Total Number of Users
*******************************************************************************
*

AIM:
To write a linux script to display Total Number of Users
CODING:

clear
echo "total number of users"
echo "********************"
who | wc -l

OUTPUT:

total number of user


********************
2

4
RESULT:

Thus the program was succesfully executed.

Ex.no:5
Date: 30/08/18
TR Command
*******************************************************************************
*

AIM:
To write a linux script to convert lowercase to uppercase using TR command

CODING:

clear
echo "TR command"
echo "**********"
ls
echo "enter one file name"
read fn
if [ -f $fn ]
then
echo "file is exist"
tr "[a-z]" "[A-Z]" < $fn
else
echo "file is not exist"
fi

OUTPUT:

TR command
**********
10.sh 12.sh~ 15.sh 17.sh~ 1.sh 22.sh 3.sh 5.sh~ 8.sh test
10.sh~ 13.sh 15.sh~ 18.sh 20.sh 22.sh~ 3.sh~ 6.sh 8.sh~
11.sh 13.sh~ 16.sh 18.sh~ 20.sh~ 2.sh 4.sh 6.sh~ 9.sh
11.sh~ 14.sh 16.sh~ 19.sh 21.sh 2.sh~ 4.sh~ 7.sh 9.sh~
12.sh 14.sh~ 17.sh 19.sh~ 21.sh~ 30.sh 5.sh 7.sh~ spirel.docx
enter one file name
test
file is exist
HAI
HOW ARE YOU
I AM FINE

5
RESULT:

Thus the program was succesfully executed.

Ex.no:6
Date: 24/9/18
Copy & Rename the File
*******************************************************************************
*

AIM:
To write a linux script to Copy & Rename the File.

CODING:
clear
echo "copy & Rename the file"
echo "**********************"
echo "menu"
echo "****"
echo "1. copy"
echo "2.Rename"
read ch
case $ch in
1)echo "copy the file"
echo "*************"
echo "enter old the file name"
read f1
echo "enter the new file name"
read f2
cp $f1 $f2;;
2)echo "Rename the file"
echo "***************"
echo "enter the old file name"
read f1
echo "enter the new file name"
read f2
mv $f1 $f2;;
esac

OUTPUT:

copy & Rename the file


**********************
menu
****
1. copy

6
2.Rename
1
copy the file
*************
enter old the file name
test
enter the new file name
raji

RESULT:

Thus the program was succesfully executed.


Ex.no:7
Date: 16/10/18
Compare Two Files
*******************************************************************************
*

AIM:
To write a linux script to Compare two files

CODING:

clear
echo "compare using cmp commmand"
echo "enter the file name"
read f1 f2
echo "contents of" $f1
cat $f1
echo "contants of" $f2
cat $f2
echo "compare using cmp commands"
if ( cmp $f1 $f2 )
then
echo "two files are similar"
else
echo "two files are not similar"
fi

OUTPUT:
compare using cmp commmand
enter the file name
sa sa1
contents of sa
Hai
How are you?
contants of sa1
hello
I am fine
compare using cmp commands
sa sa1 differ: byte 1, line 1
two files are not similar

7
RESULT:

Thus the program was succesfully executed.

Ex.no:8
Date: 16/10/18
Append Two Files
*******************************************************************************
*

AIM:
To write a linux script to Append two files

CODING:
clear
echo "enter the first file name"
read a
echo "enter the second file name"
read b
if test -f $a
then
if test -f $b
then
echo "content of the first file"
cat $a
echo "content of the second file"
cat $b
cat >>$a $b
echo "appending after second file"
cat $a
else
echo "file do not exists"
fi
fi

OUTPUT:

enter the first file name


sa
enter the second file name
sa1
content of the first file

8
Hai
How are you?
content of the second file
hello
I am fine
appending after second file
Hai
How are you?
hello
I am fine

RESULT:

Thus the program was succesfully executed.

Ex.no:9
Date: 23/07/18 Print N Value In Reverse Order
*******************************************************************************
*

AIM:
To write a linux script to Print N Value In Reverse Order

CODING:

clear
a=5
while [ $a -gt 0 ]
do
echo $a
a=`expr $a - 1`
done

OUTPUT:

5
4
3
2
1

9
RESULT:

Thus the program was succesfully executed.

Ex.no:10
Date: 23/07/18
Average Calculation
*******************************************************************************
*

AIM:
To write a linux script to Average Calculation

CODING:

clear
echo "enter the five values"
read a b c d e
t=`expr $a + $b + $c + $d + $e `
avg=`expr $t / 5 `
echo $t
echo $avg

OUTPUT:

enter the five values


35786
29
5

10
RESULT:

Thus the program was succesfully executed.

Ex.no:11
Date: 30/07/18

Factorial Calculation
*******************************************************************************
*

AIM:
To write a linux script to Factorial Calculation

CODING:
clear
echo "enter the n value"
echo "*****************"
read n
f=1
while [ $n -gt 0 ]
do
f=`expr $f \* $n`
n=`expr $n - 1`
done
echo $f

OUTPUT:

Enter the n value


*****************
5
120

11
RESULT:

Thus the program was succesfully executed.

Ex.no:12
Date: 7/08/18
Swapping Two Values
*******************************************************************************
*

AIM:
To write a linux script to swap two values.

CODING:

clear
echo "SWAPPING"
echo "********"
echo "Enter the values:"
read a b
echo "Before swapping\n"
echo "$a \t $b"
t=$a
a=$b
b=$t
echo "After swapping\n"
echo "$a \t $b"

OUTPUT:

SWAPPING
********
Enter the values:
56 67

12
Before swapping

56 67
After swapping

67 56

RESULT:

Thus the program was succesfully executed.

Ex.no:13
Date: 7/08/18
Biggest Among Three Numbers
*******************************************************************************
*

AIM:
To write a linux script to Count Number of Files and Directories

CODING:
clear
echo "Biggest among three numers"
echo "**************************"
echo "enter the a,b,c values"
read a b c
if [ $a -gt $b ] && [ $a -gt $c ]
then
echo "a is biggest number"
else
if [ $b -gt $a ] && [ $b -gt $c ]
then
echo "b is biggest number"
else
echo "c is biggest number"
fi
fi

13
OUTPUT:
Biggest among three numers
**************************
enter the a,b,c values
245
c is biggest number

RESULT:

Thus the program was succesfully executed.

Ex.no:14
Date: 21/08/18
Checking Palindrome Number
*******************************************************************************
*

AIM:
To write a linux script to check given number is palindrome or not

CODING:
clear
echo "palindrome or not"
echo "*****************"
echo "enter one number"
read a
n=$a
s=0
while [ $a -gt 0 ]
do
r=`expr $a % 10`
s=`expr $s \* 10 + $r`
a=`expr $a / 10`
done
if [ $n -eq $s ]
then
echo "the $n is palindrome"

14
else
echo "the $n is not palindrome"
fi

OUTPUT:

palindrome or not
*****************
enter one number
121
the 121 is palindrome

RESULT:

Thus the program was succesfully executed.

Ex.no:15
Date: 5/09/18 Arithmetic Operation

*******************************************************************************
*

AIM:
To write a linux script to do Arithmetic Operation

CODING:

clear
echo "ARITHMETIC OPERATIONS"
echo "*********************"
echo "menu"
echo "****"
echo "(1)ADDITION"
echo "(2)SUBTRACTION"
echo "(3)MULTIPLICATION"
echo "(4)DIVISION"
echo "enter the two number"
read a b
echo "enter the choice"
read c
case $c in

15
(1) d=`expr $a + $b`;;
(2) d=`expr $a - $b`;;
(3) d=`expr $a \* $b`;;
(4) d=`expr $a / $b`;;
(*)exit;
esac
echo "result" $d

OUTPUT:

ARITHMETIC OPERATIONS
*********************
menu
****
(1)ADDITION
(2)SUBTRACTION
(3)MULTIPLICATION
(4)DIVISION
enter the two number
23 45
enter the choice
3
result 1035

RESULT:

Thus the program was succesfully executed.

Ex.no:16
Date: 5/09/18
Multiplication Table
*******************************************************************************
*

AIM:
To write a linux script to display multiplication table

CODING:
clear
echo "Enter the Table value"
read n
echo "Enter the ending value"
read e
i=1
while [ $e -ge $i ]
do
r=`expr $i \* $n`
echo "$i * $n =$r"
i=`expr $i + 1`
done

16
OUTPUT:
enter the Table value
7
enter the ending value
9
1 * 7 =7
2 * 7 =14
3 * 7 =21
4 * 7 =28
5 * 7 =35
6 * 7 =42
7 * 7 =49
8 * 7 =56
9 * 7 =63

RESULT:

Thus the program was succesfully executed.

Ex.no:17
Date: 24/09/18
Student Mark Details
*******************************************************************************
*

AIM:
To write a linux script to display student mark details

CODING:
clear
echo "student name"
read n
echo "roll no"
read r
echo "Tamil"
read t
echo "English"
read e
echo "Maths"
read m

17
echo "NAME:$n"
echo "ROLL NO:$r"
echo "Tamil:$t"
echo "ENGLISH:$e"
echo "MATHS:$m"
tot=`expr $t + "$e + $m `
echo "THE RESULT IS:$tot"
avg=`expr $tot / 3 `
echo "AVERAGE IS:$avg
OUTPUT:
student name
Raji
roll no
1201
Tamil
90
English
90
Maths
90
NAME:Raji
ROLL NO:1201
Tamil:90
ENGLISH:90
MATHS:90
THE RESULT IS:270
AVERAGE IS:90

RESULT:

Thus the program was succesfully executed.

Ex.no:18
Date: 24/09/18
Convert Decimal to Hexa
*******************************************************************************
*

AIM:
To write a linux script to Convert Decimal to Hexa

CODING:
clear
echo "Decimal to hexa number"
echo "**********************"
echo "enter the Decimal number"
read n
t=$n
s=0
f=1
while [ $n -gt 0 ]

18
do
r=`expr $n % 16`
n=`expr $n / 16`
s=`expr $s + $r \* $f`
f=`expr $f \* 10`
done
echo "decimal-->" $t "hexa---->" $s

OUTPUT:

Decimal to hexa number


**********************
enter the Decimal number
82
decimal--> 82 hexa----> 52

RESULT:

Thus the program was succesfully executed.

Ex.no:19
Date: 1/10/18
Draw Pattern
*******************************************************************************
*

AIM:
To write a linux script to Draw Pattern

CODING:
clear
echo "Draw pattern"
echo "************"
echo "enter the n value"
read n
i=1
j=1
while [ $i -le $n ]

19
do
j=1
while [ $j -le $i ]
do
echo -n $i
j=`expr $j + 1`
done
echo " "
i=`expr $i + 1`
done
OUTPUT:

Draw pattern
************
enter the n value
5
1
22
333
4444
55555

RESULT:

Thus the program was succesfully executed.

Ex.no:20
Date: 1/10/18
Sum Of Digit & Reverse Number
*******************************************************************************
*

AIM:
To write a linux script to dispaly sum of digit

CODING:
clear
echo "sum of digit and reverse number"
echo "******************************"
echo "enter the value"
echo "*****************"
read n

20
s=0
rev=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $s + $r`
rev=`expr $rev \* 10 + $r`
n=`expr $n / 10`
done
echo "sum of digit" $s
echo "reverse number" $rev

OUTPUT:

sum of digit and reverse number


******************************
enter the value
*****************
345
sum of digit 12
reverse number 543

RESULT:

Thus the program was succesfully executed.

Ex.no:21
Date: 1/10/18
Add Two Real Numbers
*******************************************************************************
*

AIM:
To write a linux script to add two real numbers

CODING:
clear
echo "Add to real number"
echo "******************"

21
echo "enter the two values"
read a b
if [ $a -gt 0 -a $b -gt 0 ]
then
c=`expr $a + $b`
else
echo "please enter two real number"
fi
echo "result" $c

OUTPUT:

Add to real number


******************
enter the two values
34 56
result 90

Add to real number


******************
enter the two values
-8 6
please enter two real number
result

RESULT:

Thus the program was succesfully executed.

Ex.no:22
Date: 1/10/18
Calculate Electricity Bill
*******************************************************************************
*

AIM:
To write a linux script to Calculate Eb Bill

CODING:

clear

22
echo "enter the customer name"
read na
echo "enter current reading"
read cur
echo "enter previous reading"
read pre
co=`expr $cur - $pre`
if [ $co -le 100 ]
then
amt=`expr $co \* 1`
else
amt=`expr $co \* 2`
fi
echo "customer name is $na"
echo "bill amonut is $amt"

OUTPUT:

enter the customer name


raji
enter current reading
3000
enter previous reading
2800
customer name is raji
bill amonut is 400

RESULT:

Thus the program was succesfully executed.

23

You might also like