How to test if array elements are all equal in bash?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
13
down vote

favorite
3












The following array represented the numbers of disks on each linux machines



Each single array includes the number of disks on a linux machine.



echo $ARRAY_DISK_Quantity[*]
4 4 4 4 2 4 4 4


what is the simple way to identify that all array's values are equal?



Good status:



4 4 4 4 4 4 4 4


Bad status:



4 4 4 4 4 4 2 4


Bad status:



6 6 6 6 6 6 6 6 6 6 2 6 2






share|improve this question






















  • So many answers and no votes?
    – Jesse_b
    Dec 25 '17 at 11:59










  • Will this only be testing integers or should it also test strings?
    – Jesse_b
    Dec 25 '17 at 12:01










  • I just waiting for the best answer dont worry soon I will vote
    – yael
    Dec 25 '17 at 12:02










  • I meant everyone else. This question deserves upvote IMO.
    – Jesse_b
    Dec 25 '17 at 12:03










  • once you need something of at least this level of complexity, it's a good time to start using a real programming language, until it's too late…
    – Sarge Borsch
    Dec 26 '17 at 17:35














up vote
13
down vote

favorite
3












The following array represented the numbers of disks on each linux machines



Each single array includes the number of disks on a linux machine.



echo $ARRAY_DISK_Quantity[*]
4 4 4 4 2 4 4 4


what is the simple way to identify that all array's values are equal?



Good status:



4 4 4 4 4 4 4 4


Bad status:



4 4 4 4 4 4 2 4


Bad status:



6 6 6 6 6 6 6 6 6 6 2 6 2






share|improve this question






















  • So many answers and no votes?
    – Jesse_b
    Dec 25 '17 at 11:59










  • Will this only be testing integers or should it also test strings?
    – Jesse_b
    Dec 25 '17 at 12:01










  • I just waiting for the best answer dont worry soon I will vote
    – yael
    Dec 25 '17 at 12:02










  • I meant everyone else. This question deserves upvote IMO.
    – Jesse_b
    Dec 25 '17 at 12:03










  • once you need something of at least this level of complexity, it's a good time to start using a real programming language, until it's too late…
    – Sarge Borsch
    Dec 26 '17 at 17:35












up vote
13
down vote

favorite
3









up vote
13
down vote

favorite
3






3





The following array represented the numbers of disks on each linux machines



Each single array includes the number of disks on a linux machine.



echo $ARRAY_DISK_Quantity[*]
4 4 4 4 2 4 4 4


what is the simple way to identify that all array's values are equal?



Good status:



4 4 4 4 4 4 4 4


Bad status:



4 4 4 4 4 4 2 4


Bad status:



6 6 6 6 6 6 6 6 6 6 2 6 2






share|improve this question














The following array represented the numbers of disks on each linux machines



Each single array includes the number of disks on a linux machine.



echo $ARRAY_DISK_Quantity[*]
4 4 4 4 2 4 4 4


what is the simple way to identify that all array's values are equal?



Good status:



4 4 4 4 4 4 4 4


Bad status:



4 4 4 4 4 4 2 4


Bad status:



6 6 6 6 6 6 6 6 6 6 2 6 2








share|improve this question













share|improve this question




share|improve this question








edited Dec 25 '17 at 13:30









Jesse_b

10.5k22659




10.5k22659










asked Dec 25 '17 at 10:28









yael

2,0091145




2,0091145











  • So many answers and no votes?
    – Jesse_b
    Dec 25 '17 at 11:59










  • Will this only be testing integers or should it also test strings?
    – Jesse_b
    Dec 25 '17 at 12:01










  • I just waiting for the best answer dont worry soon I will vote
    – yael
    Dec 25 '17 at 12:02










  • I meant everyone else. This question deserves upvote IMO.
    – Jesse_b
    Dec 25 '17 at 12:03










  • once you need something of at least this level of complexity, it's a good time to start using a real programming language, until it's too late…
    – Sarge Borsch
    Dec 26 '17 at 17:35
















  • So many answers and no votes?
    – Jesse_b
    Dec 25 '17 at 11:59










  • Will this only be testing integers or should it also test strings?
    – Jesse_b
    Dec 25 '17 at 12:01










  • I just waiting for the best answer dont worry soon I will vote
    – yael
    Dec 25 '17 at 12:02










  • I meant everyone else. This question deserves upvote IMO.
    – Jesse_b
    Dec 25 '17 at 12:03










  • once you need something of at least this level of complexity, it's a good time to start using a real programming language, until it's too late…
    – Sarge Borsch
    Dec 26 '17 at 17:35















So many answers and no votes?
– Jesse_b
Dec 25 '17 at 11:59




So many answers and no votes?
– Jesse_b
Dec 25 '17 at 11:59












Will this only be testing integers or should it also test strings?
– Jesse_b
Dec 25 '17 at 12:01




Will this only be testing integers or should it also test strings?
– Jesse_b
Dec 25 '17 at 12:01












I just waiting for the best answer dont worry soon I will vote
– yael
Dec 25 '17 at 12:02




I just waiting for the best answer dont worry soon I will vote
– yael
Dec 25 '17 at 12:02












I meant everyone else. This question deserves upvote IMO.
– Jesse_b
Dec 25 '17 at 12:03




I meant everyone else. This question deserves upvote IMO.
– Jesse_b
Dec 25 '17 at 12:03












once you need something of at least this level of complexity, it's a good time to start using a real programming language, until it's too late…
– Sarge Borsch
Dec 26 '17 at 17:35




once you need something of at least this level of complexity, it's a good time to start using a real programming language, until it's too late…
– Sarge Borsch
Dec 26 '17 at 17:35










7 Answers
7






active

oldest

votes

















up vote
9
down vote



accepted










bash + GNU sort + GNU grep solution:



if [ "$#array[@]" -gt 0 ] && [ $(printf "%s00" "$array[@]" | 
LC_ALL=C sort -z -u |
grep -z -c .) -eq 1 ] ; then
echo ok
else
echo bad
fi


English explanation: if unique-sorting the elements of the array results in only one element, then print "ok". Otherwise print "bad".



