how to reference bash variables inside perl script inside bash script?

Eric Shubert ejs at shubes.net
Thu Jun 24 08:02:35 MST 2010


Steven A. DuChene wrote:
> I am attempting to write what I thought would be a simple wrapper
> script around ldapmodify that would allow me to easily reset a user's
> password. Inside my bash based wrapper script is a single line of perl
> that encrypts the password I input and returns it as a specially formatted
> string that then gets output to a LDIF file that is eventually sourced by
> ldapmodify.
> 
> Then I wanted to be able to run the resulting script by doing:
> 
> reset_password george newpasswd
> 
> to reset the password in ldap to newpasswd for user account george
> 
> My problem is I want to be able to pick up $2 from the command line invocation
> of my bash script for the desired password and then pass that into the one line
> perl piece. But with all of the single quotes, double quotes, back ticks and
> etc. I am not able to get the password value in the $2 to be correctly passed
> into the perl one line part of the bash script.
> 
> The core part of the script I am having a problem with looks like:
> 
> #!/bin/bash
> 
> OPASS=$2
> 
> PASS=`perl -e 'print("userPassword: {CRYPT}".crypt("$OPASS","frat-salt")."\n");'`
> 
> 
> The actual value of $PASS is supposed to end up looking like:
> 
> userPassword: {CRYPT}frJTbxR6L.kgA
> 
> I have looked at doing something like this:
> 
> #!/bin/bash
> 
> perl << 'EOF'
> print("userPassword: {CRYPT}".crypt("$OPASS","frat-salt")."\n");
> EOF
> 
> but I still don't think the bash variable OPASS would get properly evaluated in the perl invocation.
> 
> Any ideas?
> --
> Steven DuChene
> 
> 

Stuff inside of single quotes won't have variables expanded:
shubes at edwin:~$ OPASS=test
shubes at edwin:~$ echo "$OPASS"
test
shubes at edwin:~$ echo '$OPASS'
$OPASS
shubes at edwin:~$

So you need to have the -e argument surrounded by double-quotes. I don't 
know if the perl code will allow you to use single quotes in place of 
double quotes or not. If it does, then maybe use single quotes there for 
readability. Otherwise, and in the case of the $OPASS parameter, you'll 
need to escape the double quotes individually, as in:
PASS=`perl -e "print(\"userPassword: 
{CRYPT}\".crypt(\"$OPASS\",\"frat-salt\").\"\n\");'`

A bit messy, but I think it would work. ;)

-- 
-Eric 'shubes'



More information about the PLUG-discuss mailing list