Question

Write a Bash shell script testaverager.sh that will determine if a person has passed a class....

Write a Bash shell script testaverager.sh that will determine if a person has passed a class. It should take in as a command-line parameter a name (first and last name) as well as a series of numbers. This list could be of any length. Your script should start by calculating the average of the scores. If the result is below 70, it should output “Sorry [name] but you will have to retake the class!”. If the result is 70 or greater, congratulation the student with some message about succeeding at the class

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

--------------

cat testavaerager.sh
#!/bin/bash

#regular expression to test if variable is number
re='^[0-9]+$'

#loop for reading command line arguments which is a number
for var in "$@"
do
if [[ $var =~ $re ]] ; then
sum=$((sum + var))
count=$((count+1))
fi
done

#calculating the precentage
per=`expr $sum / $count`

#displaying the percentage
echo "Percentage: " $per

#verifying if it is less than 70 or not
if [ $per -lt 70 ]
then
echo "Sorry $1 $2 but you will have to retake the class!"
else
echo "Congatulations $1 $2 you passed the class"
fi

--------------

Output

---------------

root:~ # sh testavaerager.sh Mike kelly 60 70 90 50 94
Percentage: 72
Congatulations Mike kelly you passed the class


root:~ # sh testavaerager.sh Bill Harry 34 56 89 12 34 56 67
Percentage: 49
Sorry Bill Harry but you will have to retake the class!

Add a comment
Answer #2

Here's an example of a Bash shell script named "testaverager.sh" that calculates the average of scores and determines if a person has passed a class:

bashCopy code#!/bin/bash# Check if the number of command-line arguments is less than 2if [ $# -lt 2 ]; then
  echo "Usage: $0 <name> <score1> <score2> ..."
  exit 1fi# Extract the name from the command-line argumentsname=$1shift# Calculate the average of scoressum=0
count=0for score in "$@"; do
  sum=$((sum + score))
  count=$((count + 1))doneaverage=$((sum / count))# Check if the average is below 70if [ $average -lt 70 ]; then
  echo "Sorry $name, but you will have to retake the class!"else
  echo "Congratulations $name! You have succeeded in the class."fi

To use this script, save it to a file (e.g., testaverager.sh), make it executable (chmod +x testaverager.sh), and then run it with the desired name and scores as command-line arguments. For example:

rubyCopy code$ ./testaverager.sh John Doe 80 75 85 90Congratulations John Doe! You have succeeded in the class.
rubyCopy code$ ./testaverager.sh Jane Smith 60 65 55Sorry Jane Smith, but you will have to retake the class!

Make sure to replace the score values with the actual scores for accurate calculations.


answered by: Mayre Yıldırım
Add a comment
Know the answer?
Add Answer to:
Write a Bash shell script testaverager.sh that will determine if a person has passed a class....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Problem 1 Write a BASH script to create a user account from the Linux system on...

    Problem 1 Write a BASH script to create a user account from the Linux system on which your script is run. The script should process two positional parameters. First positional parameter is supposed to be a string with a user name (e.g., user_name) Second positional parameter is supposed to be a string with a user password (e.g., user_password) In your script: Check if two positional parameters were passed to your script when it was invoked If NOT, print an appropriate...

  • Write a script using the bash shell to add new users to the system. The script...

    Write a script using the bash shell to add new users to the system. The script should read user information from a comma-delimited file (each field should be separated by a comma). The filename should be passed to the script as a command-line argument. Be sure the script contains a usage clause. The input file should contain the user information with one user per line. As the system administrator, you should determine the information necessary for each user to properly...

  • Write a bash shell script to carry out each of the following tasks: What to Do:...

    Write a bash shell script to carry out each of the following tasks: What to Do: a. Safe Delete: When you use the “rm” command in Linux, it will delete the specified files, with no chance for recovering them back later. Write a script (called srm) that will safely delete the files passed to it as command-line arguments. For example, typing the command: “srm file1 file2 file3”, the script shall not actually delete these files, but instead it shall move...

  • 1. Write 4 simple shell scripts. Each question will ask you to write one or more...

    1. Write 4 simple shell scripts. Each question will ask you to write one or more command lines to solve a problem. Put the command(s) from each problem into a file named problem1.sh, problem2.sh Note that if you want your script to be interpreted by bash, each file should start with the line: #!/bin/bash and will have one (or more, as needed) commands following it. To execute each script in bash, so you can determine if it is working or...

  • Write a bash shell script, deleteFilesWithZeroLength.sh, that removes all zero length ordinary files in the directory passed as an optional argument. If you do not specify the directory argument, the script uses the present working directory as the defaul

    Write a bash shell script, deleteFilesWithZeroLength.sh, that removes all zero length ordinary files in the directory passed as an optional argument. If you do not specify the directory argument, the script uses the present working directory as the default argument. Do appropriate exception handling in your script such as:If the arguments are more than 1, print out “Too many arguments passed”.If the argument passed is a regular file, print out “XXX is regular file”.1c. If the directory doesn’t exist, print out “Directory...

  • Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them....

    Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them. The program should print the result of the comparison. Specifically, it should print “<x> is <comparison> <y>”, where <x> is the first number, <y> is the second number and <comparison> is one of “equal to”, “greater than” or “less than”. If the two numbers are equal, the program should have an exit status of zero. The exit...

  • 8. (4 pts + 2Xc) Write a bash shell script called 08-numMajors that will do the following i. Read data from a class enrollment file that will be specified on the command line ii. If the file does no...

    8. (4 pts + 2Xc) Write a bash shell script called 08-numMajors that will do the following i. Read data from a class enrollment file that will be specified on the command line ii. If the file does not exist, is a directory, or there are more or less than one parameters provided, display an appropriate error/usage message and exit gracefully Display the number of a specified major who are taking a given class iii. The following is a sample...

  • Write a shell script that given a radius, calculates the area of a circle. (Hint –...

    Write a shell script that given a radius, calculates the area of a circle. (Hint – don’t write this from scratch. Find another script that reads user input and performs a calculation, then modify it to meet your needs.) Save the script in your home directory with the name circle.sh The formula for calculating the area is: Area=Π*Radius2 You can use 3.14 for Π, and either do Radius*Radius or Radius^2 to get the square of the radius. Use the scale...

  • Objective : Write a C Shell script which copies all files(*.java and *.class) from your home dire...

    Objective : Write a C Shell script which copies all files(*.java and *.class) from your home directory to a new one, and Analyze the new directory information such as number of files, user permissions, and disk usage. Sample Output:                                                    << CS Directory Analysis >>      Date:   ============================================================ Current Directory: /home/tomss New Directory Created : /home/tomss/pgm01 File information Total Number of files : 22 files Directory files:   0 files Plain text files:   10 files File have read permissions: 3 files File have...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT