0% found this document useful (0 votes)
2 views

linu

Linux programs

Uploaded by

ca-684
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

linu

Linux programs

Uploaded by

ca-684
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

echo Arithmetic operation on floating point numbers

echo Enter the first number


read a
echo Enter the second number
read b
sum1=`expr "$a + $b" | bc`
product=`expr "$a * $b" | bc`
div=`expr "$a / $b" | bc`
mod=`expr "$a % $b" | bc`
diff=`expr "$a - $b" | bc`
echo The sum is : $sum1
echo The difference is : $diff
echo The product is : $product
echo The division result is : $div
echo The remainder is : $mod

echo The Area, Perimeter and Circufernce Calculator


echo enter the length of the rectange
read length
echo enter the breadth of the rectangle
read width
area=`expr "$length * $width" | bc`
perim=`expr "2 * ($length + $width)" | bc`
echo area is $area
echo perimeter is $perim
echo Enter the radious
read rad
vol=`expr "2 * 3.1415 * $rad" |bc`
cir=`expr "3.1415 * $rad * $rad" |bc`
echo the area of circle is $vol
echo the circumference is $cir

echo enter the number :


read num
dsum=0
rev=0
num1=$num
while ((num > 0))
do
check=$((num % 10))
((dsum = check + dsum))
((num = num / 10))
((rev = rev * 10 + check))
done
echo The Sum is: $dsum
echo The Reverse is: $rev
if [ $rev -eq $num1 ]
then
echo the number is palindrome
else
echo the number is not palindrome
fi

