开发者

Parameter not passed through well

I created a function for crypting passwords. But the first parameter is not passed through well. crypt_pass "a" outputs the same as crypt_pass "b". What am I doing wrong?

crypt_pass() {
    echo $(per开发者_Python百科l -e'print crypt($1, "aa")')
}

Regards, Kevin


Without having tested it, my guess would be that inside ' quotes, no variable substitution is performed, and the $1 is passed literally.


Embedding the parameter inside a perl script can lead to trouble if there are characters special to perl in the parameter value. Best to do something like

crypt_pass() {
    echo $(perl -e 'print crypt($ARGV[0], "aa")' "$1")
}


You should enclose the Perl code in double quotes, this way bash can substitute its $1 before the string is passed to the Perl interpreter.

crypt_pass() {
    echo $(perl -e"print crypt($1, \"aa\")")
}

Since in Perl you can use single quotes for a string, you can avoid the escaping by just using single quotes, so it would become a bit cleaner:

crypt_pass() {
    echo $(perl -e"print crypt($1, 'aa')")
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