The array is printed with NUL bytes separating each element, piped into GNU sort (relying on the -z aka --zero-terminated and -u aka --unique options), and then into grep (using options -z aka --null-data and -c aka --count) to count the output lines.



Unlike my previous version, I can't use wc here because it requires input lines terminated with a newline...and using sed or tr to convert NULs to newlines after the sort would defeat the purpose of using NUL separators. grep -c makes a reasonable substitute.




Here's the same thing rewritten as a function:



function count_unique() 
local LC_ALL=C

if [ "$#" -eq 0 ] ; then
echo 0
else
echo "$(printf "%s00" "$@"



ARRAY_DISK_Quantity=(4 4 4 4 2 4 4 4)

if [ "$(count_unique "$ARRAY_DISK_Quantity[@]")" -eq 1 ] ; then
echo "ok"
else
echo "bad"
fi





share|improve this answer


















  • 1




    Note that sort -u doesn't return unique elements but one of each set of elements that sort the same. For instance, it would say "ok" on ARRAY_DISK_Quantity=(① ②) on a GNU systems where locales typically decide those 2 characters sort the same. You'd want LC_ALL=C sort -u for byte-to-byte uniqueness.
    – Stéphane Chazelas
    Dec 25 '17 at 11:58











  • just another note it will be fail also in case no additional disks are appears from CLI so need also to add this syntax
    – yael
    Dec 25 '17 at 12:22










  • [[ ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | wc -l ` -eq ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | grep -c "0" ` ]] && echo fail
    – yael
    Dec 25 '17 at 12:22










  • @StéphaneChazelas the locale issue is worth dealing with, as is the IFS issue. Testing for an empty list is, IMO, best done separately - there's no need to check for non-unique elements in an empty set.
    – cas
    Dec 25 '17 at 12:53










  • Hi Cas I prefer your previous answer
    – yael
    Dec 25 '17 at 13:55

















up vote
7
down vote













With zsh:



