Sei sulla pagina 1di 10

Program 1

if [ $# -eq 0 ]
then
echo "No Arguments"
fi
echo $* > f02
i=$#
while [ $i -ne 0 ]
do
echo $(cut -d ' ' -f$i f02)
i=$(expr $i - 1)
done > f01
echo $(cat f01)
=====================OUTPUT========================
[root1@localhost unixprogs]$ vi p111.sh
[root1@localhost unixprogs]$ sh p111.sh 1 2 3
3 2 1
[root1@localhost unixprogs]$ sh p111.sh a b c
c b a
===================================================
Program 2
v=`date|cut -c 9-10`
echo "$v"
if [ $v -lt 10 ] ; then
cal|sed s/"$v"/*/
else
cal|sed s/"$v"/**/
fi
===================OUTPUT=========================
[root1@localhost unixprogs]$ vi ppp22.sh
[root1@localhost ~]$ sh ppp22.sh
7
October 2013
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 * 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
====================================================
Program 3
echo
read
if [
echo
elif
echo
else

"Enter the 3 numbers"


a b c
$a -lt $b -a $a -lt $c ] ; then
"$a is smallest"
[ $b -lt $c ]; then
"$b is smallest"

echo "$c is smallest"


fi
==========================OUTPUT====================
[root1@localhost unixprogs]$ vi p3.sh
[root1@localhost unixprogs]$ sh p3.sh
Enter the 3 numbers
21 34 56
21 is smallest
[root1@localhost unixprogs]$ sh p3.sh
Enter the 3 numbers
32 12 24
12 is smallest
==========================================================
Program 4
echo
read
if [
then
echo
echo
else
echo
fi

"Enter the file name "


filename
-f $filename ]
"The creation time of $filename :"
$(ls -l $filename|cut -d' ' -f1)
"File not found"

=======================OUTPUT===========================
[root1@localhost unixprogs]$ vi p4.sh
[root1@localhost unixprogs]$ sh p4.sh
Enter the file name
p3.sh
The creation time of p3.sh :
-rw-rw-r--.
[root1@localhost unixprogs]$ sh p4.sh
Enter the file name
p2
File not found
======================================================
Program 5
echo "Enter the String"
read str
v=$(expr "$str" : '.*')
echo $v
if [ $v -lt 10 ]
then
echo "No of Characters are less than 10"
else
echo "No of Characters are more than 10"

fi
=========================OUTPUT=========================
[root1@localhost unixprogs]$ vi p6.sh
[root1@localhost unixprogs]$ sh p6.sh
Enter the String
GFGC MUDALGI
12
No of Characters are more than 10
[root1@localhost unixprogs]$ sh p6.sh
Enter the String
GFGC
4
No of Characters are less than 10
=========================================================
Program 6
n=$1
sum=0
while [ $n -gt 0 ]
do
rm=$(expr $n % 10)
sum=$(expr $sum + $rm)
n=$(expr $n / 10)
done
echo "sum=$sum"
=======================OUTPUT=======================
[root1@localhost unixprogs]$ vi add.sh
[root1@localhost unixprogs]$ sh add.sh 123
sum=6
[root1@localhost unixprogs]$ sh add.sh 248
sum=14
======================================================
Program 7
echo
read
echo
echo
echo
echo

"Enter the number"


n
"Decimal to Binary Conversion"
"obase=2; $n" | bc
"Decimal to Hexadecimal Conversion"
"obase=16; $n" | bc

====================================OUTPUT=====================
[root1@localhost unixprogs]$ vi p7.sh
[root1@localhost unixprogs]$ sh p7.sh
Enter the number

14
Decimal to Binary Conversion
1110
Decimal to Hexadecimal Conversion
E
[root1@localhost unixprogs]$ sh p7.sh
Enter the number
16
Decimal to Binary Conversion
10000
Decimal to Hexadecimal Conversion
10
====================================================================
Program 8
echo "Enter the basic salary"
read bs
if [ $bs -lt 15000 ]
then
hr=`echo "scale=2; $bs* 10 / 100"|bc`
da=`echo "scale=2; $bs* 85 /100"|bc`
else
hr=500
hr=`echo "scale2; $bs*15 /100"|bc`
da=$(echo "scale=2;$bs * 90/100"|bc)
fi
gs=`echo "scale=2; $bs+$hr+$da"|bc`
echo "Gross salary = $gs"
===========================OUTPUT======================
[root1@localhost ~]$ vi p8.sh
[root1@localhost ~]$ sh p8.sh
Enter the basic salary
23000
Gross salary = 23000
24150.00
[root1@localhost ~]$ sh p8.sh
Enter the basic salary
12000
Gross salary = 23400.00
=====================================================
Program 9
echo " Menu
1.HOME
2.PATH
3.Login name
4.Login shell
5.User"
echo "Enter Ur Choice"
read ch

case $ch in
1) echo $HOME ;;
2) echo $PATH ;;
3) echo $LOGNAME;;
4) echo $SHELL ;;
5) echo $USER ;;
*) echo "INVALID OPTION"
esac
==========================OUTPUT===============================
[root1@localhost unixprogs]$ vi p9.sh
[root1@localhost unixprogs]$ sh p9.sh
Menu
1.HOME
2.PATH
3.Login name
4.Login shell
5.User
Enter Ur Choice
2
/
usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/loc
al/sbin:/usr/sbin:/sbin:/home/root1/bin
[root1@localhost unixprogs]$ sh p9.sh
Menu
1.HOME
2.PATH
3.Login name
4.Login shell
5.User
Enter Ur Choice
3
root1
=========================================================================
=============
Program 10
d=`date|cut -d ' ' -f4`
echo Time is $d
v=`date|cut -c 12-13`
echo
if [ $v -lt 12 ]
then
echo "Good Morning!!!!!"
fi
if [ $v -gt 12 -a $v -lt 16 ]
then
echo "Good Afternoon"
fi
if [ $v -gt 17 -a $v -lt 23 ]
then
echo "Good Evening"
fi

=============================OUTPUT================================
[root1@localhost unixprogs]$ vi p10.sh
[root1@localhost unixprogs]$ sh p10.sh
Time is 9
Good Morning!!!!!
===============================================================
program 11
if [ $# -eq 0 ]
then
echo "No Arguments"
else
echo "Enter the word you want to delete"
read w
echo "The contains of the file:"
for i in $*
do
grep -v "$w" $i|cat >$i
cat $i
done
fi
===========================================
[root1@localhost unixprogs]$ sh p11.sh f1
Enter the word you want to delete
Hi
The contains of the file:
Hello
gud morning
[root1@localhost unixprogs]$ sh p11.sh
No Arguments
=============================================
program 12
if [ $# -lt 2 ]
then
echo "Operation cannot perform"
else
v=`echo "$1 ^ $2 "|bc`
echo "$v"
fi
~
===================output=========================
[root1@localhost unixprogs]$ sh p12.sh 2 3
8
[root1@localhost unixprogs]$ sh p12.sh 2
Operation cannot perform

=================================================
program 13
if [ $# -eq 3 ]
then
limit=`wc -l < $1`
if [ $2 -le $limit -a $3 -le $limit ]
then
sed -n "$2,$3p" $1
fi
else
echo " Improper Arguments"
fi
~
~
~
========================output========================
[root1@localhost unixprogs]$ sh p13.sh p11.sh 1 4
for i in $*
do
if [ -d $i ]
then
[root1@localhost unixprogs]$ sh p13.sh p11.sh 1
Improper Arguments
==============================================
program 14
echo "Patterns"
echo MENU
echo "1.Pattern 1
2.Pattern 2"
echo "Enter UR Choice"
read ch
case $ch in
1)
for ((i=1; i <= 4; i++))
do
for ((j=1; j <= $i; j++))
do
printf "$j"
done
printf "\n"
done
;;
2)
for ((
do
for ((
do
printf
done
printf

i=1; i <= 4; i++ ))


j=1; j <= $i; j++ ))
"$i"
"\n"

done
;;
esac
================================OUTPUT===============================
[root1@localhost unixprogs]$ vi p14.sh
[root1@localhost unixprogs]$ sh p14.sh
Patterns
MENU
1.Pattern 1
2.Pattern 2
Enter UR Choice
1
1
12
123
1234
[root1@localhost unixprogs]$ sh p14.sh
Patterns
MENU
1.Pattern 1
2.Pattern 2
Enter UR Choice
2
1
22
333
4444
====================================================
program 15
echo
read
if [
then
echo
echo
else
echo
fi

"Enter the file name"


name
-f $name ]
$name |tr '[a-z]' '[A-Z]'
`cat $name |tr '[a-z]' '[A-Z]'`
"file does not exist"

========================================output===========================
=========
[root1@localhost unixprogs]$ vi p15.sh
[root1@localhost unixprogs]$ sh p15.sh f1
Enter the file name
f1
F1
WELCOME TO GFGC MUDALGI

[root1@localhost unixprogs]$ sh p15.sh f2


Enter the file name
f2
F2
DO OR DIE
=========================================================================
===
program 16
#!/bin/bash
echo "enter maximum number"
read n
# taking input from user
echo "enter Numbers in array:"
for (( i = 0; i < $n; i++ ))
do
read nos[$i]
done
# Now do the Sorting of numbers
for (( i = 0; i < $n ; i++ ))
do
for (( j = 0; j < $n-$i; j++ ))
do
if [ ${nos[$j]} -gt ${nos[$j+1]} ]
then
t=${nos[$j]}
nos[$j]=${nos[$j+1]}
nos[$j+1]=$t
fi
done
done
# Printing the sorted number in ascending order
echo -e "\nSorted Numbers "
for (( i=0; i < $n; i++ ))
do
echo ${nos[$i]}
done
===================================output============================
[root1@localhost unixprogs]$ vi p16.sh
[root1@localhost unixprogs]$ sh p16.sh
enter maximum number
5
enter Numbers in array:
9
4
3
2
5
Sorted Numbers
2

3
4
5
9
[root1@localhost unixprogs]$

Potrebbero piacerti anche