Some Shell Scripts

This post covers some of the shell scripts to:

  1. Find roots of the quadratic equation

  2. To count the number of files and directories in directory

  3. Store the information of a student such as student name, department, year and semester in an array and display all of the information store in an array.

  4. Display grade of student for marks of 5 subjects given as input

Programs

  1. Find roots of the quadratic equation

[php]
#!bin/sh
#root of quadratic equations
echo -e "Enter the value of a:\c"
read a
echo -e "Enter the value of b:\c"
read b
echo -e "Enter the value of c:\c"
read c
echo "The two roots of the quadratic equations are "
echo "(-$b+sqrt($b^2-4*$a*$c))/2*$a" | bc
echo "(-$b-sqrt($b^2-4*$a*$c))/2*$a" | bc
[/php]

  1. To count the number of files and directories in directory

[php]
#!bin/sh
#Program code to compute the total number of file and folder
echo -e "The total number of files present is :\c"
ls -l | grep "^-" | wc -l
echo -e "The total number of directories present is :\c"
ls -l | grep "^d" | wc -l
[/php]

  1. Store the information of a student such as student name, department, year and semester in an array and display all of the information store in an array.

[php]
#!bin/sh
#Program to store student info in array and then display it.
echo -e "Enter the name:\c"
read name
echo -e "Enter the department name:\c"
read department
echo -e "Enter the year of join:\c"
read year
echo -e "Enter the current semester:\c"
read semester
student_info=("$name" "$department" "$year" "$semester")
echo ${student_info[@]}
[/php]

  1. Display grade of student for marks of 5 subjects given as input

    [php]#!bin/sh
    #Grading a student based on his 5 subject marks:
    echo -e "Enter sub1 mark:\c"
    read sub1
    echo -e "Enter sub2 mark:\c"
    read sub2
    echo -e "Enter sub3 mark:\c"
    read sub3
    echo -e "Enter sub4 mark:\c"
    read sub4
    echo -e "Enter sub5 mark:\c"
    read sub5
    sum=$(($sub1+$sub2+$sub3+$sub4+$sub5))
    avg=$(echo "$sum/5" | bc)
    if [ $avg -ge 80 ]
    then
    echo Excellent. You have scored $avg%
    elif [ $avg -ge 70 ]
    then
    echo Very Good. You have scored $avg%
    elif [ $avg -ge 60 ]
    then
    echo Good. You have scored $avg%
    elif [ $avg -ge 50 ]
    then
    echo Satisfactory. You have scored $avg%
    elif [ $avg -ge 40 ]
    then
    echo Unsatisfactory. You have scored $avg%
    else
    echo Failed. You have scored $avg%
    fi[/php]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *