SHELL传参注意事项,尽量使用双引号传参

#!/bin/bash
function convert(){
        keyword=${1//\\/\\\\\\\\}
        keyword=${keyword//\./\\.}
        keyword=${keyword//\*/\\\\*}
        keyword=${keyword//\&/\\\\&}
        keyword=${keyword//\$/\\\\$}
        keyword=${keyword//\+/\\\\+}
        keyword=${keyword//\=/\\\\=}
        keyword=${keyword//\^/\\\\^}
        keyword=${keyword//\%/\\\\%}
        keyword=${keyword//\!/\\\\!}
        keyword=${keyword//\@/\\\\@}
        keyword=${keyword//\#/\\\\#}
        keyword=${keyword//\[/\\\\[}
        keyword=${keyword//\]/\\\\]}
        keyword=${keyword//\-/\\\\-}
        keyword=${keyword//\ /\\ }
#       keyword=${keyword//\?/\\\\?}    ?号不能转义.要不然替换不成功
        keyword=${keyword//\"/\\\"}
        echo $keyword
}

#调用函数,如果传参有空格,必须加双引号,否则函数会认为是多个参数
repkey=$(convert "$2")
repkey1=$(convert "$2")
newkey=$(convert "$3")

#调用shell脚本
sh xxx.sh /root "rep1" "rep2" "hh" 0

 

posted @ 2019-11-10 18:27  赵海博运维笔记  阅读(2258)  评论(0编辑  收藏  举报