Validation using While and IF










0















I am learning Shell scripting on own. I am trying to do an assignment to get details from the user like username their individual marks ,DOB and send a report in mail with the Details calculated like total and average.



 validate_marks() 
local Value=$1
if [ "$Value" -ge "0" ] && [ "$Value" -le "100" ]
then
return 0
else
echo "Enter numbers between 0-100"
exit 1.
fi


echo "Enter Marks for each subject:"
read -p "Enter English Marks:" ENG
validate_marks $ENG
read -p "Enter Maths Marks:" MATHS
validate_marks $MATHS
read -p "Enter Science Marks:" SCI
validate_marks $SCI
read -p "Enter History Marks:" HIST
validate_marks $HIST
read -p "Enter your Email id:" EMAIL
validate_email $EMAIL


In the validate marks function I am checking if the value entered is between 0 -100 if it is more return error and go back to prompt until it is correct. How do I achieve this.










share|improve this question

















  • 1





    First thing to learn, indentation will help a lot with readability of code! Then run your code through shellcheck.net it will point out some issues. Finally when posting a question here, we expect you to put what the problem is, any error logs, debugging steps you have already taken, ... Your validate_marks function works, but there is a syntax error in it (see the site I mentioned).

    – Nic3500
    Nov 13 '18 at 20:57











  • Also see How to use Shellcheck, How to debug a bash script? (U&L.SE), How to debug a bash script? (SO), How to debug bash script? (AskU), Debugging Bash scripts, etc.

    – jww
    Nov 14 '18 at 23:36















0















I am learning Shell scripting on own. I am trying to do an assignment to get details from the user like username their individual marks ,DOB and send a report in mail with the Details calculated like total and average.



 validate_marks() 
local Value=$1
if [ "$Value" -ge "0" ] && [ "$Value" -le "100" ]
then
return 0
else
echo "Enter numbers between 0-100"
exit 1.
fi


echo "Enter Marks for each subject:"
read -p "Enter English Marks:" ENG
validate_marks $ENG
read -p "Enter Maths Marks:" MATHS
validate_marks $MATHS
read -p "Enter Science Marks:" SCI
validate_marks $SCI
read -p "Enter History Marks:" HIST
validate_marks $HIST
read -p "Enter your Email id:" EMAIL
validate_email $EMAIL


In the validate marks function I am checking if the value entered is between 0 -100 if it is more return error and go back to prompt until it is correct. How do I achieve this.










share|improve this question

















  • 1





    First thing to learn, indentation will help a lot with readability of code! Then run your code through shellcheck.net it will point out some issues. Finally when posting a question here, we expect you to put what the problem is, any error logs, debugging steps you have already taken, ... Your validate_marks function works, but there is a syntax error in it (see the site I mentioned).

    – Nic3500
    Nov 13 '18 at 20:57











  • Also see How to use Shellcheck, How to debug a bash script? (U&L.SE), How to debug a bash script? (SO), How to debug bash script? (AskU), Debugging Bash scripts, etc.

    – jww
    Nov 14 '18 at 23:36













0












0








0








I am learning Shell scripting on own. I am trying to do an assignment to get details from the user like username their individual marks ,DOB and send a report in mail with the Details calculated like total and average.



 validate_marks() 
local Value=$1
if [ "$Value" -ge "0" ] && [ "$Value" -le "100" ]
then
return 0
else
echo "Enter numbers between 0-100"
exit 1.
fi


echo "Enter Marks for each subject:"
read -p "Enter English Marks:" ENG
validate_marks $ENG
read -p "Enter Maths Marks:" MATHS
validate_marks $MATHS
read -p "Enter Science Marks:" SCI
validate_marks $SCI
read -p "Enter History Marks:" HIST
validate_marks $HIST
read -p "Enter your Email id:" EMAIL
validate_email $EMAIL


In the validate marks function I am checking if the value entered is between 0 -100 if it is more return error and go back to prompt until it is correct. How do I achieve this.










share|improve this question














I am learning Shell scripting on own. I am trying to do an assignment to get details from the user like username their individual marks ,DOB and send a report in mail with the Details calculated like total and average.



 validate_marks() 
local Value=$1
if [ "$Value" -ge "0" ] && [ "$Value" -le "100" ]
then
return 0
else
echo "Enter numbers between 0-100"
exit 1.
fi


