🏮 Final Programs:
--Percentage
echo "Enter Percentage:"
read per
if [ $per -ge 85 ]
then
echo "Grade A"
elif [ $per -ge 60 ] && [ $per -lt 85 ]
then
echo "Grade B"
elif [ $per -ge 40 ] && [ $per -lt 60 ]
then
echo "Grade C"
else
echo "Fail"
fi
#!bin/bash
for (( c=1 ; c<=5 ; c++ ))
do
echo "Welcome $c times"
done
--table
echo "Enter no to generate table :"
read no
for (( i=1 ; i<=10 ; i++ ))
do
echo " "$((no*i))
done
--inverted pyramid
for (( i=1 ; i<8 ; i=i+2 ))
do
for (( j=1 ; j<7 ;j++ ))
do
if [ $i -le $j ]
then
echo -n "* "
else
echo -n " "
if [ $i -eq 7 -a $j -eq 5 ]
then
echo "* "
fi
fi
done
echo ""
done
---one side pyramid
echo "Enter no:"
read no
for (( i=0 ; i<no ; i++ ))
do
for (( j=0 ; j<i ; j++ ))
do
echo -n "* "
done
echo
done
---fibonacci
echo "Enter no to fibonacci:"
read num
no1=0
no2=1
echo ""$no1
echo ""$no2
for (( i=2 ; i<num ; i++ ))
do
no3=$(( no1+no2 ))
echo ""$no3
no1=$no2
no2=$no3
done
--addition of digits in a number
read -n 5 -p 'Enter 5 digit no:'$no
sum1=0
while [ $no -gt 0 ]
do
r= $(( no % 10 ))
sum1= $(( sum1 + r ))
no= $(( no / 10 ))
done
echo "Addition of digits of $no is $sum"
----mathematical operations using switch
read -p "enter 1st no" no1
read -p "enter 2nd no" no2
echo -e "1 add \n2 subtract \n3 multiply \n4 divide"
read -p "enter your choice " ch
case $ch in
1) c=$(( $no1 + $no2 ))
echo "addition = $c ";;
2) c=$(( $no1 - $no2 ))
echo "subtraction = $c ";;
3) c=$(( $no1 * $no2 ))
echo "multiplication = $c ";;
4) c=$(( $no1 / $no2 ))
echo "division = $c ";;
*) echo "invalid choice";;
esac
---Days of week
for i in sun mon tue wed thur fri sat
do
echo $i
done
---file program to check is it exist
echo -e "Enter the name of file: "
read file_name
if [ -f "$file_name" ]; then
echo "$file_name exists"
else
echo "$file_name does not exist"
fi
---To check whether entered input is file or directory
echo "Enter file or directory name"
read file
if [ -f "$file" ]
then
echo "$file It is a file"
else
if [ -d "$file" ]
then
echo "$file It is a directory"
fi
fi
---To list files present in given directory
echo "Enter directory name"
read dir
if [ -d $dir ]
then
echo "list of files in the directory"
cd $dir
ls
else
echo "Enter proper directory name"
fi
---File operations like rename,copy,remove
echo "enter file name"
read file
if [ -d "$file" ]
then
echo "it is directory"
else
echo "Enter your choice"
echo "1.copy file "
echo "2.rename file"
echo "3.remove"
read ch
case "$ch" in
"1") echo "Enter file name to be copied"
read cpfile
cp $file $cpfile
echo "File copied"
;;
"2") echo "enter name to rename "
read newname
mv $file $newname
echo "$file renamed as $newname"
;;
"3") rm $file
echo "removed "
;;
esac
fi
---To check file has read write execute permissions
echo "Enter the File : "
read line
if [ -r $line ]
then
echo "$line has all the read permissions"
fi
if [ -x $line ]
then
echo "$line has all the execute permissions"
fi
if [ -w $line ]
then
echo "$line has all the write permissions"
fi
--Displays list of executable files in current directory
pwd
ls > f
exec < f
while read line
do
if [ -f $line ]
then
if [ -x $line ]
then
echo "$line"
fi
fi
done
--Display list of files which has all read,write,execute permission
pwd
ls > f
exec < f
while read line
do
if [ -f $line ]
then
if [ -x $line ] && [ -r $line ] && [ -w $line ]
then
echo "$line"
fi
fi
done
---Assign all the permission to file
echo "Enter file name:"
read file
chmod u+xrw $file
chmod o+xrw $file
chmod g+xrw $file