Shell script-How to return maximum value in array?












1















I have a array:



ARRAY=(12.5 6.2)


I wish to return the maximum value in ARRAY which Output is 12.5



Anyone can share me ideas?



I have try this:



max=0
for v in ${ARRAY[@]}; do
if (( $v > $max )); then max=$v; fi;
done
echo $max


But it return me:



((: 12.5 > 0 : syntax error: invalid arithmetic operator (error token is ".5 > 0 ")
((: 6.2 > 0 : syntax error: invalid arithmetic operator (error token is ".2 > 0 ")









share|improve this question







New contributor




Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    1















    I have a array:



    ARRAY=(12.5 6.2)


    I wish to return the maximum value in ARRAY which Output is 12.5



    Anyone can share me ideas?



    I have try this:



    max=0
    for v in ${ARRAY[@]}; do
    if (( $v > $max )); then max=$v; fi;
    done
    echo $max


    But it return me:



    ((: 12.5 > 0 : syntax error: invalid arithmetic operator (error token is ".5 > 0 ")
    ((: 6.2 > 0 : syntax error: invalid arithmetic operator (error token is ".2 > 0 ")









    share|improve this question







    New contributor




    Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      1












      1








      1








      I have a array:



      ARRAY=(12.5 6.2)


      I wish to return the maximum value in ARRAY which Output is 12.5



      Anyone can share me ideas?



      I have try this:



      max=0
      for v in ${ARRAY[@]}; do
      if (( $v > $max )); then max=$v; fi;
      done
      echo $max


      But it return me:



      ((: 12.5 > 0 : syntax error: invalid arithmetic operator (error token is ".5 > 0 ")
      ((: 6.2 > 0 : syntax error: invalid arithmetic operator (error token is ".2 > 0 ")









      share|improve this question







      New contributor




      Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I have a array:



      ARRAY=(12.5 6.2)


      I wish to return the maximum value in ARRAY which Output is 12.5



      Anyone can share me ideas?



      I have try this:



      max=0
      for v in ${ARRAY[@]}; do
      if (( $v > $max )); then max=$v; fi;
      done
      echo $max


      But it return me:



      ((: 12.5 > 0 : syntax error: invalid arithmetic operator (error token is ".5 > 0 ")
      ((: 6.2 > 0 : syntax error: invalid arithmetic operator (error token is ".2 > 0 ")






      shell-script array bash-array






      share|improve this question







      New contributor




      Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 14 hours ago









      Shi Jie TioShi Jie Tio

      1343




      1343




      New contributor




      Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Shi Jie Tio is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes


















          2














          printf '%sn' "${ARRAY[@]}" |
          awk '$1 > m || NR == 1 { m = $1 } END { print m }'


          Since the bash shell does not do floating point arithmetics, it's easier to compare floating point numbers in another language. Here I'm using awk to find the maximum of all the elements in the ARRAY array.



          The printf command will output each element of the array on its own line and the awk code will update its m value to be the maximum of the values seen so far. At the end, the m value is printed.



          The test on NR == 1 will be true on for the first line read by the awk program and would initialise the value of m to the first value of the array (something that you fail to do, which means that your code would have returned 0 for an array with all negative numbers, had it worked).






          share|improve this answer


























          • Possible alternative to || NR == 1: set m to array's first element using -v m="${ARRAY}"

            – Olorin
            14 hours ago













          • Can your function here store in a variable?

            – Shi Jie Tio
            14 hours ago











          • @ShiJieTio I'm using no function, but the result may be stored in a variable using a standard command substitution as usual: max=$( printf ... | awk ... ).

            – Kusalananda
            14 hours ago













          • for example: Result= "%sn' "${ARRAY[@]}" |awk '$1 > m || m == "" { m = $1 } END { print m }"

            – Shi Jie Tio
            13 hours ago











          • @ShiJieTio No. You left out the printf and the $( ... ) bit.

            – Kusalananda
            13 hours ago





















          0














          If you want to do floating point arithmetics, you'd need to switch to ksh93, zsh or yash (or fish if you're ready to wander away from Bourne-like shells), bash doesn't support them.



          For a syntax compatible to all 3 shells:



          max() {
          [ "$#" -gt 0 ] || return
          typeset i max="$1"; shift
          for i do
          if [ "$((i > max))" -ne 0 ]; then
          max=$i
          fi
          done
          printf '%sn' "$max"
          }
          array=(12.5 6.2 nan 0xfff -inf inf -1e12 1e20)
          max "${array[@]}"


          (you need a recent version of zsh for inf/nan to be supported. Variations in case are supported in all 3 (INF, NaN, Inf...), but only yash supports alternative spellings like Infinity; 0x12p34 types of numbers are not supported by zsh yet. Beware that whether 010 means 10 or 8 depends on the shell and its configuration)






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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
            });


            }
            });






            Shi Jie Tio is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f494977%2fshell-script-how-to-return-maximum-value-in-array%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            printf '%sn' "${ARRAY[@]}" |
            awk '$1 > m || NR == 1 { m = $1 } END { print m }'


            Since the bash shell does not do floating point arithmetics, it's easier to compare floating point numbers in another language. Here I'm using awk to find the maximum of all the elements in the ARRAY array.



            The printf command will output each element of the array on its own line and the awk code will update its m value to be the maximum of the values seen so far. At the end, the m value is printed.



            The test on NR == 1 will be true on for the first line read by the awk program and would initialise the value of m to the first value of the array (something that you fail to do, which means that your code would have returned 0 for an array with all negative numbers, had it worked).






            share|improve this answer


























            • Possible alternative to || NR == 1: set m to array's first element using -v m="${ARRAY}"

              – Olorin
              14 hours ago













            • Can your function here store in a variable?

              – Shi Jie Tio
              14 hours ago











            • @ShiJieTio I'm using no function, but the result may be stored in a variable using a standard command substitution as usual: max=$( printf ... | awk ... ).

              – Kusalananda
              14 hours ago













            • for example: Result= "%sn' "${ARRAY[@]}" |awk '$1 > m || m == "" { m = $1 } END { print m }"

              – Shi Jie Tio
              13 hours ago











            • @ShiJieTio No. You left out the printf and the $( ... ) bit.

              – Kusalananda
              13 hours ago


















            2














            printf '%sn' "${ARRAY[@]}" |
            awk '$1 > m || NR == 1 { m = $1 } END { print m }'


            Since the bash shell does not do floating point arithmetics, it's easier to compare floating point numbers in another language. Here I'm using awk to find the maximum of all the elements in the ARRAY array.



            The printf command will output each element of the array on its own line and the awk code will update its m value to be the maximum of the values seen so far. At the end, the m value is printed.



            The test on NR == 1 will be true on for the first line read by the awk program and would initialise the value of m to the first value of the array (something that you fail to do, which means that your code would have returned 0 for an array with all negative numbers, had it worked).






            share|improve this answer


























            • Possible alternative to || NR == 1: set m to array's first element using -v m="${ARRAY}"

              – Olorin
              14 hours ago













            • Can your function here store in a variable?

              – Shi Jie Tio
              14 hours ago











            • @ShiJieTio I'm using no function, but the result may be stored in a variable using a standard command substitution as usual: max=$( printf ... | awk ... ).

              – Kusalananda
              14 hours ago













            • for example: Result= "%sn' "${ARRAY[@]}" |awk '$1 > m || m == "" { m = $1 } END { print m }"

              – Shi Jie Tio
              13 hours ago











            • @ShiJieTio No. You left out the printf and the $( ... ) bit.

              – Kusalananda
              13 hours ago
















            2












            2








            2







            printf '%sn' "${ARRAY[@]}" |
            awk '$1 > m || NR == 1 { m = $1 } END { print m }'


            Since the bash shell does not do floating point arithmetics, it's easier to compare floating point numbers in another language. Here I'm using awk to find the maximum of all the elements in the ARRAY array.



            The printf command will output each element of the array on its own line and the awk code will update its m value to be the maximum of the values seen so far. At the end, the m value is printed.



            The test on NR == 1 will be true on for the first line read by the awk program and would initialise the value of m to the first value of the array (something that you fail to do, which means that your code would have returned 0 for an array with all negative numbers, had it worked).






            share|improve this answer















            printf '%sn' "${ARRAY[@]}" |
            awk '$1 > m || NR == 1 { m = $1 } END { print m }'


            Since the bash shell does not do floating point arithmetics, it's easier to compare floating point numbers in another language. Here I'm using awk to find the maximum of all the elements in the ARRAY array.



            The printf command will output each element of the array on its own line and the awk code will update its m value to be the maximum of the values seen so far. At the end, the m value is printed.



            The test on NR == 1 will be true on for the first line read by the awk program and would initialise the value of m to the first value of the array (something that you fail to do, which means that your code would have returned 0 for an array with all negative numbers, had it worked).







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 13 hours ago

























            answered 14 hours ago









            KusalanandaKusalananda

            124k16234386




            124k16234386













            • Possible alternative to || NR == 1: set m to array's first element using -v m="${ARRAY}"

              – Olorin
              14 hours ago













            • Can your function here store in a variable?

              – Shi Jie Tio
              14 hours ago











            • @ShiJieTio I'm using no function, but the result may be stored in a variable using a standard command substitution as usual: max=$( printf ... | awk ... ).

              – Kusalananda
              14 hours ago













            • for example: Result= "%sn' "${ARRAY[@]}" |awk '$1 > m || m == "" { m = $1 } END { print m }"

              – Shi Jie Tio
              13 hours ago











            • @ShiJieTio No. You left out the printf and the $( ... ) bit.

              – Kusalananda
              13 hours ago





















            • Possible alternative to || NR == 1: set m to array's first element using -v m="${ARRAY}"

              – Olorin
              14 hours ago













            • Can your function here store in a variable?

              – Shi Jie Tio
              14 hours ago











            • @ShiJieTio I'm using no function, but the result may be stored in a variable using a standard command substitution as usual: max=$( printf ... | awk ... ).

              – Kusalananda
              14 hours ago













            • for example: Result= "%sn' "${ARRAY[@]}" |awk '$1 > m || m == "" { m = $1 } END { print m }"

              – Shi Jie Tio
              13 hours ago











            • @ShiJieTio No. You left out the printf and the $( ... ) bit.

              – Kusalananda
              13 hours ago



















            Possible alternative to || NR == 1: set m to array's first element using -v m="${ARRAY}"

            – Olorin
            14 hours ago







            Possible alternative to || NR == 1: set m to array's first element using -v m="${ARRAY}"

            – Olorin
            14 hours ago















            Can your function here store in a variable?

            – Shi Jie Tio
            14 hours ago





            Can your function here store in a variable?

            – Shi Jie Tio
            14 hours ago













            @ShiJieTio I'm using no function, but the result may be stored in a variable using a standard command substitution as usual: max=$( printf ... | awk ... ).

            – Kusalananda
            14 hours ago







            @ShiJieTio I'm using no function, but the result may be stored in a variable using a standard command substitution as usual: max=$( printf ... | awk ... ).

            – Kusalananda
            14 hours ago















            for example: Result= "%sn' "${ARRAY[@]}" |awk '$1 > m || m == "" { m = $1 } END { print m }"

            – Shi Jie Tio
            13 hours ago





            for example: Result= "%sn' "${ARRAY[@]}" |awk '$1 > m || m == "" { m = $1 } END { print m }"

            – Shi Jie Tio
            13 hours ago













            @ShiJieTio No. You left out the printf and the $( ... ) bit.

            – Kusalananda
            13 hours ago







            @ShiJieTio No. You left out the printf and the $( ... ) bit.

            – Kusalananda
            13 hours ago















            0














            If you want to do floating point arithmetics, you'd need to switch to ksh93, zsh or yash (or fish if you're ready to wander away from Bourne-like shells), bash doesn't support them.



            For a syntax compatible to all 3 shells:



            max() {
            [ "$#" -gt 0 ] || return
            typeset i max="$1"; shift
            for i do
            if [ "$((i > max))" -ne 0 ]; then
            max=$i
            fi
            done
            printf '%sn' "$max"
            }
            array=(12.5 6.2 nan 0xfff -inf inf -1e12 1e20)
            max "${array[@]}"


            (you need a recent version of zsh for inf/nan to be supported. Variations in case are supported in all 3 (INF, NaN, Inf...), but only yash supports alternative spellings like Infinity; 0x12p34 types of numbers are not supported by zsh yet. Beware that whether 010 means 10 or 8 depends on the shell and its configuration)






            share|improve this answer




























              0














              If you want to do floating point arithmetics, you'd need to switch to ksh93, zsh or yash (or fish if you're ready to wander away from Bourne-like shells), bash doesn't support them.



              For a syntax compatible to all 3 shells:



              max() {
              [ "$#" -gt 0 ] || return
              typeset i max="$1"; shift
              for i do
              if [ "$((i > max))" -ne 0 ]; then
              max=$i
              fi
              done
              printf '%sn' "$max"
              }
              array=(12.5 6.2 nan 0xfff -inf inf -1e12 1e20)
              max "${array[@]}"


              (you need a recent version of zsh for inf/nan to be supported. Variations in case are supported in all 3 (INF, NaN, Inf...), but only yash supports alternative spellings like Infinity; 0x12p34 types of numbers are not supported by zsh yet. Beware that whether 010 means 10 or 8 depends on the shell and its configuration)






              share|improve this answer


























                0












                0








                0







                If you want to do floating point arithmetics, you'd need to switch to ksh93, zsh or yash (or fish if you're ready to wander away from Bourne-like shells), bash doesn't support them.



                For a syntax compatible to all 3 shells:



                max() {
                [ "$#" -gt 0 ] || return
                typeset i max="$1"; shift
                for i do
                if [ "$((i > max))" -ne 0 ]; then
                max=$i
                fi
                done
                printf '%sn' "$max"
                }
                array=(12.5 6.2 nan 0xfff -inf inf -1e12 1e20)
                max "${array[@]}"


                (you need a recent version of zsh for inf/nan to be supported. Variations in case are supported in all 3 (INF, NaN, Inf...), but only yash supports alternative spellings like Infinity; 0x12p34 types of numbers are not supported by zsh yet. Beware that whether 010 means 10 or 8 depends on the shell and its configuration)






                share|improve this answer













                If you want to do floating point arithmetics, you'd need to switch to ksh93, zsh or yash (or fish if you're ready to wander away from Bourne-like shells), bash doesn't support them.



                For a syntax compatible to all 3 shells:



                max() {
                [ "$#" -gt 0 ] || return
                typeset i max="$1"; shift
                for i do
                if [ "$((i > max))" -ne 0 ]; then
                max=$i
                fi
                done
                printf '%sn' "$max"
                }
                array=(12.5 6.2 nan 0xfff -inf inf -1e12 1e20)
                max "${array[@]}"


                (you need a recent version of zsh for inf/nan to be supported. Variations in case are supported in all 3 (INF, NaN, Inf...), but only yash supports alternative spellings like Infinity; 0x12p34 types of numbers are not supported by zsh yet. Beware that whether 010 means 10 or 8 depends on the shell and its configuration)







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 13 hours ago









                Stéphane ChazelasStéphane Chazelas

                301k55564916




                301k55564916






















                    Shi Jie Tio is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    Shi Jie Tio is a new contributor. Be nice, and check out our Code of Conduct.













                    Shi Jie Tio is a new contributor. Be nice, and check out our Code of Conduct.












                    Shi Jie Tio is a new contributor. Be nice, and check out our Code of Conduct.
















                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • 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%2funix.stackexchange.com%2fquestions%2f494977%2fshell-script-how-to-return-maximum-value-in-array%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







                    Popular posts from this blog

                    Histoire des bourses de valeurs

                    Why is there Russian traffic in my log files?

                    Rename multiple files to decrement number in file name?