www.data-blogger.com
www.data-blogger.com

Reputation: 4164

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 $(perl -e'print crypt($1, "aa")')
}

Regards, Kevin

Upvotes: 1

Views: 84

Answers (3)

glenn jackman
glenn jackman

Reputation: 246847

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")
}

Upvotes: 1

Blagovest Buyukliev
Blagovest Buyukliev

Reputation: 43508

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')")
}

Upvotes: 1

Simon Richter
Simon Richter

Reputation: 29588

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

Upvotes: 2

Related Questions