2

I'm new to bash shell scripting and it is my first day and first time to post a question here in stackoverflow. I already searched the archive to no avail. I hope someone can help me.

I have an array like this

declare -a SID=("mydb1" "mydb2" "mydb3")

In my script, the user will be prompted to enter a string and it will be stored in $DBNAME variable.

For example a user entered "mydb2" (without quote), this will be stored in $DBNAME variable.

I want to create a loop and I want the input of the user to be tested against each element of the ${SID[@]} variable.

And when a match found, it will exit from the loop and continue with the next command in a script.

Please help me create a script to match a string value against each element of an array variable.

Any help would be highly appreciated. Thank you!

2
  • What do you want to happen when a match is found? Are you only testing THAT a match is found? Or do you care which match? Commented Oct 28, 2011 at 6:18
  • Hi Mark thank you for your concern. Yes I wanted to test the input of the user and it should be matched with one of the element. Commented Oct 28, 2011 at 6:21

3 Answers 3

5

@Flimzy's approach is good. The correct way to use a for-loop is

for db in "${SID[@]}"; do
  if [[ $db = $DBNAME ]]; then
    echo yes
    break
  fi
done
Sign up to request clarification or add additional context in comments.

1 Comment

+1, because this answer works as intended... On the other hand, I'll mention Flimzy's answer here (because my comment to his queston is buried at the end of a long list of comments).. Flimsy's answer can give false positives, eg. "mydb1 mydb2"
1

If all you want to do is check that the user entered a valid dbname, do this:

declare -a SID=("mydb1" "mydb2" "mydb3")
case " ${SID[*]} " in
    *\ $DBNAME\ *)
        echo Entered a correct DB name! Good job, pal!
        ;;
    *)
        echo Try again
        ;;
 esac

This can lead to false-positives in cases where you allow space-containing user-input. If this is a concern, you can solve the problem by using a non-space delimiter that is not allowed in the user's input. For example:

case ".mydb1.mydb2.mydb3." in
    *.$DBNAME.*)

If your user input is completely open-ended, and unvalidated, then a for loop is probably your best bet, as explained in @glennjackson's answer.

7 Comments

I really appreciate that. Thank you! How about if the array values are not explicitly declared in the loop. Something like this, but this one is not working. read -p "Enter DB name :" DBNAME for DB in "${SID[@]}" do if [[ DB != DBNAME ]] ; then printf "Match not found\n" else printf "Match found\n" fi done
You could use a for loop as you suggest, but that's messier if all you care about is finding a match, because you have to keep a temporary variable that you set to true when you find a match, then check that variable after the loop to see if a match was found.
Flimzy and Mark thank you very much. It worked! People like you motivates newbies like me to pursue our goal to learn more. I really appreciate it. In the meantime, I will browse the archive and try to immerse myself with the system. :)
Sure! Tell me how to up-vote and accept your answer. I already clicked the YES button, is that it?
Sorry not enough reputation. Will do next time that's for sure. Thanks again!
|
0

If all you want is to check that $DBNAME is an entry is $SID, the easiest thing to do is probably:

if echo ${SID[@]} | grep -wq "$DBNAME"; then
   # DBNAME is in SID array
fi

Note that -w and -q are non-standard options to grep, so you might want:

if echo ${SID[@]} | grep "\<$DBNAME\>" > /dev/null; then
   # DBNAME is in SID array
fi

This will fail if any of the entries in SID contain spaces, and other odd characters will undoubtedly cause problems as well.

2 Comments

This can return a false positive, even if entries in SID contain no spaces, eg. if the user enters "mydb1 mydb2"
Follow up to this issue. I did further research and I believe it should be: if echo ${SID[@]} | grep -o "$DBNAME"; then echo "Match found" fi Not sure really. :-) The grep -wq "$DBNAME" and grep "\<$DBNAME\>" did not work for me. Again thanks for all the contributions!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.