针对数组的每个元素测试字符串

发布于 2024-12-12 16:48:33 字数 433 浏览 1 评论 0原文

我是 bash shell 脚本编写的新手,这是我第一天也是第一次在 stackoverflow 上发布问题。我已经搜索过档案没有结果。我希望有人能帮助我。

我有一个像这样的数组

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

在我的脚本中,系统将提示用户输入一个字符串,并将其存储在 $DBNAME 变量中。

例如,用户输入“mydb2”(不带引号),这将存储在 $DBNAME 变量中。

我想创建一个循环,并且希望针对 ${SID[@]} 变量的每个元素测试用户的输入。

当找到匹配项时,它将退出循环并继续执行脚本中的下一个命令。

请帮助我创建一个脚本来将字符串值与数组变量的每个元素进行匹配。

任何帮助将不胜感激。谢谢你!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开浅夏 2024-12-19 16:48:34

@Flimzy 的方法很好。使用for循环的正确方法是

for db in "${SID[@]}"; do
  if [[ $db = $DBNAME ]]; then
    echo yes
    break
  fi
done

@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
豆芽 2024-12-19 16:48:34

如果您只想检查用户是否输入了有效的数据库名,请执行以下操作:

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

如果您允许包含空格的用户输入,这可能会导致误报。如果这是一个问题,您可以通过使用用户输入中不允许的非空格分隔符来解决问题。例如:

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

如果您的用户输入完全开放且未经验证,那么 for 循环可能是您最好的选择,如 @glennjackson 的答案中所述。

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.

自演自醉 2024-12-19 16:48:34

如果您想要的只是检查 $DBNAME 是否为 $SID 条目,最简单的做法可能是:

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

请注意 -w 和 -q 是 grep 的非标准选项,因此您可能需要:

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

如果有的话,这将失败SID 中的条目包含空格,并且
其他奇怪的字符无疑也会引起问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文