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

 

猜你喜欢

转载自www.cnblogs.com/bretzhao/p/11831208.html