Shell Programming Submit a single pdf file that shows the co

Shell Programming

Submit a single pdf file that shows the contents of the script as specified below.

•      Suppose there is file where each line is made up of the first name of a student followed by a score (the first name and score are separated by a space).

•      Write a script to do the following tasks.

            Accept the name of the file through the first argument (i.e. $1)

            Put the first names into an array called names

            Put the corresponding scores into an array called scores

            Print names and corresponding scores (i.e. output should look the same as the input file)

            Calculate the average of all scores, and print it.

            Sort the arrays in terms of scores (in ascending order)

            Print sorted names and scores.

            Find the highest scorer, and print the information.

            Find the lowest scorer, and print the information.

Solution

The following shell script does the required things that mentioned in the Question.

#!/bin/bash

file = \"$1\" // Accepting the file name through first argument

declare -a names

declare -a scores

while read line

do

IFS=\' \' read -r -a names ,scores <<< \"$line\" //Split the line on spaces and store them into arrays

done < \"$file\"

for (i=0;i<${#names[@]};i++)

do

echo \"$names[$i] $scores[$i]\"

done

average=0

total=0

for var in \"${scores[@]}\"

do

total = $((total+var))

done

average = (($total/5))

echo $average

declare -a sortedScores

for name in \"${names[@]}\"

do

echo \"$name\"

done | sort

for score in \"${scores[@]}\"

do

echo \"$score\"

done | sort

echo \"The Lowest scorer is ${names[0]}\" // Printing the lowest scorer

echo \"The highest scorer is ${names[@]}\" // Printing the highest scorer

  

Shell Programming Submit a single pdf file that shows the contents of the script as specified below. • Suppose there is file where each line is made up of the f
Shell Programming Submit a single pdf file that shows the contents of the script as specified below. • Suppose there is file where each line is made up of the f

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site