n=0
echo Number at odd position
read -p "Enter a number : " n
echo $n
l=${#n}
i=1
echo The odd position numbers are
while [ $i -le $l ]
do
d=$(echo $n | cut -c $i)
echo -n $d " "
((i= i + 2))
Done

echo find Gross Salary


echo Enter total no of employees:
read n
count=1
while [ $count -le $n ]
do
echo enter the basic salary:
read bsalary
if [ $bsalary -lt 15000 ]
then
gsal=`expr "$bsalary + ($bsalary * .3) + 500 + ($bsalary * .1)" | bc`
echo the gross salary is $gsal
else
gsal=`expr "$bsalary + ($bsalary * .5) + 1000 + ($bsalary * .15)" | bc`
echo the gross salary is $gsal
fi
((count = count + 1))
Done

echo find the Loss or profit


echo Enter the cost price of the product:
read cost
echo Enter the Selling price of the product:
read sell
if [ $cost -gt $sell ]
then
loss=`expr "$sell - $cost" | bc`
echo loss is $loss
else
profit=`expr "$sell - $cost" | bc`
echo profit is $profit
fi

echo Student Marks and Grade Program


echo "enter Total no of students :"
read n
count=1
while [ $count -le $n ]
do
total=0
for((i=1; i <= 5;i++))
do
echo "enter marks of subjects $i : "
read marks
total=$(( $total + $marks))
done
percent=`expr "$total * 100 / 500" |bc`
echo The Total Marks is $total and Percentage is $percent
if [ $percent -ge 80 ]
then
echo The Grade is: Distinction
elif test $percent -ge 60
then
echo The Grade is: First Class
elif test $percent -ge 50
then
echo The Grade is: Second Class
else
echo Failed
fi

echo find an entered is upper / lower / alpha numeric


echo Enter the character
read ch

if [[ "$ch" =~ [ABCDEFGHIJKLMNOPQRSTUVWXYZ] ]]
then
echo The character is UPPERCASE
elif [[ "$ch" =~ [a-z] ]]
then
echo The character is Lowercase
elif [[ "$ch" =~ [0-9] ]]
then
echo The character is a Digit
elif [[ "$ch" =~ ['!@#\$%^\&*()_-+'] ]]
then
echo it is a Special Character
else
echo Nothing matching....
fi

echo The multiplication table


echo Enter the number:
read n
echo Enter the Range upto where the table :
read r
i=0
while [ $i -le $r ]
do
echo " $n x $i = `expr $n \* $i` "
i=`expr $i + 1`
done

echo find the power of number


echo "Input number"
read a
echo "Input power"
read b
counter=0
ans=1
while [ $b -ne $counter ]
do
ans=`expr $ans \* $a`
counter=`expr $counter + 1`
done
echo "$a power of $b is $ans"

echo Program to find prime between 1 to n


echo enter the upper limit
read upper
flag=0
number=2
echo The prime numbers are:
while [ $number -le $upper ]
do
flag=0
for((i=2; i<=$number/2; i++))
do
num=$((number % i))
if [ $num -eq 0 ]
then
flag=1
break
fi
done
if [ $flag -eq 0 ]
then
echo -n $number " "
fi
((number = number + 1))
Done

echo Program to print all Combinations of A, B, C


for c1 in {a..c}
do
for c2 in {a..c}
do
for c3 in {a..c}
do
echo -n " " "$c1$c2$c3"
done
echo
done
done

function fact() {
local n=$1
if [[ $n -eq 0 ]]; then
echo 1
else
echo $((n*$(fact $n-1)))
fi
}
echo program to find the sum of series
echo enter the upper limit
read n
sum=0
for ((i=1;i<=n;i++))
do
sum=`expr "$sum + ($i / $(fact $i))" |bc`
done
echo "sum n series is $sum"

echo leap year


echo enter the year to test. press enter a year to current
read year
if [ -z "$year" ]
then
year=$(date +"%Y")
fi
check=`echo $year % 4 | bc`
if [ $check -eq 0 ]
then
if [ `echo $year % 100 |bc ` -eq 0 ]
then
if [ `echo $year % 400 |bc` -eq 0 ]
then
echo the year $year is a leap year
else
echo the year $year is not a leap year
fi
else
echo the year $year is a leap year
fi
else
echo the year $year is not a leap year
fi

while :
do
clear
echo The program to List, Display, Make Dir, Copy and Rename File printf "\nSELECT
YOUR OPTION\n"
echo "1. List Directory"
echo "2. Create Directory"
echo "3. Copy Files"
echo "4. Display Files"
echo "5. Rename a File"
echo "6. Delete a file"
echo "7. Exit from program"
echo "Enter your menu choice [1-7]: "
read choice
case $choice in
1) ls
read -p "Press any key...: " ;;
2) echo "Enter The Directory Name"
read dirName
mkdir $dirName;
echo Directory Created...
read -p "Press any key...: " ;;
3) read -p "Enter name of the file to be copied :" fname read -p "Enter the name of the new
file :" newname cp $fname $newname
echo File copied...
read -p "Press any key...: " ;;
4) echo Enter the file name to be read
read fname
cat $fname
read -p "Press any key...: " ;;
5) echo Enter the file name
read fname
echo Enter the new name to be renamed
read newname
mv $fname $newname
echo file renamed...
read -p "Press any key...: " ;;
6) echo Enter the file name
read fname
rm $fname
echo file deleted...
read -p "Press any key...: " ;;
7) echo "Quitting ..."
exit;;
*) echo "invalid option";;
esac
done

while :
do
clear
printf "\nSELECT YOUR OPTION\n"
echo "1. Contents of /etc/passwd"
echo "2.list of OUTPUT: 'who'"
echo "3. Present working directory"
echo "4. Exit from program"
echo "Enter your menu choice [1-7]: "
read choice
case $choice in
1) cat etc/passwd
read -p "Press any key...: " ;;
2) who
read -p "Press any key...: " ;;
3) pwd
read -p "Press any key...: " ;;
4) echo "Quitting ..."
exit;;
*) echo "invalid option";;
esac
done

echo "Enter LOGNAME OR UID"


read input
if [[ $input ]] && [ $input -eq $input 2>/dev/null ]
then
echo "Number of terminals are "
cat /etc/passwd | grep $input -c
else
cat /etc/passwd>userlist
echo "Number of terminals are "
grep -c $input userlist
fi

echo file append program


echo -n "Enter file name : "
read file
if test -f "$file"
then
if [ -w $file ]
then
echo you have write permission on file: $file
echo Enter text and press ctrl+d to save
cat >> $file
else
echo no write permission
fi
printf "\nThe Appended file " $file " is follows :\n"
cat $file
else
echo The file does not exists!!
Fi

echo Linux Program to List files with Permissions


for i in `ls`
do
exist=""
read=""
write=""
exe=""
#-e Returns true value if file exists.
if [ -e $i ]
then
exist=" file exists "
fi
#-r Return true value if file exists and is readable
if [ -r $i ]
then
read=" readabale "
fi
#-w Return true value if file exists and is writable
if [ -w $i ]
then
write=" Writable "
fi
#-x Return true value if file exists and is executable.
if [ -x $i ]
then
exe=" Executable "
fi
printf "%-35s" $i
echo -- $exist -- $read -- $write -- $exe
done

len=${#@}
if [ $len -eq 0 ]
then
echo No arguments please use with two arguments
exit
else
file1=$1
file2=$2
if [ -f $file1 -a -f $file2 ]
then
echo The file Comparison Program
if cmp -s "$file1" "$file2"
then
printf 'The file "%s" is the same as "%s"\n'"$file1" "$file2"
read -p "do you want to delete the second file y/n " chk
if [ "$chk" = "y" ]
then
rm $file2
echo The second duplicate file deleted...
fi
else
printf 'The file "%s" is different from "%s"\n'"$file1" "$file2"
fi
else
echo "File(s) not exists!"
fi
fi

len=${#@}
if [ $len -eq 0 ]
then
echo No arguments please use with some arguments
exit
else
echo Check files exists or not Program
for i in $@
do
if [ -e $i ]
then
echo $i " file exists "
else
echo $i " File not Exists "
fi
done
fi

while :
do
clear
echo
echo "
"
echo Student Management System
echo "
"
printf "\nSELECT YOUR OPTION\n"
echo "1. Add Record"
echo "2. View All Records"
echo "3. View a Record"
echo "4. Delete a Record"
echo "5. Exit from program"
echo "Enter your menu choice [1-5]: "
read choice
case $choice in
1) echo "enter rollnumber of student "
read number
echo "enter name of student "
read name
echo "enter marks Subject1 "
read sub1
echo "enter marks Subject2 "
read sub2
echo "enter marks Subject3 "
read sub3
total=`expr "$sub1 + $sub2 + $sub3" |bc`
record="$number,$name,$sub1,$sub2,$sub3,$total"
echo $record >> stud_data.dt
read -p "Press any key...: " ;;
2) printf "Number \tName\tSubject1\tSubject2\tSubject3\tTotal\n"
while read line
do for j in $line do
sno=$(echo "$j" | cut -d "," -f 1)
snm=$(echo "$j" | cut -d "," -f 2)
sub1=$(echo "$j" | cut -d "," -f 3)
sub2=$(echo "$j" | cut -d "," -f 4)
sub3=$(echo "$j" | cut -d "," -f 5)
total=$(echo "$j" | cut -d "," -f 6)
done printf '%s\t%s\t\t%s\t%s\t\t%s\t\t%s\n' "$sno" "$snm"
"$sub1" "$sub2" "$sub3" "$total"
done < stud_data.dt
read -p "Press any key...: " ;;
3) read -p "Enter the Student Number " csno
while read line
do for j in $line do 27
sno=$(echo "$j" | cut -d "," -f 1)
snm=$(echo "$j" | cut -d "," -f 2)
sub1=$(echo "$j" | cut -d "," -f 3)
sub2=$(echo "$j" | cut -d "," -f 4)
sub3=$(echo "$j" | cut -d "," -f 5)
total=$(echo "$j" | cut -d "," -f 6)
done
if [ $csno -eq $sno ]
then break
fi done < stud_data.dt
if [ $csno -eq $sno ] then
printf "Number \tName\tSubject1\tSubject2\tSubject3\tTotal\n"
printf '%s\t%s\t\t%s\t%s\t\t%s\t\t%s\n' "$sno" "$snm" "$sub1"
"$sub2" "$sub3" "$total"
else
echo No records found
fi
read -p "Press any key...: " ;;
4) echo "enter roll number "
read rn
grep ^$rn stud_data.dt
if [ $? -ne 0 ]; then
echo "record for roll number does not exist "
else
grep -v $rn stud_data.dt > tmp
cp tmp stud_data.dt
echo "deletion complete "
fi read -p "Press any key...: " ;;
5) echo "Quitting ..."
exit;;
*) echo "invalid option";;
esac
done