if (($#$(u)ARRAY_DISK_Quantity[@] == 1)); then
echo OK
else
echo not OK
fi


Where (u) is a parameter expansion flag to expand unique values. So we're getting a count of the unique values in the array.



Replace == 1 with <= 1 is you want to consider an empty array is OK.



With ksh93, you could sort the array and check that the first element is the same as the last:



set -s -- "$ARRAY_DISK_Quantity[@]"
if [ "$1" = "$@: -1" ]; then
echo OK
else
echo not OK
fi


With ksh88 or pdksh/mksh:



set -s -- "$ARRAY_DISK_Quantity[@]"
if eval '[ "$1" = "$'"$#"'" ]'; then
echo OK
else
echo not OK
fi


With bash, you'd probably need a loop:



unique_values() return 1
done
return 0

if unique_values "$ARRAY_DISK_Quantity[@]"; then
echo OK
else
echo not OK
fi


(would work with all the Bourne-like shells with array support (ksh, zsh, bash, yash)).



Note that it returns OK for an empty array. Add a [ "$#" -gt 0 ] || return at the start of the function if you don't want that.






share|improve this answer






















  • all these answers not seems to support bash ?
    – yael
    Dec 25 '17 at 12:04










  • @yael, see edit for a bash solution. But why would you use bash?
    – Stéphane Chazelas
    Dec 25 '17 at 12:08










  • In Bash, the help page for typeset says Obsolete. See `help declare'. Is there a reason you're using it instead of local or declare?
    – wjandrea
    Dec 26 '17 at 2:52






  • 1




    @wjandrea typeset is the one that works in all 4 shells. It's also the original one from ksh in the early 80s ( bash mostly copied ksh88 when it comes to variable scoping type setting and declaration but decided to rename typeset declare and make typeset an alias to declare).
    – Stéphane Chazelas
    Dec 26 '17 at 7:32

















up vote
4
down vote













bash + awk soltion:



function get_status() 
arr=("$@") # get the array passed as argument
if awk 'v && $1!=v exit 1 v=$1 ' <(printf "%dn" "$arr[@]"); then
echo "status: Ok"
else
echo "status: Bad"
fi




Test case #1:



ARRAY_DISK_Quantity=(4 4 4 4 4 2 4 4)
get_status "$ARRAY_DISK_Quantity[@]"
status: Bad



Test case #2:



ARRAY_DISK_Quantity=(4 4 4 4 4 4 4 4)
get_status "$ARRAY_DISK_Quantity[@]"
status: Ok





share|improve this answer





























    up vote
    4
    down vote













    I have another bash only solution that should work with strings as well:



    isarray.equal () 
    local placeholder="$1"
    local num=0
    while (( $# )); do
    if [[ "$1" != "$placeholder" ]]; then
    num=1
    echo 'Bad' && break
    fi
    shift
    done
    [[ "$num" -ne 1 ]] && echo 'Okay'



    Demonstration:



    [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(4 4 4 4 2 4 4 4)
    [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
    Bad
    [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(4 4 4 4 4 4 4 4)
    [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
    Okay
    [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(four four four four two four four four)
    [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
    Bad
    [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(four four four four four four four four)
    [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
    Okay





    share|improve this answer






















    • Note that dots are not valid in function names, though Bash is pretty permissive. This may cause issues like in exporting the function.
      – wjandrea
      Dec 26 '17 at 2:55










    • unix.stackexchange.com/a/245953/237982
      – Jesse_b
      Dec 26 '17 at 3:30

















    up vote
    2
    down vote













    With bash and GNU grep:



    if grep -qE '^([0-9]+)( 1)*$' <<< "$ARRAY_DISK_Quantity[@]"; then 
    echo "okay"
    else
    echo "not okay"
    fi





    share|improve this answer






















    • Yes, but what about (10 10 10 10)? Otherwise, quite nice.
      – Joe
      Dec 30 '17 at 6:20










    • @Joe: Good catch. I've updated my answer.
      – Cyrus
      Dec 30 '17 at 6:39

















    up vote
    1
    down vote













    Here is POSIX Awk:



    awk 'BEGIN while (++z < ARGC) if (ARGV[z] != ARGV[1]) exit 1' "$ARRAY_DISK_Quantity[@]"





    share|improve this answer



























      up vote
      0
      down vote













      bash only solution (assuming a is ARRAY_DISK_Quantity)



      ttt=$a[0]
      res=0
      for i in "$a[@]"
      do
      let res+=$(if [ "$ttt" -ne "$i" ]; then echo 1; else echo 0; fi);
      done
      if [ "$res" -eq 0 ]
      then
      echo "ok"
      else
      echo "bad"
      fi





      share|improve this answer




















      • Works, but counts all errors when just one is enough: if [ "$ttt" -ne "$i" ]; then res=1; break; fi;
        – Joe
        Dec 30 '17 at 6:30










      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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412933%2fhow-to-test-if-array-elements-are-all-equal-in-bash%23new-answer', 'question_page');

      );

      Post as a guest






























      7 Answers
      7






      active

      oldest

      votes








      7 Answers
      7






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      9
      down vote



      accepted










      bash + GNU sort + GNU grep solution:



      if [ "$#array[@]" -gt 0 ] && [ $(printf "%s00" "$array[@]" | 
      LC_ALL=C sort -z -u |
      grep -z -c .) -eq 1 ] ; then
      echo ok
      else
      echo bad
      fi


      English explanation: if unique-sorting the elements of the array results in only one element, then print "ok". Otherwise print "bad".



      The array is printed with NUL bytes separating each element, piped into GNU sort (relying on the -z aka --zero-terminated and -u aka --unique options), and then into grep (using options -z aka --null-data and -c aka --count) to count the output lines.



      Unlike my previous version, I can't use wc here because it requires input lines terminated with a newline...and using sed or tr to convert NULs to newlines after the sort would defeat the purpose of using NUL separators. grep -c makes a reasonable substitute.




      Here's the same thing rewritten as a function:



      function count_unique() 
      local LC_ALL=C

      if [ "$#" -eq 0 ] ; then
      echo 0
      else
      echo "$(printf "%s00" "$@"



      ARRAY_DISK_Quantity=(4 4 4 4 2 4 4 4)

      if [ "$(count_unique "$ARRAY_DISK_Quantity[@]")" -eq 1 ] ; then
      echo "ok"
      else
      echo "bad"
      fi





      share|improve this answer


















      • 1




        Note that sort -u doesn't return unique elements but one of each set of elements that sort the same. For instance, it would say "ok" on ARRAY_DISK_Quantity=(① ②) on a GNU systems where locales typically decide those 2 characters sort the same. You'd want LC_ALL=C sort -u for byte-to-byte uniqueness.
        – Stéphane Chazelas
        Dec 25 '17 at 11:58











      • just another note it will be fail also in case no additional disks are appears from CLI so need also to add this syntax
        – yael
        Dec 25 '17 at 12:22










      • [[ ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | wc -l ` -eq ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | grep -c "0" ` ]] && echo fail
        – yael
        Dec 25 '17 at 12:22










      • @StéphaneChazelas the locale issue is worth dealing with, as is the IFS issue. Testing for an empty list is, IMO, best done separately - there's no need to check for non-unique elements in an empty set.
        – cas
        Dec 25 '17 at 12:53










      • Hi Cas I prefer your previous answer
        – yael
        Dec 25 '17 at 13:55














      up vote
      9
      down vote



      accepted










      bash + GNU sort + GNU grep solution:



      if [ "$#array[@]" -gt 0 ] && [ $(printf "%s00" "$array[@]" | 
      LC_ALL=C sort -z -u |
      grep -z -c .) -eq 1 ] ; then
      echo ok
      else
      echo bad
      fi


      English explanation: if unique-sorting the elements of the array results in only one element, then print "ok". Otherwise print "bad".



      The array is printed with NUL bytes separating each element, piped into GNU sort (relying on the -z aka --zero-terminated and -u aka --unique options), and then into grep (using options -z aka --null-data and -c aka --count) to count the output lines.



      Unlike my previous version, I can't use wc here because it requires input lines terminated with a newline...and using sed or tr to convert NULs to newlines after the sort would defeat the purpose of using NUL separators. grep -c makes a reasonable substitute.




      Here's the same thing rewritten as a function:



      function count_unique() 
      local LC_ALL=C

      if [ "$#" -eq 0 ] ; then
      echo 0
      else
      echo "$(printf "%s00" "$@"



      ARRAY_DISK_Quantity=(4 4 4 4 2 4 4 4)

      if [ "$(count_unique "$ARRAY_DISK_Quantity[@]")" -eq 1 ] ; then
      echo "ok"
      else
      echo "bad"
      fi





      share|improve this answer


















      • 1




        Note that sort -u doesn't return unique elements but one of each set of elements that sort the same. For instance, it would say "ok" on ARRAY_DISK_Quantity=(① ②) on a GNU systems where locales typically decide those 2 characters sort the same. You'd want LC_ALL=C sort -u for byte-to-byte uniqueness.
        – Stéphane Chazelas
        Dec 25 '17 at 11:58











      • just another note it will be fail also in case no additional disks are appears from CLI so need also to add this syntax
        – yael
        Dec 25 '17 at 12:22










      • [[ ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | wc -l ` -eq ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | grep -c "0" ` ]] && echo fail
        – yael
        Dec 25 '17 at 12:22










      • @StéphaneChazelas the locale issue is worth dealing with, as is the IFS issue. Testing for an empty list is, IMO, best done separately - there's no need to check for non-unique elements in an empty set.
        – cas
        Dec 25 '17 at 12:53










      • Hi Cas I prefer your previous answer
        – yael
        Dec 25 '17 at 13:55












      up vote
      9
      down vote



      accepted







      up vote
      9
      down vote



      accepted






      bash + GNU sort + GNU grep solution:



      if [ "$#array[@]" -gt 0 ] && [ $(printf "%s00" "$array[@]" | 
      LC_ALL=C sort -z -u |
      grep -z -c .) -eq 1 ] ; then
      echo ok
      else
      echo bad
      fi


      English explanation: if unique-sorting the elements of the array results in only one element, then print "ok". Otherwise print "bad".



      The array is printed with NUL bytes separating each element, piped into GNU sort (relying on the -z aka --zero-terminated and -u aka --unique options), and then into grep (using options -z aka --null-data and -c aka --count) to count the output lines.



      Unlike my previous version, I can't use wc here because it requires input lines terminated with a newline...and using sed or tr to convert NULs to newlines after the sort would defeat the purpose of using NUL separators. grep -c makes a reasonable substitute.




      Here's the same thing rewritten as a function:



      function count_unique() 
      local LC_ALL=C

      if [ "$#" -eq 0 ] ; then
      echo 0
      else
      echo "$(printf "%s00" "$@"



      ARRAY_DISK_Quantity=(4 4 4 4 2 4 4 4)

      if [ "$(count_unique "$ARRAY_DISK_Quantity[@]")" -eq 1 ] ; then
      echo "ok"
      else
      echo "bad"
      fi





      share|improve this answer














      bash + GNU sort + GNU grep solution:



      if [ "$#array[@]" -gt 0 ] && [ $(printf "%s00" "$array[@]" | 
      LC_ALL=C sort -z -u |
      grep -z -c .) -eq 1 ] ; then
      echo ok
      else
      echo bad
      fi


      English explanation: if unique-sorting the elements of the array results in only one element, then print "ok". Otherwise print "bad".



      The array is printed with NUL bytes separating each element, piped into GNU sort (relying on the -z aka --zero-terminated and -u aka --unique options), and then into grep (using options -z aka --null-data and -c aka --count) to count the output lines.



      Unlike my previous version, I can't use wc here because it requires input lines terminated with a newline...and using sed or tr to convert NULs to newlines after the sort would defeat the purpose of using NUL separators. grep -c makes a reasonable substitute.




      Here's the same thing rewritten as a function:



      function count_unique() 
      local LC_ALL=C

      if [ "$#" -eq 0 ] ; then
      echo 0
      else
      echo "$(printf "%s00" "$@"



      ARRAY_DISK_Quantity=(4 4 4 4 2 4 4 4)

      if [ "$(count_unique "$ARRAY_DISK_Quantity[@]")" -eq 1 ] ; then
      echo "ok"
      else
      echo "bad"
      fi






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 25 '17 at 14:44

























      answered Dec 25 '17 at 11:17









      cas

      37.7k44394




      37.7k44394







      • 1




        Note that sort -u doesn't return unique elements but one of each set of elements that sort the same. For instance, it would say "ok" on ARRAY_DISK_Quantity=(① ②) on a GNU systems where locales typically decide those 2 characters sort the same. You'd want LC_ALL=C sort -u for byte-to-byte uniqueness.
        – Stéphane Chazelas
        Dec 25 '17 at 11:58











      • just another note it will be fail also in case no additional disks are appears from CLI so need also to add this syntax
        – yael
        Dec 25 '17 at 12:22










      • [[ ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | wc -l ` -eq ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | grep -c "0" ` ]] && echo fail
        – yael
        Dec 25 '17 at 12:22










      • @StéphaneChazelas the locale issue is worth dealing with, as is the IFS issue. Testing for an empty list is, IMO, best done separately - there's no need to check for non-unique elements in an empty set.
        – cas
        Dec 25 '17 at 12:53










      • Hi Cas I prefer your previous answer
        – yael
        Dec 25 '17 at 13:55












      • 1




        Note that sort -u doesn't return unique elements but one of each set of elements that sort the same. For instance, it would say "ok" on ARRAY_DISK_Quantity=(① ②) on a GNU systems where locales typically decide those 2 characters sort the same. You'd want LC_ALL=C sort -u for byte-to-byte uniqueness.
        – Stéphane Chazelas
        Dec 25 '17 at 11:58











      • just another note it will be fail also in case no additional disks are appears from CLI so need also to add this syntax
        – yael
        Dec 25 '17 at 12:22










      • [[ ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | wc -l ` -eq ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | grep -c "0" ` ]] && echo fail
        – yael
        Dec 25 '17 at 12:22










      • @StéphaneChazelas the locale issue is worth dealing with, as is the IFS issue. Testing for an empty list is, IMO, best done separately - there's no need to check for non-unique elements in an empty set.
        – cas
        Dec 25 '17 at 12:53










      • Hi Cas I prefer your previous answer
        – yael
        Dec 25 '17 at 13:55







      1




      1




      Note that sort -u doesn't return unique elements but one of each set of elements that sort the same. For instance, it would say "ok" on ARRAY_DISK_Quantity=(① ②) on a GNU systems where locales typically decide those 2 characters sort the same. You'd want LC_ALL=C sort -u for byte-to-byte uniqueness.
      – Stéphane Chazelas
      Dec 25 '17 at 11:58





      Note that sort -u doesn't return unique elements but one of each set of elements that sort the same. For instance, it would say "ok" on ARRAY_DISK_Quantity=(① ②) on a GNU systems where locales typically decide those 2 characters sort the same. You'd want LC_ALL=C sort -u for byte-to-byte uniqueness.
      – Stéphane Chazelas
      Dec 25 '17 at 11:58













      just another note it will be fail also in case no additional disks are appears from CLI so need also to add this syntax
      – yael
      Dec 25 '17 at 12:22




      just another note it will be fail also in case no additional disks are appears from CLI so need also to add this syntax
      – yael
      Dec 25 '17 at 12:22












      [[ ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | wc -l ` -eq ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | grep -c "0" ` ]] && echo fail
      – yael
      Dec 25 '17 at 12:22




      [[ ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | wc -l ` -eq ` printf "%sn" "$ARRAY_DISK_Quantity[@]" | grep -c "0" ` ]] && echo fail
      – yael
      Dec 25 '17 at 12:22












      @StéphaneChazelas the locale issue is worth dealing with, as is the IFS issue. Testing for an empty list is, IMO, best done separately - there's no need to check for non-unique elements in an empty set.
      – cas
      Dec 25 '17 at 12:53




      @StéphaneChazelas the locale issue is worth dealing with, as is the IFS issue. Testing for an empty list is, IMO, best done separately - there's no need to check for non-unique elements in an empty set.
      – cas
      Dec 25 '17 at 12:53












      Hi Cas I prefer your previous answer
      – yael
      Dec 25 '17 at 13:55




      Hi Cas I prefer your previous answer
      – yael
      Dec 25 '17 at 13:55












      up vote
      7
      down vote













      With zsh:



      if (($#$(u)ARRAY_DISK_Quantity[@] == 1)); then
      echo OK
      else
      echo not OK
      fi


      Where (u) is a parameter expansion flag to expand unique values. So we're getting a count of the unique values in the array.



      Replace == 1 with <= 1 is you want to consider an empty array is OK.



      With ksh93, you could sort the array and check that the first element is the same as the last:



      set -s -- "$ARRAY_DISK_Quantity[@]"
      if [ "$1" = "$@: -1" ]; then
      echo OK
      else
      echo not OK
      fi


      With ksh88 or pdksh/mksh:



      set -s -- "$ARRAY_DISK_Quantity[@]"
      if eval '[ "$1" = "$'"$#"'" ]'; then
      echo OK
      else
      echo not OK
      fi


      With bash, you'd probably need a loop:



      unique_values() return 1
      done
      return 0

      if unique_values "$ARRAY_DISK_Quantity[@]"; then
      echo OK
      else
      echo not OK
      fi


      (would work with all the Bourne-like shells with array support (ksh, zsh, bash, yash)).



      Note that it returns OK for an empty array. Add a [ "$#" -gt 0 ] || return at the start of the function if you don't want that.






      share|improve this answer






















      • all these answers not seems to support bash ?
        – yael
        Dec 25 '17 at 12:04










      • @yael, see edit for a bash solution. But why would you use bash?
        – Stéphane Chazelas
        Dec 25 '17 at 12:08










      • In Bash, the help page for typeset says Obsolete. See `help declare'. Is there a reason you're using it instead of local or declare?
        – wjandrea
        Dec 26 '17 at 2:52






      • 1




        @wjandrea typeset is the one that works in all 4 shells. It's also the original one from ksh in the early 80s ( bash mostly copied ksh88 when it comes to variable scoping type setting and declaration but decided to rename typeset declare and make typeset an alias to declare).
        – Stéphane Chazelas
        Dec 26 '17 at 7:32














      up vote
      7
      down vote













      With zsh:



      if (($#$(u)ARRAY_DISK_Quantity[@] == 1)); then
      echo OK
      else
      echo not OK
      fi


      Where (u) is a parameter expansion flag to expand unique values. So we're getting a count of the unique values in the array.



      Replace == 1 with <= 1 is you want to consider an empty array is OK.



      With ksh93, you could sort the array and check that the first element is the same as the last:



      set -s -- "$ARRAY_DISK_Quantity[@]"
      if [ "$1" = "$@: -1" ]; then
      echo OK
      else
      echo not OK
      fi


      With ksh88 or pdksh/mksh:



      set -s -- "$ARRAY_DISK_Quantity[@]"
      if eval '[ "$1" = "$'"$#"'" ]'; then
      echo OK
      else
      echo not OK
      fi


      With bash, you'd probably need a loop:



      unique_values() return 1
      done
      return 0

      if unique_values "$ARRAY_DISK_Quantity[@]"; then
      echo OK
      else
      echo not OK
      fi


      (would work with all the Bourne-like shells with array support (ksh, zsh, bash, yash)).



      Note that it returns OK for an empty array. Add a [ "$#" -gt 0 ] || return at the start of the function if you don't want that.






      share|improve this answer






















      • all these answers not seems to support bash ?
        – yael
        Dec 25 '17 at 12:04










      • @yael, see edit for a bash solution. But why would you use bash?
        – Stéphane Chazelas
        Dec 25 '17 at 12:08










      • In Bash, the help page for typeset says Obsolete. See `help declare'. Is there a reason you're using it instead of local or declare?
        – wjandrea
        Dec 26 '17 at 2:52






      • 1




        @wjandrea typeset is the one that works in all 4 shells. It's also the original one from ksh in the early 80s ( bash mostly copied ksh88 when it comes to variable scoping type setting and declaration but decided to rename typeset declare and make typeset an alias to declare).
        – Stéphane Chazelas
        Dec 26 '17 at 7:32












      up vote
      7
      down vote










      up vote
      7
      down vote









      With zsh:



      if (($#$(u)ARRAY_DISK_Quantity[@] == 1)); then
      echo OK
      else
      echo not OK
      fi


      Where (u) is a parameter expansion flag to expand unique values. So we're getting a count of the unique values in the array.



      Replace == 1 with <= 1 is you want to consider an empty array is OK.



      With ksh93, you could sort the array and check that the first element is the same as the last:



      set -s -- "$ARRAY_DISK_Quantity[@]"
      if [ "$1" = "$@: -1" ]; then
      echo OK
      else
      echo not OK
      fi


      With ksh88 or pdksh/mksh:



      set -s -- "$ARRAY_DISK_Quantity[@]"
      if eval '[ "$1" = "$'"$#"'" ]'; then
      echo OK
      else
      echo not OK
      fi


      With bash, you'd probably need a loop:



      unique_values() return 1
      done
      return 0

      if unique_values "$ARRAY_DISK_Quantity[@]"; then
      echo OK
      else
      echo not OK
      fi


      (would work with all the Bourne-like shells with array support (ksh, zsh, bash, yash)).



      Note that it returns OK for an empty array. Add a [ "$#" -gt 0 ] || return at the start of the function if you don't want that.






      share|improve this answer














      With zsh:



      if (($#$(u)ARRAY_DISK_Quantity[@] == 1)); then
      echo OK
      else
      echo not OK
      fi


      Where (u) is a parameter expansion flag to expand unique values. So we're getting a count of the unique values in the array.



      Replace == 1 with <= 1 is you want to consider an empty array is OK.



      With ksh93, you could sort the array and check that the first element is the same as the last:



      set -s -- "$ARRAY_DISK_Quantity[@]"
      if [ "$1" = "$@: -1" ]; then
      echo OK
      else
      echo not OK
      fi


      With ksh88 or pdksh/mksh:



      set -s -- "$ARRAY_DISK_Quantity[@]"
      if eval '[ "$1" = "$'"$#"'" ]'; then
      echo OK
      else
      echo not OK
      fi


      With bash, you'd probably need a loop:



      unique_values() return 1
      done
      return 0

      if unique_values "$ARRAY_DISK_Quantity[@]"; then
      echo OK
      else
      echo not OK
      fi


      (would work with all the Bourne-like shells with array support (ksh, zsh, bash, yash)).



      Note that it returns OK for an empty array. Add a [ "$#" -gt 0 ] || return at the start of the function if you don't want that.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Dec 25 '17 at 12:43

























      answered Dec 25 '17 at 11:50









      Stéphane Chazelas

      282k53518851




      282k53518851











      • all these answers not seems to support bash ?
        – yael
        Dec 25 '17 at 12:04










      • @yael, see edit for a bash solution. But why would you use bash?
        – Stéphane Chazelas
        Dec 25 '17 at 12:08










      • In Bash, the help page for typeset says Obsolete. See `help declare'. Is there a reason you're using it instead of local or declare?
        – wjandrea
        Dec 26 '17 at 2:52






      • 1




        @wjandrea typeset is the one that works in all 4 shells. It's also the original one from ksh in the early 80s ( bash mostly copied ksh88 when it comes to variable scoping type setting and declaration but decided to rename typeset declare and make typeset an alias to declare).
        – Stéphane Chazelas
        Dec 26 '17 at 7:32
















      • all these answers not seems to support bash ?
        – yael
        Dec 25 '17 at 12:04










      • @yael, see edit for a bash solution. But why would you use bash?
        – Stéphane Chazelas
        Dec 25 '17 at 12:08










      • In Bash, the help page for typeset says Obsolete. See `help declare'. Is there a reason you're using it instead of local or declare?
        – wjandrea
        Dec 26 '17 at 2:52






      • 1




        @wjandrea typeset is the one that works in all 4 shells. It's also the original one from ksh in the early 80s ( bash mostly copied ksh88 when it comes to variable scoping type setting and declaration but decided to rename typeset declare and make typeset an alias to declare).
        – Stéphane Chazelas
        Dec 26 '17 at 7:32















      all these answers not seems to support bash ?
      – yael
      Dec 25 '17 at 12:04




      all these answers not seems to support bash ?
      – yael
      Dec 25 '17 at 12:04












      @yael, see edit for a bash solution. But why would you use bash?
      – Stéphane Chazelas
      Dec 25 '17 at 12:08




      @yael, see edit for a bash solution. But why would you use bash?
      – Stéphane Chazelas
      Dec 25 '17 at 12:08












      In Bash, the help page for typeset says Obsolete. See `help declare'. Is there a reason you're using it instead of local or declare?
      – wjandrea
      Dec 26 '17 at 2:52




      In Bash, the help page for typeset says Obsolete. See `help declare'. Is there a reason you're using it instead of local or declare?
      – wjandrea
      Dec 26 '17 at 2:52




      1




      1




      @wjandrea typeset is the one that works in all 4 shells. It's also the original one from ksh in the early 80s ( bash mostly copied ksh88 when it comes to variable scoping type setting and declaration but decided to rename typeset declare and make typeset an alias to declare).
      – Stéphane Chazelas
      Dec 26 '17 at 7:32




      @wjandrea typeset is the one that works in all 4 shells. It's also the original one from ksh in the early 80s ( bash mostly copied ksh88 when it comes to variable scoping type setting and declaration but decided to rename typeset declare and make typeset an alias to declare).
      – Stéphane Chazelas
      Dec 26 '17 at 7:32










      up vote
      4
      down vote













      bash + awk soltion:



      function get_status() 
      arr=("$@") # get the array passed as argument
      if awk 'v && $1!=v exit 1 v=$1 ' <(printf "%dn" "$arr[@]"); then
      echo "status: Ok"
      else
      echo "status: Bad"
      fi




      Test case #1:



      ARRAY_DISK_Quantity=(4 4 4 4 4 2 4 4)
      get_status "$ARRAY_DISK_Quantity[@]"
      status: Bad



      Test case #2:



      ARRAY_DISK_Quantity=(4 4 4 4 4 4 4 4)
      get_status "$ARRAY_DISK_Quantity[@]"
      status: Ok





      share|improve this answer


























        up vote
        4
        down vote













        bash + awk soltion:



        function get_status() 
        arr=("$@") # get the array passed as argument
        if awk 'v && $1!=v exit 1 v=$1 ' <(printf "%dn" "$arr[@]"); then
        echo "status: Ok"
        else
        echo "status: Bad"
        fi




        Test case #1:



        ARRAY_DISK_Quantity=(4 4 4 4 4 2 4 4)
        get_status "$ARRAY_DISK_Quantity[@]"
        status: Bad



        Test case #2:



        ARRAY_DISK_Quantity=(4 4 4 4 4 4 4 4)
        get_status "$ARRAY_DISK_Quantity[@]"
        status: Ok





        share|improve this answer
























          up vote
          4
          down vote










          up vote
          4
          down vote









          bash + awk soltion:



          function get_status() 
          arr=("$@") # get the array passed as argument
          if awk 'v && $1!=v exit 1 v=$1 ' <(printf "%dn" "$arr[@]"); then
          echo "status: Ok"
          else
          echo "status: Bad"
          fi




          Test case #1:



          ARRAY_DISK_Quantity=(4 4 4 4 4 2 4 4)
          get_status "$ARRAY_DISK_Quantity[@]"
          status: Bad



          Test case #2:



          ARRAY_DISK_Quantity=(4 4 4 4 4 4 4 4)
          get_status "$ARRAY_DISK_Quantity[@]"
          status: Ok





          share|improve this answer














          bash + awk soltion:



          function get_status() 
          arr=("$@") # get the array passed as argument
          if awk 'v && $1!=v exit 1 v=$1 ' <(printf "%dn" "$arr[@]"); then
          echo "status: Ok"
          else
          echo "status: Bad"
          fi




          Test case #1:



          ARRAY_DISK_Quantity=(4 4 4 4 4 2 4 4)
          get_status "$ARRAY_DISK_Quantity[@]"
          status: Bad



          Test case #2:



          ARRAY_DISK_Quantity=(4 4 4 4 4 4 4 4)
          get_status "$ARRAY_DISK_Quantity[@]"
          status: Ok






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Dec 25 '17 at 11:00

























          answered Dec 25 '17 at 10:45









          RomanPerekhrest

          22.4k12145




          22.4k12145




















              up vote
              4
              down vote













              I have another bash only solution that should work with strings as well:



              isarray.equal () 
              local placeholder="$1"
              local num=0
              while (( $# )); do
              if [[ "$1" != "$placeholder" ]]; then
              num=1
              echo 'Bad' && break
              fi
              shift
              done
              [[ "$num" -ne 1 ]] && echo 'Okay'



              Demonstration:



              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(4 4 4 4 2 4 4 4)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Bad
              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(4 4 4 4 4 4 4 4)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Okay
              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(four four four four two four four four)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Bad
              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(four four four four four four four four)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Okay





              share|improve this answer






















              • Note that dots are not valid in function names, though Bash is pretty permissive. This may cause issues like in exporting the function.
                – wjandrea
                Dec 26 '17 at 2:55










              • unix.stackexchange.com/a/245953/237982
                – Jesse_b
                Dec 26 '17 at 3:30














              up vote
              4
              down vote













              I have another bash only solution that should work with strings as well:



              isarray.equal () 
              local placeholder="$1"
              local num=0
              while (( $# )); do
              if [[ "$1" != "$placeholder" ]]; then
              num=1
              echo 'Bad' && break
              fi
              shift
              done
              [[ "$num" -ne 1 ]] && echo 'Okay'



              Demonstration:



              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(4 4 4 4 2 4 4 4)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Bad
              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(4 4 4 4 4 4 4 4)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Okay
              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(four four four four two four four four)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Bad
              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(four four four four four four four four)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Okay





              share|improve this answer






















              • Note that dots are not valid in function names, though Bash is pretty permissive. This may cause issues like in exporting the function.
                – wjandrea
                Dec 26 '17 at 2:55










              • unix.stackexchange.com/a/245953/237982
                – Jesse_b
                Dec 26 '17 at 3:30












              up vote
              4
              down vote










              up vote
              4
              down vote









              I have another bash only solution that should work with strings as well:



              isarray.equal () 
              local placeholder="$1"
              local num=0
              while (( $# )); do
              if [[ "$1" != "$placeholder" ]]; then
              num=1
              echo 'Bad' && break
              fi
              shift
              done
              [[ "$num" -ne 1 ]] && echo 'Okay'



              Demonstration:



              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(4 4 4 4 2 4 4 4)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Bad
              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(4 4 4 4 4 4 4 4)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Okay
              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(four four four four two four four four)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Bad
              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(four four four four four four four four)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Okay





              share|improve this answer














              I have another bash only solution that should work with strings as well:



              isarray.equal () 
              local placeholder="$1"
              local num=0
              while (( $# )); do
              if [[ "$1" != "$placeholder" ]]; then
              num=1
              echo 'Bad' && break
              fi
              shift
              done
              [[ "$num" -ne 1 ]] && echo 'Okay'



              Demonstration:



              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(4 4 4 4 2 4 4 4)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Bad
              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(4 4 4 4 4 4 4 4)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Okay
              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(four four four four two four four four)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Bad
              [root@JBSTEST001 ~]# ARRAY_DISK_Quantity=(four four four four four four four four)
              [root@JBSTEST001 ~]# isarray.equal "$ARRAY_DISK_Quantity[@]"
              Okay






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 25 '17 at 13:56

























              answered Dec 25 '17 at 12:25









              Jesse_b

              10.5k22659




              10.5k22659











              • Note that dots are not valid in function names, though Bash is pretty permissive. This may cause issues like in exporting the function.
                – wjandrea
                Dec 26 '17 at 2:55










              • unix.stackexchange.com/a/245953/237982
                – Jesse_b
                Dec 26 '17 at 3:30
















              • Note that dots are not valid in function names, though Bash is pretty permissive. This may cause issues like in exporting the function.
                – wjandrea
                Dec 26 '17 at 2:55










              • unix.stackexchange.com/a/245953/237982
                – Jesse_b
                Dec 26 '17 at 3:30















              Note that dots are not valid in function names, though Bash is pretty permissive. This may cause issues like in exporting the function.
              – wjandrea
              Dec 26 '17 at 2:55




              Note that dots are not valid in function names, though Bash is pretty permissive. This may cause issues like in exporting the function.
              – wjandrea
              Dec 26 '17 at 2:55












              unix.stackexchange.com/a/245953/237982
              – Jesse_b
              Dec 26 '17 at 3:30




              unix.stackexchange.com/a/245953/237982
              – Jesse_b
              Dec 26 '17 at 3:30










              up vote
              2
              down vote













              With bash and GNU grep:



              if grep -qE '^([0-9]+)( 1)*$' <<< "$ARRAY_DISK_Quantity[@]"; then 
              echo "okay"
              else
              echo "not okay"
              fi





              share|improve this answer






















              • Yes, but what about (10 10 10 10)? Otherwise, quite nice.
                – Joe
                Dec 30 '17 at 6:20










              • @Joe: Good catch. I've updated my answer.
                – Cyrus
                Dec 30 '17 at 6:39














              up vote
              2
              down vote













              With bash and GNU grep:



              if grep -qE '^([0-9]+)( 1)*$' <<< "$ARRAY_DISK_Quantity[@]"; then 
              echo "okay"
              else
              echo "not okay"
              fi





              share|improve this answer






















              • Yes, but what about (10 10 10 10)? Otherwise, quite nice.
                – Joe
                Dec 30 '17 at 6:20










              • @Joe: Good catch. I've updated my answer.
                – Cyrus
                Dec 30 '17 at 6:39












              up vote
              2
              down vote










              up vote
              2
              down vote









              With bash and GNU grep:



              if grep -qE '^([0-9]+)( 1)*$' <<< "$ARRAY_DISK_Quantity[@]"; then 
              echo "okay"
              else
              echo "not okay"
              fi





              share|improve this answer














              With bash and GNU grep:



              if grep -qE '^([0-9]+)( 1)*$' <<< "$ARRAY_DISK_Quantity[@]"; then 
              echo "okay"
              else
              echo "not okay"
              fi






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Dec 30 '17 at 6:39

























              answered Dec 25 '17 at 16:02









              Cyrus

              7,0362835




              7,0362835











              • Yes, but what about (10 10 10 10)? Otherwise, quite nice.
                – Joe
                Dec 30 '17 at 6:20










              • @Joe: Good catch. I've updated my answer.
                – Cyrus
                Dec 30 '17 at 6:39
















              • Yes, but what about (10 10 10 10)? Otherwise, quite nice.
                – Joe
                Dec 30 '17 at 6:20










              • @Joe: Good catch. I've updated my answer.
                – Cyrus
                Dec 30 '17 at 6:39















              Yes, but what about (10 10 10 10)? Otherwise, quite nice.
              – Joe
              Dec 30 '17 at 6:20




              Yes, but what about (10 10 10 10)? Otherwise, quite nice.
              – Joe
              Dec 30 '17 at 6:20












              @Joe: Good catch. I've updated my answer.
              – Cyrus
              Dec 30 '17 at 6:39




              @Joe: Good catch. I've updated my answer.
              – Cyrus
              Dec 30 '17 at 6:39










              up vote
              1
              down vote













              Here is POSIX Awk:



              awk 'BEGIN while (++z < ARGC) if (ARGV[z] != ARGV[1]) exit 1' "$ARRAY_DISK_Quantity[@]"





              share|improve this answer
























                up vote
                1
                down vote













                Here is POSIX Awk:



                awk 'BEGIN while (++z < ARGC) if (ARGV[z] != ARGV[1]) exit 1' "$ARRAY_DISK_Quantity[@]"





                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  Here is POSIX Awk:



                  awk 'BEGIN while (++z < ARGC) if (ARGV[z] != ARGV[1]) exit 1' "$ARRAY_DISK_Quantity[@]"





                  share|improve this answer












                  Here is POSIX Awk:



                  awk 'BEGIN while (++z < ARGC) if (ARGV[z] != ARGV[1]) exit 1' "$ARRAY_DISK_Quantity[@]"






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 25 '17 at 22:29









                  Steven Penny

                  2,30821635




                  2,30821635




















                      up vote
                      0
                      down vote













                      bash only solution (assuming a is ARRAY_DISK_Quantity)



                      ttt=$a[0]
                      res=0
                      for i in "$a[@]"
                      do
                      let res+=$(if [ "$ttt" -ne "$i" ]; then echo 1; else echo 0; fi);
                      done
                      if [ "$res" -eq 0 ]
                      then
                      echo "ok"
                      else
                      echo "bad"
                      fi





                      share|improve this answer




















                      • Works, but counts all errors when just one is enough: if [ "$ttt" -ne "$i" ]; then res=1; break; fi;
                        – Joe
                        Dec 30 '17 at 6:30














                      up vote
                      0
                      down vote













                      bash only solution (assuming a is ARRAY_DISK_Quantity)



                      ttt=$a[0]
                      res=0
                      for i in "$a[@]"
                      do
                      let res+=$(if [ "$ttt" -ne "$i" ]; then echo 1; else echo 0; fi);
                      done
                      if [ "$res" -eq 0 ]
                      then
                      echo "ok"
                      else
                      echo "bad"
                      fi





                      share|improve this answer




















                      • Works, but counts all errors when just one is enough: if [ "$ttt" -ne "$i" ]; then res=1; break; fi;
                        – Joe
                        Dec 30 '17 at 6:30












                      up vote
                      0
                      down vote










                      up vote
                      0
                      down vote









                      bash only solution (assuming a is ARRAY_DISK_Quantity)



                      ttt=$a[0]
                      res=0
                      for i in "$a[@]"
                      do
                      let res+=$(if [ "$ttt" -ne "$i" ]; then echo 1; else echo 0; fi);
                      done
                      if [ "$res" -eq 0 ]
                      then
                      echo "ok"
                      else
                      echo "bad"
                      fi





                      share|improve this answer












                      bash only solution (assuming a is ARRAY_DISK_Quantity)



                      ttt=$a[0]
                      res=0
                      for i in "$a[@]"
                      do
                      let res+=$(if [ "$ttt" -ne "$i" ]; then echo 1; else echo 0; fi);
                      done
                      if [ "$res" -eq 0 ]
                      then
                      echo "ok"
                      else
                      echo "bad"
                      fi






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Dec 25 '17 at 10:58









                      Bhavin Chirag

                      1514




                      1514











                      • Works, but counts all errors when just one is enough: if [ "$ttt" -ne "$i" ]; then res=1; break; fi;
                        – Joe
                        Dec 30 '17 at 6:30
















                      • Works, but counts all errors when just one is enough: if [ "$ttt" -ne "$i" ]; then res=1; break; fi;
                        – Joe
                        Dec 30 '17 at 6:30















                      Works, but counts all errors when just one is enough: if [ "$ttt" -ne "$i" ]; then res=1; break; fi;
                      – Joe
                      Dec 30 '17 at 6:30




                      Works, but counts all errors when just one is enough: if [ "$ttt" -ne "$i" ]; then res=1; break; fi;
                      – Joe
                      Dec 30 '17 at 6:30












                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f412933%2fhow-to-test-if-array-elements-are-all-equal-in-bash%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      How to check contact read email or not when send email to Individual?

                      Bahrain

                      Postfix configuration issue with fips on centos 7; mailgun relay