Results 1 to 4 of 4

Thread: User input and calling functions in bash

  1. #1
    Join Date
    Feb 2023
    Beans
    5

    Question User input and calling functions in bash

    Hi again everyone.

    I'm getting better at bash shell script, but I'm still a beginner, so I have another question.

    I'm trying to prompt the user to enter their grades so I can run these two functions that will average the grades and give them a letter grade. This is currently the code I have right now:
    Code:
    #!/bin/bash
    
    #First, define calcAverage
    calcAverage() {
               sum=0
               for score in "$1"
               do
                    sum=$((sum + score))
               done
               average=$((sum /$#))
               echo "$average"
    }
    
    #Then, define determineGrade
    determineGrade() {
       score=$1
       if (( score >= 90 && score <= 100)); then
       grade="A"
       elif (( score >= 80 && score <= 89)); then
       grade="B"
       elif (( score >= 70 && score <= 79)); then
       grade="C"
       elif (( score >= 60 && score <= 69)); then
       grade="D"
       else
         grade="F"
         fi
         echo "$grade"
    }
    To be specific, I'm trying to get the user to input five grades to average. I'm not really sure where to go from here, though... I know how to ask for a user input, but while calling the functions? I'm confused, especially because the prompt in general confused me.
    So I'm asking, where do I go from here? How do I call the functions while using the user's input?

    Last edited by coffeecat; March 24th, 2023 at 08:56 AM. Reason: added code tags

  2. #2
    Join Date
    Feb 2023
    Beans
    5

    Re: User input and calling functions in bash



    Sorry the code came out weird on text. I tried to submit a screenshot but it won't work so. I can try posting it on imgur or something?
    Last edited by lefleur; March 24th, 2023 at 06:13 AM.

  3. #3
    Join Date
    Jun 2006
    Location
    UK
    Beans
    Hidden!
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: User input and calling functions in bash

    Quote Originally Posted by lefleur View Post
    Sorry the code came out weird on text.
    That's because you did not enclose your code in BBCode code tags. The forum software strips out adjacent spaces on display if in the main body of the text. If put between code tags all spaces are displayed and code appears as it should. Use the # icon in the advanced editor to add code tags. A link to more detailed instructions on using BBCode code tags in my sig.

    I've added code tags to your post #1. See how that restores/preserves columns in code.

    Quote Originally Posted by lefleur View Post
    I tried to submit a screenshot but it won't work so.
    That's because you dragged and dropped your image into the message editor. That will never work. All it does is to clog your post up with masses of base64 code which is not displayed.

    Quote Originally Posted by lefleur View Post
    I can try posting it on imgur or something?
    Please do not do that. If you need to include an image in your post, simply use the paperclip icon in the advanced message editor toolbar to upload images to your post. That way you get clickable thumbnails. However if you are trying to display text, please do not post screenshots. So long as you use code tags for code, terminal output, and the like, you should post text as text. It is easier to read and forum members helping can quote it if need be.
    Last edited by coffeecat; March 24th, 2023 at 10:17 AM.
    Ubuntu 22.04 Desktop Guide - Ubuntu 24.04 Desktop Guide - Forum Guide to BBCode - Using BBCode code tags

    Member: Not Canonical Team

    If you need help with your forum account, such as SSO login issues, username changes, etc, the correct place to contact an admin is here. Please do not PM me about these matters unless you have been asked to - unsolicited PMs concerning forum accounts will be ignored.

  4. #4
    Join Date
    Jun 2006
    Location
    UK
    Beans
    Hidden!
    Distro
    Ubuntu 22.04 Jammy Jellyfish

    Re: User input and calling functions in bash

    And another thing...

    Is this another assignment question? I'll repeat the quote from our rules that I quoted in your earlier thread:

    While we are happy to serve as a resource for hints and for asking questions when you get stuck and need a little help, the Ubuntu Forums is not a homework service. Please do not post your homework assignments expecting someone else to do it for you.
    This is both for you and for those helping.
    Ubuntu 22.04 Desktop Guide - Ubuntu 24.04 Desktop Guide - Forum Guide to BBCode - Using BBCode code tags

    Member: Not Canonical Team

    If you need help with your forum account, such as SSO login issues, username changes, etc, the correct place to contact an admin is here. Please do not PM me about these matters unless you have been asked to - unsolicited PMs concerning forum accounts will be ignored.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •