如何在字符串中找到子字符串(或如何grep变量)? [英] How to find substring inside a string (or how to grep a variable)?
本文介绍了如何在字符串中找到子字符串(或如何grep变量)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是BASH,我不知道如何找到一个子字符串。
I'm using BASH, and I don't know how to find a substring. It keeps failing, I've got a string (should this be an array?)
下面, LIST
是一个字符串(应该是一个数组)数据库名称的字符串列表 SOURCE
是回复,其中一个数据库。以下仍然无效:
Below, LIST
is a string list of database names, SOURCE
is the reply, one of those databases. The following still doesn't work:
echo "******************************************************************"
echo "* DB2 Offline Backup Script *"
echo "******************************************************************"
echo "What's the name of of the database you would like to backup?"
echo "It will be named one in this list:"
echo ""
LIST=`db2 list database directory | grep "Database alias" | awk '{print $4}'`
echo $LIST
echo ""
echo "******************************************************************"
echo -n ">>> "
read -e SOURCE
if expr match "$LIST" "$SOURCE"; then
echo "match"
exit -1
else
echo "no match"
fi
exit -1
我也尝试过但不起作用:
I've also tried this but doesn't work:
if [ `expr match "$LIST" '$SOURCE'` ]; then
推荐答案
LIST="some string with a substring you want to match"
SOURCE="substring"
if echo "$LIST" | grep -q "$SOURCE"; then
echo "matched";
else
echo "no match";
fi
这篇关于如何在字符串中找到子字符串(或如何grep变量)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文