2015-04-12 99 views
1

我有我想比較平等的字符兩個字符串,字符串必須包含確切的字符,但是可以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' 

這樣做的最佳方法是什麼?我的猜測是循環遍歷第一個參數中的所有字符。

回答

2

您可以使用trmychars用符號代替任何字符,那麼你就可以測試,如果得到的字符串是從符號,PE有什麼不同,:

tr -s "[$mychars]" "." <<< "ggaaabbbcdefg" 

輸出:

. 

但是:

tr -s "[$mychars]" "." <<< "xxxggaaabbbcdefgxxx" 

打印:

xxx.xxx 

所以,你的函數可以是這樣的:

function test() { 
    local dictionary="$1" 
    local res=$(tr -s "[$dictionary]" "." <<< "$2") 
    if [ "$res" == "." ]; then 
     return 1 
    else 
     return 0 
    fi 
} 

更新:正如@mklement0建議,全功能可通過縮短(和邏輯固定)以下:

function test() { 
    local dictionary="$1" 
    [[ '.' == $(tr -s "[$dictionary]" "." <<< "$2") ]] 
} 
+1

謝謝,我更新了回答您的建議 – higuaro

0

accepted answer's solution短,聰明,高效

這裏有一個效率較低的替代,這可能會感興趣,如果你想知道哪些字符是唯一的第一串,返回一個排序,不同的列表:

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 

注我已經改名test()charTest()避免名稱衝突與test內置/實用

  • sed 's/\(.\)/\1\'$'\n''/g'通過將每一個單獨的行將輸入到單個字符。
    • 注意,命令創建在最後一個額外的空行,但並不在這種情況下重要;消除它,追加; ${s/\n$//;}sed腳本。
    • 該命令被寫在一個符合POSIX標準的方式,這複雜化了,由於具有在\轉義實際換行符來拼接(經由ANSI C-引用字符串,$\n');如果你有GNUsed,則可以簡化爲sed -r 's/(.)/\1\n/g
  • sort -u然後排序結果字符和雜草進行重複(-u)的列表。
  • comm -23比較不同組在兩個串排序的字符,並打印那些特有的第一串(comm採用了3列布局,用含有特有的第一文件中的行的第一列中,含有行第2列特有的第二列,第3列印刷線兩個輸入文件的共同點; -23抑制第二和第三列,僅有效地打印所特有的所述第一輸入端的線)。
  • [[ -z $charsUniqueToStr1 ]]然後如果測試$charsUniqueToStr1爲空(-z);
    換句話說:成功(退出代碼0)表示,如果第一個字符串不包含字符。不包含在第二個字符串中;否則,失敗(退出代碼1);由於條件([[ .. ]])成爲全功能的最後聲明,其退出代碼也成爲功能的退出代碼。
相關問題