Shell脚本中实现字符串变量的去重功能

来源:互联网 发布:python macd代码 编辑:程序博客网 时间:2024/04/28 12:13

网上找了很久都没找到关于字符串变量去重复的Shell脚本,于是根据已有的知识自己实现了一个。
用到的一个关键方法是字符串的截取,该方法参考自林疯子的博客:(转)shell脚本处理字符串常用方法:

${varible#*string}   #从左向右截取第一个string后的字符串

下面是我自己实现的字符串变量去重函数:

purgeDuplicates(){    local uniqs=    local tmps=$1    for su in {$1}; do        tmps=${tmps#*$su}        isuniq=yes        for dd in {$tmps}; do            if [ "$su" = "$dd" ]; then                isuniq=no                break            fi        done        if [ "$isuniq" = "yes" ]; then            if [ -z "$uniqs" ]; then                uniqs=$su            else                uniqs="$uniqs $su"            fi        fi    done    echo $uniqs}
0 0