How can we check whether the user input equal to one elements of an array?Command substitiution problem, confusedBash reading array in different formatbash: populating array with elements containing spaces and special charactersHow to hide stderr without “muting” user inputprocessing arguments with bash functionHow I can count how many repeated numbers in a file and then organize them by repetitions?Using user input to determine how many times a for loop iteratesModify a list: filter it, and rearrange the values in the resultMatching two files and keeping blocks that contain the matchHow to get bash file to echo differently based on user input?

Pass on your radiation

How to permanently set refresh interval for top command?

Is there a difference between 回转 and 掉头?

Is it appropriate to rewrite and republish another author's useful but very badly written paper?

Finding New York City tournaments as a visitor from Moscow

Is it principled to tip less if a pricey restaurant doesn't accept Visa or Mastercard?

I am ask to complete my withdrawal transaction with COT fee of 1200 dollars

Is paying for portrait photos good for the people in the community you're photographing?

How to quickly type a C expression where the assigned-to variable is repeated?

Why are bicycle tires incapable of maintaining pressure over time, while car tyres seem to have less of a problem?

How can I open or identify this lamp?

How to get a bowl with one liter of water

"Sack" data structure in C#

Does the sterile cockpit rule mean flight attendants could not inform the pilots if a passenger is in the lavatory while on final?

Does the geothermal activity influence the climate in Iceland?

How can I force a bank to close my account with them?

Check if simple regex matches string

What's the name of this retro Simpsons videogame?

How to Take Advantage of Arbitrage Opportunity of Two Options

How to write rhead only on the first page?

Is it appropriate to say おじゃまします when entering one's own room?

Set identification

Can we change the volume of a wire by applying force on it in order to stretch it, if yes is there any way to calculate it?

Partial Relaxation



How can we check whether the user input equal to one elements of an array?


Command substitiution problem, confusedBash reading array in different formatbash: populating array with elements containing spaces and special charactersHow to hide stderr without “muting” user inputprocessing arguments with bash functionHow I can count how many repeated numbers in a file and then organize them by repetitions?Using user input to determine how many times a for loop iteratesModify a list: filter it, and rearrange the values in the resultMatching two files and keeping blocks that contain the matchHow to get bash file to echo differently based on user input?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









4


















Imagine we have an array like this:



declare -a array=( "one" "two" "three" "four" "five" )

for i in "$all[@]"; do
echo "$i"
done


And we can give an input from the user like this:



read -p "Enter a number: " number


I need something to check if the user input matches one of the elements of this array. If not, this should run:



echo "Try again: " 


This process should be repeated until the user input matches to one of the array elements.










share|improve this question



























  • Is this a homework project?

    – Graham
    Oct 1 at 16:50











  • No. I just tried many ways but as I'm new learner I did not find a good way.

    – msndm
    Oct 1 at 16:53






  • 3





    this reads very similar to a homwork problem... What have you tried so far? Have you gotten any errors? Or are you just asking for a handout?

    – j-money
    Oct 1 at 16:54






  • 1





    stackexchange.com/filters/169 might be a better place for this question if its not about Ubuntu.

    – Graham
    Oct 1 at 16:55







  • 1





    This question is perfectly on-topic here. Please do not close questions as off-topic just because they're about bash. We have a lot of users who are able to help with Bash questions. I would even argue that these questions would be better received here on Ask Ubuntu and on Unix & Linux!

    – Dan
    Oct 2 at 11:27


















4


















Imagine we have an array like this:



declare -a array=( "one" "two" "three" "four" "five" )

for i in "$all[@]"; do
echo "$i"
done


And we can give an input from the user like this:



read -p "Enter a number: " number


I need something to check if the user input matches one of the elements of this array. If not, this should run:



echo "Try again: " 


This process should be repeated until the user input matches to one of the array elements.