echo "Enter Marks for each subject:"
read -p "Enter English Marks:" ENG
validate_marks $ENG
read -p "Enter Maths Marks:" MATHS
validate_marks $MATHS
read -p "Enter Science Marks:" SCI
validate_marks $SCI
read -p "Enter History Marks:" HIST
validate_marks $HIST
read -p "Enter your Email id:" EMAIL
validate_email $EMAIL


In the validate marks function I am checking if the value entered is between 0 -100 if it is more return error and go back to prompt until it is correct. How do I achieve this.







linux bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 20:25









Jayashree Jayashree

31




31







  • 1





    First thing to learn, indentation will help a lot with readability of code! Then run your code through shellcheck.net it will point out some issues. Finally when posting a question here, we expect you to put what the problem is, any error logs, debugging steps you have already taken, ... Your validate_marks function works, but there is a syntax error in it (see the site I mentioned).

    – Nic3500
    Nov 13 '18 at 20:57











  • Also see How to use Shellcheck, How to debug a bash script? (U&L.SE), How to debug a bash script? (SO), How to debug bash script? (AskU), Debugging Bash scripts, etc.

    – jww
    Nov 14 '18 at 23:36












  • 1





    First thing to learn, indentation will help a lot with readability of code! Then run your code through shellcheck.net it will point out some issues. Finally when posting a question here, we expect you to put what the problem is, any error logs, debugging steps you have already taken, ... Your validate_marks function works, but there is a syntax error in it (see the site I mentioned).

    – Nic3500
    Nov 13 '18 at 20:57











  • Also see How to use Shellcheck, How to debug a bash script? (U&L.SE), How to debug a bash script? (SO), How to debug bash script? (AskU), Debugging Bash scripts, etc.

    – jww
    Nov 14 '18 at 23:36







1




1





First thing to learn, indentation will help a lot with readability of code! Then run your code through shellcheck.net it will point out some issues. Finally when posting a question here, we expect you to put what the problem is, any error logs, debugging steps you have already taken, ... Your validate_marks function works, but there is a syntax error in it (see the site I mentioned).

– Nic3500
Nov 13 '18 at 20:57





First thing to learn, indentation will help a lot with readability of code! Then run your code through shellcheck.net it will point out some issues. Finally when posting a question here, we expect you to put what the problem is, any error logs, debugging steps you have already taken, ... Your validate_marks function works, but there is a syntax error in it (see the site I mentioned).

– Nic3500
Nov 13 '18 at 20:57













Also see How to use Shellcheck, How to debug a bash script? (U&L.SE), How to debug a bash script? (SO), How to debug bash script? (AskU), Debugging Bash scripts, etc.

– jww
Nov 14 '18 at 23:36





Also see How to use Shellcheck, How to debug a bash script? (U&L.SE), How to debug a bash script? (SO), How to debug bash script? (AskU), Debugging Bash scripts, etc.

– jww
Nov 14 '18 at 23:36












1 Answer
1






active

oldest

votes


















0














How about modifying the function to return the validated score. Then you can enclose the looping until the valid number is given within the function.

Then the code will look like:



#!/bin/bash

function validate_marks()
local subject=$1
local score
while true; do
read -p "Enter $subject:" score
if [[ "$score" =~ ^[0-9]+$ ]] && [[ "$score" -ge 0 ]] && [[ "$score" -le 100 ]]; then
echo "$score"
return
else
echo "** Enter numbers between 0-100 **" >&2
fi
done


echo "Enter Marks for each subject:"

ENG=$(validate_marks "English Marks")
MATHS=$(validate_marks "Maths Marks")
SCI=$(validate_marks "Science Marks")
HIST=$(validate_marks "History Marks")
EMAIL=$(validate_marks "your Email id")



  • while true creates an infinite loop to keep on asking for an input until the if condition is met.

  • The "$score" =~ ^[0-9]+$ portion checks if the user input is a number just to avoid an internal error.

  • The output of the bash function is transmitted to the caller via stdout with echo or something like that.

  • Then the message within the function should be sent to stderr by >&2.

