Operating System - Linux
1830888 Members
1460 Online
110017 Solutions
New Discussion

Pass a parameter that contains a dollar sign

 
SOLVED
Go to solution
Scott Lindstrom_2
Regular Advisor

Pass a parameter that contains a dollar sign

I have a script that generates an encrypted password (using makekey), then calls another script passing the new encrypted password. I am finding sometimes makekey gens a key that starts with a dollar sign. So I get this situation:

First script calls the second script:

$THqj3LornI/c

The second script echos the parameter received:
+ newpass=/c

Obviously not correct.

I also have this example:

<script name> $TAs5SRJ.tlhQ
+ newpass=.tlhQ

Is the leading dollar sign causing this behavior? If so, do I change the calling script or the called script (and how)?

TIA,

Scott
14 REPLIES 14
A. Clay Stephenson
Acclaimed Contributor

Re: Pass a parameter that contains a dollar sign

Yes, the variable $TAs5SRJ is not defined and is thus a null value.

<script name> "\$TAs5SRJ.tlhQ"

will fix you as the \ is escaping the special meaning of $.
If it ain't broke, I can fix that.
Scott Lindstrom_2
Regular Advisor

Re: Pass a parameter that contains a dollar sign

Thanks! I'll have to test and see if I can always lead with a \ (without testing what the first character of the password is), or if I should only do it if the first character is a $.

Scott
A. Clay Stephenson
Acclaimed Contributor

Re: Pass a parameter that contains a dollar sign

Actually what is have to do is precede EVERY '$' with \ no matter where (or how many times) it occurs in the string.
If it ain't broke, I can fix that.
Scott Lindstrom_2
Regular Advisor

Re: Pass a parameter that contains a dollar sign

That makes it a bit more interesting. Is there an easy way to do that?

For example, if I had:
$hj7f$k38$$l

could I run it through some command to get:

\$hj7f\$k38\$\$l

Scott
A. Clay Stephenson
Acclaimed Contributor

Re: Pass a parameter that contains a dollar sign

The problem that you encounter with trying to create a function is the very same one you now face; how do you pass the paramter. It's easy if it's a constant because quoting will fix you. In your case, perhaps the easist method is to allow a temporary file to serve for you. Rather that capturing the output of makekey in a variable save it to a temporary file. The temp file should be PID dependent so that collisions won't occur and the mode of the file should be very restrictive as well -- although a hash created by makekey is difficult to crack -- assuming non-trivial plaintext is input. You should also have a trap to remove this temp file upon exit or receipt of common signals.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Pass a parameter that contains a dollar sign

Okay I've thought a little bit about it and this technique should work.

Rather than capturing makekey's stdout in a variable directly do something like this:

PLAINTEXT="12345678.k"
PWHASH=$(echo "${PLAINTEXT}" | /usr/lbin/makekey | awk '{gsub("$","\$"); print $0}')
echo "PWHASH = \"${PWHASH}\""


If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Pass a parameter that contains a dollar sign

Hi Scott:

You asked:

/* begin quote */
For example, if I had:
$hj7f$k38$$l

could I run it through some command to get:

\$hj7f\$k38\$\$l

/* end quote */

...Yes, here's a simple way:

# echo '$hj7f$k38$$l'|perl -ne 'print quotemeta'
\$hj7f\$k38\$\$l\

# X=`echo '$hj7f$k38$$l'|perl -ne 'print quotemeta'`; echo ${X}
\$hj7f\$k38\$\$l\

Note that we use single quote marks around the string to prevent the shell from interpreting the dollar-signs.

Regards!

...JRF...
Scott Lindstrom_2
Regular Advisor

Re: Pass a parameter that contains a dollar sign

It looks like I have my morning all set with testing. I will advise on the outcome!

Scott
James R. Ferguson
Acclaimed Contributor

Re: Pass a parameter that contains a dollar sign

Hi (again) Scoot:

One small correction - the addition of the '-l' switch so the snippets read:

# echo '$hj7f$k38$$l'|perl -nle 'print quotemeta'
\$hj7f\$k38\$\$l

(and):

# X=`echo '$hj7f$k38$$l'|perl -nle 'print quotemeta'`; echo ${X}
\$hj7f\$k38\$\$l

Regards!

...JRF...
OldSchool
Honored Contributor

Re: Pass a parameter that contains a dollar sign

or you can run it thru sed,

echo "12345.k" | /usr/lbin/makekey | sed s/\\$/\\\\$/g
TimButler
New Member

Re: Pass a parameter that contains a dollar sign

This thread's a bit old, but the right
solution is vastly simpler than all of the
other suggestions.

When the first script calls the second,
it need only place the encrypted string in
single quotes. This will prevent any attempt
to interpret the argument as a shell variable.

tim
A. Clay Stephenson
Acclaimed Contributor

Re: Pass a parameter that contains a dollar sign

Well Tim, the whole problem is that this is not a constant; that would be easy. Instead it's a variable that may happen to include the "$" metacharacter (or more than 1).
If it ain't broke, I can fix that.
Dennis Handly
Acclaimed Contributor

Re: Pass a parameter that contains a dollar sign

Clay said:
the whole problem is that this is not a constant; that would be easy. Instead it's a variable that may happen to include the "$" metacharacter

Yes, I also thought it was as simple as Tim said.

Here are my two scripts:
$ more dollar_parm
#!/usr/bin/ksh

ABC=$(echo '$def/sam') # generate key
echo $ABC
dollar_parm2 "$ABC" # quotes not needed

$ more dollar_parm2
#!/usr/bin/ksh

echo $1

Both scripts echo: $def/sam

It seems that once you get a $ in a parm, it will stay there. So you should be able to capture the output of makekey and pass that.
Mark Fenton
Esteemed Contributor

Re: Pass a parameter that contains a dollar sign

hmm

I too, think the issue is somewhat easier than Clay avers.

create encrypted password using makekey, assigning same to a variable name. Call subsequent script with this variable as an arguement.

An interactive example:

#!/bin/sh

read password
my_key=`echo ${password}|/usr/lbin/makekey`

;# now call subsequent script with my_key

next_script ${my_key}


next_script then can act on $1 (or whichever arguement was passed)
If you desire to reassign $1 to some other more logically named variable, that is done normally
newpass=$1
when using newpass, just enclose with brackets as was done with my_key in the first script.