【发布时间】:2015-06-18 02:19:31
【问题描述】:
我有两个字符串要比较相等的字符,字符串必须包含确切的字符,但 mychars 可以有额外的字符。
mychars="abcdefg"
testone="abcdefgh" # false h is not in mychars
testtwo="abcddabc" # true all char in testtwo are in mychars
function test() {
if each char in $1 is in $2 # PSEUDO CODE
then
return 1
else
return 0
fi
}
if test $testone $mychars; then
echo "All in the string" ;
else ; echo "Not all in the string" ; fi
# should echo "Not all in the string" because the h is not in the string mychars
if test $testtwo $mychars; then
echo "All in the string" ;
else ; echo "Not all in the string" ; fi
# should echo 'All in the string'
最好的方法是什么?我的猜测是遍历第一个参数中的所有字符。
【问题讨论】:
标签: bash shell text-parsing