我有我想比較平等的字符兩個字符串,字符串必須包含確切的字符,但是可以mychars有多餘的字符。Bash shell中測試,如果在一個字符串中的所有字符在另一個字符串
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'
這樣做的最佳方法是什麼?我的猜測是循環遍歷第一個參數中的所有字符。
謝謝,我更新了回答您的建議 – higuaro