share|improve this question



























  • Is this a homework project?

    – Graham
    Oct 1 at 16:50











  • No. I just tried many ways but as I'm new learner I did not find a good way.

    – msndm
    Oct 1 at 16:53






  • 3





    this reads very similar to a homwork problem... What have you tried so far? Have you gotten any errors? Or are you just asking for a handout?

    – j-money
    Oct 1 at 16:54






  • 1





    stackexchange.com/filters/169 might be a better place for this question if its not about Ubuntu.

    – Graham
    Oct 1 at 16:55







  • 1





    This question is perfectly on-topic here. Please do not close questions as off-topic just because they're about bash. We have a lot of users who are able to help with Bash questions. I would even argue that these questions would be better received here on Ask Ubuntu and on Unix & Linux!

    – Dan
    Oct 2 at 11:27














4













4









4








Imagine we have an array like this:



declare -a array=( "one" "two" "three" "four" "five" )

for i in "$all[@]"; do
echo "$i"
done


And we can give an input from the user like this:



read -p "Enter a number: " number


I need something to check if the user input matches one of the elements of this array. If not, this should run:



echo "Try again: " 


This process should be repeated until the user input matches to one of the array elements.










share|improve this question
















Imagine we have an array like this:



declare -a array=( "one" "two" "three" "four" "five" )

for i in "$all[@]"; do
echo "$i"
done


And we can give an input from the user like this:



read -p "Enter a number: " number


I need something to check if the user input matches one of the elements of this array. If not, this should run:



echo "Try again: " 


This process should be repeated until the user input matches to one of the array elements.







bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Oct 1 at 17:18









user3140225

2,7104 gold badges12 silver badges24 bronze badges




2,7104 gold badges12 silver badges24 bronze badges










asked Oct 1 at 16:47









msndmmsndm

1193 bronze badges




1193 bronze badges















  • Is this a homework project?

    – Graham
    Oct 1 at 16:50











  • No. I just tried many ways but as I'm new learner I did not find a good way.

    – msndm
    Oct 1 at 16:53






  • 3





    this reads very similar to a homwork problem... What have you tried so far? Have you gotten any errors? Or are you just asking for a handout?

    – j-money
    Oct 1 at 16:54






  • 1





    stackexchange.com/filters/169 might be a better place for this question if its not about Ubuntu.

    – Graham
    Oct 1 at 16:55







  • 1





    This question is perfectly on-topic here. Please do not close questions as off-topic just because they're about bash. We have a lot of users who are able to help with Bash questions. I would even argue that these questions would be better received here on Ask Ubuntu and on Unix & Linux!

    – Dan
    Oct 2 at 11:27


















  • Is this a homework project?

    – Graham
    Oct 1 at 16:50











  • No. I just tried many ways but as I'm new learner I did not find a good way.

    – msndm
    Oct 1 at 16:53






  • 3





    this reads very similar to a homwork problem... What have you tried so far? Have you gotten any errors? Or are you just asking for a handout?

    – j-money
    Oct 1 at 16:54






  • 1





    stackexchange.com/filters/169 might be a better place for this question if its not about Ubuntu.

    – Graham
    Oct 1 at 16:55







  • 1





    This question is perfectly on-topic here. Please do not close questions as off-topic just because they're about bash. We have a lot of users who are able to help with Bash questions. I would even argue that these questions would be better received here on Ask Ubuntu and on Unix & Linux!

    – Dan
    Oct 2 at 11:27

















Is this a homework project?

– Graham
Oct 1 at 16:50





Is this a homework project?

– Graham
Oct 1 at 16:50













No. I just tried many ways but as I'm new learner I did not find a good way.

– msndm
Oct 1 at 16:53





No. I just tried many ways but as I'm new learner I did not find a good way.

– msndm
Oct 1 at 16:53




3




3





this reads very similar to a homwork problem... What have you tried so far? Have you gotten any errors? Or are you just asking for a handout?

– j-money
Oct 1 at 16:54





this reads very similar to a homwork problem... What have you tried so far? Have you gotten any errors? Or are you just asking for a handout?

– j-money
Oct 1 at 16:54




1




1





stackexchange.com/filters/169 might be a better place for this question if its not about Ubuntu.

– Graham
Oct 1 at 16:55






stackexchange.com/filters/169 might be a better place for this question if its not about Ubuntu.

– Graham
Oct 1 at 16:55





1




1





This question is perfectly on-topic here. Please do not close questions as off-topic just because they're about bash. We have a lot of users who are able to help with Bash questions. I would even argue that these questions would be better received here on Ask Ubuntu and on Unix & Linux!