echo bubble sort Program


echo "enter the number of elements"
read n
echo "enter $n values"
for((i=0 ; i < $n ; i++))
do
read arr[$i]
done
echo "array values before sort"
echo ${arr[*]}
for((i=0 ; i < n ; i++))
do
for((j=0 ; j < n-i-1; j++))
do
if test ${arr[j]} -gt ${arr[((j+1))]}
then
temp=${arr[j]}
arr[$j]=${arr[$((j+1))]}
arr[$((j+1))]=$temp
fi
done
done
echo "array values after sort"
echo ${arr[*]}

function fact()
{local n=$1
if [[ $n -eq 0 ]]; then
echo 1
else
echo $((n*$(fact $n-1)))
fi
}
#Factorial function
echo
echo The Factorial Program with Function
echo Enter the number
read number
echo The factorial is $(fact $number)
function palindrome
(){local input=$1
reverse=""
len=${#input}
for ((i=$len-1; i>=0; i-- ))
do
reverse="$reverse${input:$i:1}"
done
if [ $input == $reverse ]
then
echo the word $input is palindrome
else
echo the word $input is not palindrome
fi
}
echo The Palindrome Checking Program
read -p "Enter the Word to check " word
echo $(palindrome $word)

echo find word mca


len=${#@}
flag=0
if [ $len -eq 0 ]
then
echo No arguments please use with some arguments
exit
else
for i in $@
do
if grep -i -q "mca" $i
then
flag=1
break
fi
done
if [ $flag -eq 1 ]
then
echo "the word mca is found in the file $i"
echo The related content is
grep -i "mca" $i
echo
echo the number of times it occours
grep -c -i "mca" $i
else
echo "the word mca is not found in any file"
fi
fi
echo The program to find word mca
len=${#@}
flag=0
if [ $len -eq 0 ]
then
echo No arguments please use with some arguments
exit
else
for i in $@
do if grep -i -q "mca" $i
then
flag=1
break
fi
done
if [ $flag -eq 1 ]
then
echo "the word mca is found in the file $i"
echo The related content is
grep -i "mca" $i
echo echo the number of times it occours
grep -c -i "mca" $i
else
echo "the word mca is not found in any file"
fi
fi

len=${#@}
echo
echo The File and Directory checking program
if [ $len -eq 0 ]
then
echo No arguments please use with some arguments
exit
else
echo Check files exists or not Program
for i in $@
do
#-e Returns true value if file exists.
if [ -d $i ]
then
echo $i " is a Directory"
else
echo $i " is a file "
echo -n and the number of lines are:
wc -l < $i
fi
done
fi
len=${#@}
echo
echo Count Lines and Words program
if [ $len -eq 0 ]
then
file="myfile.txt"
echo No files provided...
echo Enter text and press ctrl+d to save
cat >> $file
else
file=$1
fi
echo -n The number of lines are:
wc -l < $file
echo -n and the number of words are:
wc -w < $file

len=${#@}
echo File copying program
if [ $len -eq 0 ]
then
echo No arguments please use with some arguments
exit
elif [ `expr $len % 2` -eq 0 ]
then
for ((i=1; i<$len; i+=2))
do
if [ -f ${!i} ]
then
((j=i+1))
cp ${!i} ${!j}
echo Files Copied ${!i} to ${!j}
else
echo ${!i} " File not Exists "
fi
done
else
echo you are entered odd files
echo Error!!
Fi

You might also like