Hope this helps.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288961%2fvalidation-using-while-and-if%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    How about modifying the function to return the validated score. Then you can enclose the looping until the valid number is given within the function.

    Then the code will look like:



    #!/bin/bash

    function validate_marks()
    local subject=$1
    local score
    while true; do
    read -p "Enter $subject:" score
    if [[ "$score" =~ ^[0-9]+$ ]] && [[ "$score" -ge 0 ]] && [[ "$score" -le 100 ]]; then
    echo "$score"
    return
    else
    echo "** Enter numbers between 0-100 **" >&2
    fi
    done


    echo "Enter Marks for each subject:"

    ENG=$(validate_marks "English Marks")
    MATHS=$(validate_marks "Maths Marks")
    SCI=$(validate_marks "Science Marks")
    HIST=$(validate_marks "History Marks")
    EMAIL=$(validate_marks "your Email id")



    • while true creates an infinite loop to keep on asking for an input until the if condition is met.

    • The "$score" =~ ^[0-9]+$ portion checks if the user input is a number just to avoid an internal error.

    • The output of the bash function is transmitted to the caller via stdout with echo or something like that.

    • Then the message within the function should be sent to stderr by >&2.

    Hope this helps.






    share|improve this answer



























      0














      How about modifying the function to return the validated score. Then you can enclose the looping until the valid number is given within the function.

      Then the code will look like:



      #!/bin/bash

      function validate_marks()
      local subject=$1
      local score
      while true; do
      read -p "Enter $subject:" score
      if [[ "$score" =~ ^[0-9]+$ ]] && [[ "$score" -ge 0 ]] && [[ "$score" -le 100 ]]; then
      echo "$score"
      return
      else
      echo "** Enter numbers between 0-100 **" >&2
      fi
      done


      echo "Enter Marks for each subject:"

      ENG=$(validate_marks "English Marks")
      MATHS=$(validate_marks "Maths Marks")
      SCI=$(validate_marks "Science Marks")
      HIST=$(validate_marks "History Marks")
      EMAIL=$(validate_marks "your Email id")



      • while true creates an infinite loop to keep on asking for an input until the if condition is met.

      • The "$score" =~ ^[0-9]+$ portion checks if the user input is a number just to avoid an internal error.

      • The output of the bash function is transmitted to the caller via stdout with echo or something like that.

      • Then the message within the function should be sent to stderr by >&2.

      Hope this helps.






      share|improve this answer

























        0












        0








        0







        How about modifying the function to return the validated score. Then you can enclose the looping until the valid number is given within the function.

        Then the code will look like:



        #!/bin/bash

        function validate_marks()
        local subject=$1
        local score
        while true; do
        read -p "Enter $subject:" score
        if [[ "$score" =~ ^[0-9]+$ ]] && [[ "$score" -ge 0 ]] && [[ "$score" -le 100 ]]; then
        echo "$score"
        return
        else
        echo "** Enter numbers between 0-100 **" >&2
        fi
        done


        echo "Enter Marks for each subject:"

        ENG=$(validate_marks "English Marks")
        MATHS=$(validate_marks "Maths Marks")
        SCI=$(validate_marks "Science Marks")
        HIST=$(validate_marks "History Marks")
        EMAIL=$(validate_marks "your Email id")



        • while true creates an infinite loop to keep on asking for an input until the if condition is met.

        • The "$score" =~ ^[0-9]+$ portion checks if the user input is a number just to avoid an internal error.

        • The output of the bash function is transmitted to the caller via stdout with echo or something like that.

        • Then the message within the function should be sent to stderr by >&2.

        Hope this helps.






        share|improve this answer













        How about modifying the function to return the validated score. Then you can enclose the looping until the valid number is given within the function.

        Then the code will look like:



        #!/bin/bash

        function validate_marks()
        local subject=$1
        local score
        while true; do
        read -p "Enter $subject:" score
        if [[ "$score" =~ ^[0-9]+$ ]] && [[ "$score" -ge 0 ]] && [[ "$score" -le 100 ]]; then
        echo "$score"
        return
        else
        echo "** Enter numbers between 0-100 **" >&2
        fi
        done


        echo "Enter Marks for each subject:"

        ENG=$(validate_marks "English Marks")
        MATHS=$(validate_marks "Maths Marks")
        SCI=$(validate_marks "Science Marks")
        HIST=$(validate_marks "History Marks")
        EMAIL=$(validate_marks "your Email id")



        • while true creates an infinite loop to keep on asking for an input until the if condition is met.

        • The "$score" =~ ^[0-9]+$ portion checks if the user input is a number just to avoid an internal error.

        • The output of the bash function is transmitted to the caller via stdout with echo or something like that.

        • Then the message within the function should be sent to stderr by >&2.

        Hope this helps.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 1:17









        tshionotshiono

        2,099234




        2,099234



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288961%2fvalidation-using-while-and-if%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            How to read a connectionString WITH PROVIDER in .NET Core?

            In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

            Museum of Modern and Contemporary Art of Trento and Rovereto