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?)
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
Well, what about something like this:
PS3="Select database or <Q> to quit: "
select DB in db1 db2 db3; do
[ "${REPLY^*}" = 'Q' ] && break
echo "Should backup $DB..."
done
expr match "$LIST" '$SOURCE'
don't work because of this function search $SOURCE from begin of the string and return the position just after pattern $SOURCE if found else 0. So you must write another code:
expr match "$LIST" '.*'"$SOURCE" or expr "$LIST" : '.*'"$SOURCE"
The expression $SOURCE must be double quoted so as a parser may set substitution. Single quoted not substitute and the code above will search textual string $SOURCE from the beginning of the $LIST. If you need the beginning of the string subtract the length $SOURCE e.g ${#SOURCE}. You may write also
expr "$LIST" : ".*\($SOURCE\)"
This function just extract $SOURCE from $LIST and return it. You'll get empty string else. But they problem with double double quote. I don't know how it resolve without using additional variable. It's light solution. So you may write in C. There is ready function strstr. Don't use expr index, So is very attractive. But index search not substring and only first char.
You can also compare with wildcards:
if [[ "$LIST" == *"$SOURCE"* ]]
This works in Bash without forking external commands:
function has_substring() {
[[ "$1" != "${2/$1/}" ]]
}
Example usage:
name="hello/world"
if has_substring "$name" "/"
then
echo "Indeed, $name contains a slash!"
fi
expr is used instead of [ rather than inside it, and variables are only expanded inside double quotes, so try this:
if expr match "$LIST" "$SOURCE"; then
But I'm not really clear what SOURCE is supposed to represent.
It looks like your code will read in a pattern from standard input, and exit if it matches a database alias, otherwise it will echo "ok". Is that what you want?
You can use "index" if you only want to find a single character, e.g.:
LIST="server1 server2 server3 server4 server5"
SOURCE="3"
if expr index "$LIST" "$SOURCE"; then
echo "match"
exit -1
else
echo "no match"
fi
Output is:
23
match
If you're using bash you can just say
if grep -q "$SOURCE" <<< "$LIST" ; then
...
fi
Source: Stackoverflow.com