– Dan
Oct 2 at 11:27






This question is perfectly on-topic here. Please do not close questions as off-topic just because they're about bash. We have a lot of users who are able to help with Bash questions. I would even argue that these questions would be better received here on Ask Ubuntu and on Unix & Linux!

– Dan
Oct 2 at 11:27











4 Answers
4






active

oldest

votes


















3



















You do not need to loop over the array to check whether a value belongs to it. Also, in this case I would use a recursive call of a function instead of another loop. As I understand correctly you are searching for something like this:



#!/bin/bash

array=( "one" "two" "three" "four" "five" )

function get_input()
read -p "$1: " number
if [[ " $array[*] " == *" $number "* ]]
then
echo 'true';
else
get_input 'Try again' # a recursive call of the function
fi


get_input 'Enter a number' # the initial call of the function


With the [[ test command we searing for exact match == between one string (the left side) and one regular expression (the right side).



  • The expression " $array[*] " will expand our array as a string, surrounded by two spaces, thus each element (even the first and the last) will be surrounded by spaces - try echo '^'" $array[*] "'$'.


  • The regular expression *" $number "* will match to any strung that consist of the value of the variable $number, surrounded by spaces, surrounded by no mater what other characters *.


We can perform such test by some other tools like grep (with suppressed stdout -q, and searching for exact match <...>). Here is one funny version:



#!/bin/bash

array=( "one" "two" "three" "four" "five" )

function get_input() get_input 'Try again'


get_input "Enter a number"



If we want to call the corresponding number of each element in the
array instead of inserting the words like ''one'' or ''two'', what can
we do? for example: Enter the number: 2




#!/bin/bash

array=( "one" "two" "three" "four" "five" )

# Get the lenght of the array
array_lenght="$#array[*]"

function get_input()
read -p "$1: " number

# Test whether the variable $number has a value &and&
# test whether as integer this value is less or equal to the $array_lenght
if [[ ! -z $number ]] && (( number <= array_lenght && number > 0 ))
then
# Outpit the value of the cerain array element
# Note the first array element has number 0
echo "$array[$(( number - 1 ))]"
else
get_input 'Try again'
fi


get_input 'Enter an integer number'





share|improve this answer






















  • 1





    this will get you a clear look [[ " $array[*] " =~ " $number " ]]

    – bac0n
    Oct 2 at 7:17












  • If we want to call the corresponding number of each element in the array instead of inserting the words like ''one'' or ''two'', what can we do? for example: Enter the number: 2

    – msndm
    Oct 3 at 16:48












  • Hi, @msndm, the question is updated.

    – pa4080
    Oct 3 at 19:00


















5



















You can make the array into an associative array instead.



#!/bin/bash

declare -A array=([one]=1 [two]=1 [tree]=1 [four]=1 [five]=1)

m="Guess? "

while true; do
read -r -p "$m" i
[[ $array[$i] == 1 ]] && break || m="Guess again? "
done

echo "Good guess!"





share|improve this answer


































    3



















    Probably all the other answers are more efficient than this one, but I think that this is more beginner-friendly:



    declare -a all=( "one" "two" "three" "four" "five" )

    read -p "Enter a number: " number

    while true; do

    for i in "$all[@]"; do
    if [[ "$number" = "$i" ]]; then
    exit 0
    fi
    done

    read -p "Try again: " number

    done





    share|improve this answer
































      2



















      #!/bin/bash

      declare -a array=("one" "two" "three" "four" "five")

      number="wrong"

      while [[ ! "$array[@]" =~ "$number" ]]; do
      [[ "$number" != "wrong" ]] && echo "Incorrect"
      read -p "Insert number: " number
      done





      share|improve this answer


























        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "89"
        ;
        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/4.0/"u003ecc by-sa 4.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%2faskubuntu.com%2fquestions%2f1177947%2fhow-can-we-check-whether-the-user-input-equal-to-one-elements-of-an-array%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown


























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3



















        You do not need to loop over the array to check whether a value belongs to it. Also, in this case I would use a recursive call of a function instead of another loop. As I understand correctly you are searching for something like this:



        #!/bin/bash

        array=( "one" "two" "three" "four" "five" )

        function get_input()
        read -p "$1: " number
        if [[ " $array[*] " == *" $number "* ]]
        then
        echo 'true';
        else
        get_input 'Try again' # a recursive call of the function
        fi


        get_input 'Enter a number' # the initial call of the function


        With the [[ test command we searing for exact match == between one string (the left side) and one regular expression (the right side).



        • The expression " $array[*] " will expand our array as a string, surrounded by two spaces, thus each element (even the first and the last) will be surrounded by spaces - try echo '^'" $array[*] "'$'.


        • The regular expression *" $number "* will match to any strung that consist of the value of the variable $number, surrounded by spaces, surrounded by no mater what other characters *.


        We can perform such test by some other tools like grep (with suppressed stdout -q, and searching for exact match <...>). Here is one funny version:



        #!/bin/bash

        array=( "one" "two" "three" "four" "five" )

        function get_input() get_input 'Try again'


        get_input "Enter a number"



        If we want to call the corresponding number of each element in the
        array instead of inserting the words like ''one'' or ''two'', what can
        we do? for example: Enter the number: 2




        #!/bin/bash

        array=( "one" "two" "three" "four" "five" )

        # Get the lenght of the array
        array_lenght="$#array[*]"

        function get_input()
        read -p "$1: " number

        # Test whether the variable $number has a value &and&
        # test whether as integer this value is less or equal to the $array_lenght
        if [[ ! -z $number ]] && (( number <= array_lenght && number > 0 ))
        then
        # Outpit the value of the cerain array element
        # Note the first array element has number 0
        echo "$array[$(( number - 1 ))]"
        else
        get_input 'Try again'
        fi


        get_input 'Enter an integer number'





        share|improve this answer






















        • 1





          this will get you a clear look [[ " $array[*] " =~ " $number " ]]

          – bac0n
          Oct 2 at 7:17












        • If we want to call the corresponding number of each element in the array instead of inserting the words like ''one'' or ''two'', what can we do? for example: Enter the number: 2

          – msndm
          Oct 3 at 16:48












        • Hi, @msndm, the question is updated.

          – pa4080
          Oct 3 at 19:00















        3



















        You do not need to loop over the array to check whether a value belongs to it. Also, in this case I would use a recursive call of a function instead of another loop. As I understand correctly you are searching for something like this:



        #!/bin/bash

        array=( "one" "two" "three" "four" "five" )

        function get_input()
        read -p "$1: " number
        if [[ " $array[*] " == *" $number "* ]]
        then
        echo 'true';
        else
        get_input 'Try again' # a recursive call of the function
        fi


        get_input 'Enter a number' # the initial call of the function


        With the [[ test command we searing for exact match == between one string (the left side) and one regular expression (the right side).



        • The expression " $array[*] " will expand our array as a string, surrounded by two spaces, thus each element (even the first and the last) will be surrounded by spaces - try echo '^'" $array[*] "'$'.


        • The regular expression *" $number "* will match to any strung that consist of the value of the variable $number, surrounded by spaces, surrounded by no mater what other characters *.


        We can perform such test by some other tools like grep (with suppressed stdout -q, and searching for exact match <...>). Here is one funny version:



        #!/bin/bash

        array=( "one" "two" "three" "four" "five" )

        function get_input() get_input 'Try again'


        get_input "Enter a number"



        If we want to call the corresponding number of each element in the
        array instead of inserting the words like ''one'' or ''two'', what can
        we do? for example: Enter the number: 2




        #!/bin/bash

        array=( "one" "two" "three" "four" "five" )

        # Get the lenght of the array
        array_lenght="$#array[*]"

        function get_input()
        read -p "$1: " number

        # Test whether the variable $number has a value &and&
        # test whether as integer this value is less or equal to the $array_lenght
        if [[ ! -z $number ]] && (( number <= array_lenght && number > 0 ))
        then
        # Outpit the value of the cerain array element
        # Note the first array element has number 0
        echo "$array[$(( number - 1 ))]"
        else
        get_input 'Try again'
        fi


        get_input 'Enter an integer number'





        share|improve this answer






















        • 1





          this will get you a clear look [[ " $array[*] " =~ " $number " ]]

          – bac0n
          Oct 2 at 7:17












        • If we want to call the corresponding number of each element in the array instead of inserting the words like ''one'' or ''two'', what can we do? for example: Enter the number: 2

          – msndm
          Oct 3 at 16:48












        • Hi, @msndm, the question is updated.

          – pa4080
          Oct 3 at 19:00













        3















        3











        3









        You do not need to loop over the array to check whether a value belongs to it. Also, in this case I would use a recursive call of a function instead of another loop. As I understand correctly you are searching for something like this:



        #!/bin/bash

        array=( "one" "two" "three" "four" "five" )

        function get_input()
        read -p "$1: " number
        if [[ " $array[*] " == *" $number "* ]]
        then
        echo 'true';
        else
        get_input 'Try again' # a recursive call of the function
        fi


        get_input 'Enter a number' # the initial call of the function


        With the [[ test command we searing for exact match == between one string (the left side) and one regular expression (the right side).



        • The expression " $array[*] " will expand our array as a string, surrounded by two spaces, thus each element (even the first and the last) will be surrounded by spaces - try echo '^'" $array[*] "'$'.


        • The regular expression *" $number "* will match to any strung that consist of the value of the variable $number, surrounded by spaces, surrounded by no mater what other characters *.


        We can perform such test by some other tools like grep (with suppressed stdout -q, and searching for exact match <...>). Here is one funny version:



        #!/bin/bash

        array=( "one" "two" "three" "four" "five" )

        function get_input() get_input 'Try again'


        get_input "Enter a number"



        If we want to call the corresponding number of each element in the
        array instead of inserting the words like ''one'' or ''two'', what can
        we do? for example: Enter the number: 2




        #!/bin/bash

        array=( "one" "two" "three" "four" "five" )

        # Get the lenght of the array
        array_lenght="$#array[*]"

        function get_input()
        read -p "$1: " number

        # Test whether the variable $number has a value &and&
        # test whether as integer this value is less or equal to the $array_lenght
        if [[ ! -z $number ]] && (( number <= array_lenght && number > 0 ))
        then
        # Outpit the value of the cerain array element
        # Note the first array element has number 0
        echo "$array[$(( number - 1 ))]"
        else
        get_input 'Try again'
        fi


        get_input 'Enter an integer number'





        share|improve this answer
















        You do not need to loop over the array to check whether a value belongs to it. Also, in this case I would use a recursive call of a function instead of another loop. As I understand correctly you are searching for something like this:



        #!/bin/bash

        array=( "one" "two" "three" "four" "five" )

        function get_input()
        read -p "$1: " number
        if [[ " $array[*] " == *" $number "* ]]
        then
        echo 'true';
        else
        get_input 'Try again' # a recursive call of the function
        fi


        get_input 'Enter a number' # the initial call of the function


        With the [[ test command we searing for exact match == between one string (the left side) and one regular expression (the right side).



        • The expression " $array[*] " will expand our array as a string, surrounded by two spaces, thus each element (even the first and the last) will be surrounded by spaces - try echo '^'" $array[*] "'$'.


        • The regular expression *" $number "* will match to any strung that consist of the value of the variable $number, surrounded by spaces, surrounded by no mater what other characters *.


        We can perform such test by some other tools like grep (with suppressed stdout -q, and searching for exact match <...>). Here is one funny version:



        #!/bin/bash

        array=( "one" "two" "three" "four" "five" )

        function get_input() get_input 'Try again'


        get_input "Enter a number"



        If we want to call the corresponding number of each element in the
        array instead of inserting the words like ''one'' or ''two'', what can
        we do? for example: Enter the number: 2




        #!/bin/bash

        array=( "one" "two" "three" "four" "five" )

        # Get the lenght of the array
        array_lenght="$#array[*]"

        function get_input()
        read -p "$1: " number

        # Test whether the variable $number has a value &and&
        # test whether as integer this value is less or equal to the $array_lenght
        if [[ ! -z $number ]] && (( number <= array_lenght && number > 0 ))
        then
        # Outpit the value of the cerain array element
        # Note the first array element has number 0
        echo "$array[$(( number - 1 ))]"
        else
        get_input 'Try again'
        fi


        get_input 'Enter an integer number'






        share|improve this answer















        share|improve this answer




        share|improve this answer








        edited Oct 3 at 18:57

























        answered Oct 1 at 17:37









        pa4080pa4080

        20k8 gold badges45 silver badges93 bronze badges




        20k8 gold badges45 silver badges93 bronze badges










        • 1





          this will get you a clear look [[ " $array[*] " =~ " $number " ]]

          – bac0n
          Oct 2 at 7:17












        • If we want to call the corresponding number of each element in the array instead of inserting the words like ''one'' or ''two'', what can we do? for example: Enter the number: 2

          – msndm
          Oct 3 at 16:48












        • Hi, @msndm, the question is updated.

          – pa4080
          Oct 3 at 19:00












        • 1





          this will get you a clear look [[ " $array[*] " =~ " $number " ]]

          – bac0n
          Oct 2 at 7:17












        • If we want to call the corresponding number of each element in the array instead of inserting the words like ''one'' or ''two'', what can we do? for example: Enter the number: 2

          – msndm
          Oct 3 at 16:48












        • Hi, @msndm, the question is updated.

          – pa4080
          Oct 3 at 19:00







        1




        1





        this will get you a clear look [[ " $array[*] " =~ " $number " ]]

        – bac0n
        Oct 2 at 7:17






        this will get you a clear look [[ " $array[*] " =~ " $number " ]]

        – bac0n
        Oct 2 at 7:17














        If we want to call the corresponding number of each element in the array instead of inserting the words like ''one'' or ''two'', what can we do? for example: Enter the number: 2

        – msndm
        Oct 3 at 16:48






        If we want to call the corresponding number of each element in the array instead of inserting the words like ''one'' or ''two'', what can we do? for example: Enter the number: 2

        – msndm
        Oct 3 at 16:48














        Hi, @msndm, the question is updated.

        – pa4080
        Oct 3 at 19:00





        Hi, @msndm, the question is updated.

        – pa4080
        Oct 3 at 19:00













        5



















        You can make the array into an associative array instead.



        #!/bin/bash

        declare -A array=([one]=1 [two]=1 [tree]=1 [four]=1 [five]=1)

        m="Guess? "

        while true; do
        read -r -p "$m" i
        [[ $array[$i] == 1 ]] && break || m="Guess again? "
        done

        echo "Good guess!"





        share|improve this answer































          5



















          You can make the array into an associative array instead.



          #!/bin/bash

          declare -A array=([one]=1 [two]=1 [tree]=1 [four]=1 [five]=1)

          m="Guess? "

          while true; do
          read -r -p "$m" i
          [[ $array[$i] == 1 ]] && break || m="Guess again? "
          done

          echo "Good guess!"





          share|improve this answer





























            5















            5











            5









            You can make the array into an associative array instead.



            #!/bin/bash

            declare -A array=([one]=1 [two]=1 [tree]=1 [four]=1 [five]=1)

            m="Guess? "

            while true; do
            read -r -p "$m" i
            [[ $array[$i] == 1 ]] && break || m="Guess again? "
            done

            echo "Good guess!"





            share|improve this answer
















            You can make the array into an associative array instead.



            #!/bin/bash

            declare -A array=([one]=1 [two]=1 [tree]=1 [four]=1 [five]=1)

            m="Guess? "

            while true; do
            read -r -p "$m" i
            [[ $array[$i] == 1 ]] && break || m="Guess again? "
            done

            echo "Good guess!"






            share|improve this answer















            share|improve this answer




            share|improve this answer








            edited Oct 1 at 18:01

























            answered Oct 1 at 17:09









            bac0nbac0n

            1,1531 silver badge13 bronze badges




            1,1531 silver badge13 bronze badges
























                3



















                Probably all the other answers are more efficient than this one, but I think that this is more beginner-friendly:



                declare -a all=( "one" "two" "three" "four" "five" )

                read -p "Enter a number: " number

                while true; do

                for i in "$all[@]"; do
                if [[ "$number" = "$i" ]]; then
                exit 0
                fi
                done

                read -p "Try again: " number

                done





                share|improve this answer





























                  3



















                  Probably all the other answers are more efficient than this one, but I think that this is more beginner-friendly:



                  declare -a all=( "one" "two" "three" "four" "five" )

                  read -p "Enter a number: " number

                  while true; do

                  for i in "$all[@]"; do
                  if [[ "$number" = "$i" ]]; then
                  exit 0
                  fi
                  done

                  read -p "Try again: " number

                  done





                  share|improve this answer



























                    3















                    3











                    3









                    Probably all the other answers are more efficient than this one, but I think that this is more beginner-friendly:



                    declare -a all=( "one" "two" "three" "four" "five" )

                    read -p "Enter a number: " number

                    while true; do

                    for i in "$all[@]"; do
                    if [[ "$number" = "$i" ]]; then
                    exit 0
                    fi
                    done

                    read -p "Try again: " number

                    done





                    share|improve this answer














                    Probably all the other answers are more efficient than this one, but I think that this is more beginner-friendly:



                    declare -a all=( "one" "two" "three" "four" "five" )

                    read -p "Enter a number: " number

                    while true; do

                    for i in "$all[@]"; do
                    if [[ "$number" = "$i" ]]; then
                    exit 0
                    fi
                    done

                    read -p "Try again: " number

                    done






                    share|improve this answer













                    share|improve this answer




                    share|improve this answer










                    answered Oct 1 at 17:43









                    user3140225user3140225

                    2,7104 gold badges12 silver badges24 bronze badges




                    2,7104 gold badges12 silver badges24 bronze badges
























                        2



















                        #!/bin/bash

                        declare -a array=("one" "two" "three" "four" "five")

                        number="wrong"

                        while [[ ! "$array[@]" =~ "$number" ]]; do
                        [[ "$number" != "wrong" ]] && echo "Incorrect"
                        read -p "Insert number: " number
                        done





                        share|improve this answer





























                          2



















                          #!/bin/bash

                          declare -a array=("one" "two" "three" "four" "five")

                          number="wrong"

                          while [[ ! "$array[@]" =~ "$number" ]]; do
                          [[ "$number" != "wrong" ]] && echo "Incorrect"
                          read -p "Insert number: " number
                          done





                          share|improve this answer



























                            2















                            2











                            2









                            #!/bin/bash

                            declare -a array=("one" "two" "three" "four" "five")

                            number="wrong"

                            while [[ ! "$array[@]" =~ "$number" ]]; do
                            [[ "$number" != "wrong" ]] && echo "Incorrect"
                            read -p "Insert number: " number
                            done





                            share|improve this answer














                            #!/bin/bash

                            declare -a array=("one" "two" "three" "four" "five")

                            number="wrong"

                            while [[ ! "$array[@]" =~ "$number" ]]; do
                            [[ "$number" != "wrong" ]] && echo "Incorrect"
                            read -p "Insert number: " number
                            done






                            share|improve this answer













                            share|improve this answer




                            share|improve this answer










                            answered Oct 1 at 17:30









                            guillermo chamorroguillermo chamorro

                            1,7514 silver badges17 bronze badges




                            1,7514 silver badges17 bronze badges































                                draft saved

                                draft discarded















































                                Thanks for contributing an answer to Ask Ubuntu!


                                • 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%2faskubuntu.com%2fquestions%2f1177947%2fhow-can-we-check-whether-the-user-input-equal-to-one-elements-of-an-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

                                Tamil (spriik) Luke uk diar | Nawigatjuun

                                Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

                                Training a classifier when some of the features are unknownWhy does Gradient Boosting regression predict negative values when there are no negative y-values in my training set?How to improve an existing (trained) classifier?What is effect when I set up some self defined predisctor variables?Why Matlab neural network classification returns decimal values on prediction dataset?Fitting and transforming text data in training, testing, and validation setsHow to quantify the performance of the classifier (multi-class SVM) using the test data?How do I control for some patients providing multiple samples in my training data?Training and Test setTraining a convolutional neural network for image denoising in MatlabShouldn't an autoencoder with #(neurons in hidden layer) = #(neurons in input layer) be “perfect”?