bashshell测试一个字符串中的所有字符是否都在另一个字符串中
我有两个字符串,我想比较相等的字符,字符串必须包含确切的字符,但mychars可以有额外的字符bashshell测试一个字符串中的所有字符是否都在另一个字符串中,bash,shell,text-parsing,Bash,Shell,Text Parsing,我有两个字符串,我想比较相等的字符,字符串必须包含确切的字符,但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 C
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'
最好的方法是什么?我的猜测是循环第一个参数中的所有字符。您可以使用
tr
将mychars
中的任何字符替换为一个符号,然后您可以测试生成的字符串是否与符号有任何不同,p.e.:
tr -s "[$mychars]" "." <<< "ggaaabbbcdefg"
但是:
因此,您的函数可以如下所示:
function test() {
local dictionary="$1"
local res=$(tr -s "[$dictionary]" "." <<< "$2")
if [ "$res" == "." ]; then
return 1
else
return 0
fi
}
function test() {
local dictionary="$1"
[[ '.' == $(tr -s "[$dictionary]" "." <<< "$2") ]]
}
功能测试(){
本地字典=“$1”
本地res=$(tr-s“[$dictionary]”.“The简短、聪明、高效
下面是一个效率较低的备选方案,如果您想知道第一个字符串的唯一字符是什么,则可能会感兴趣,该字符串作为排序的、不同的列表返回:
charTest() {
local charsUniqueToStr1
# Determine which chars. in $1 aren't in $2.
# This returns a sorted, distinct list of chars., each on its own line.
charsUniqueToStr1=$(comm -23 \
<(sed 's/\(.\)/\1\'$'\n''/g' <<<"$1" | sort -u) \
<(sed 's/\(.\)/\1\'$'\n''/g' <<<"$2" | sort -u))
# The test succeeds if there are no chars. in $1 that aren't also in $2.
[[ -z $charsUniqueToStr1 ]]
}
mychars="abcdefg" # define reference string
charTest "abcdefgh" "$mychars"
echo $? # print exit code: 1 - 'h' is not in reference string
charTest "abcddabc" "$mychars"
echo $? # print exit code: 0 - all chars. are in reference string
charTest(){
本地charsUniqueToStr1
#确定$1中的哪些字符不在$2中。
#这将返回一个排序的、不同的字符列表,每个字符在其自己的行上。
charsUniqueToStr1=$(通信-23\
谢谢,我更新了答案,加入了你的建议
function test() {
local dictionary="$1"
local res=$(tr -s "[$dictionary]" "." <<< "$2")
if [ "$res" == "." ]; then
return 1
else
return 0
fi
}
function test() {
local dictionary="$1"
[[ '.' == $(tr -s "[$dictionary]" "." <<< "$2") ]]
}
charTest() {
local charsUniqueToStr1
# Determine which chars. in $1 aren't in $2.
# This returns a sorted, distinct list of chars., each on its own line.
charsUniqueToStr1=$(comm -23 \
<(sed 's/\(.\)/\1\'$'\n''/g' <<<"$1" | sort -u) \
<(sed 's/\(.\)/\1\'$'\n''/g' <<<"$2" | sort -u))
# The test succeeds if there are no chars. in $1 that aren't also in $2.
[[ -z $charsUniqueToStr1 ]]
}
mychars="abcdefg" # define reference string
charTest "abcdefgh" "$mychars"
echo $? # print exit code: 1 - 'h' is not in reference string
charTest "abcddabc" "$mychars"
echo $? # print exit code: 0 - all chars. are in reference string