简体   繁体   中英

Perl substitution output drops characters from Bash script input

I have a variable in a bash script with a length of 64 characters

authkey=$(LC_ALL=C tr -cd 'a-zA-Z0-9,;.:_#*+~!@$%&()=?{[]}|><-' < /dev/random | head -c 64)

if i parse the variable to perl to do a string substitution

perl -pi -e "s/'AUTH_KEY',         'put your unique phrase here'/'AUTH_KEY',         '$authkey'/" test.txt

depending on the selected random characters the length of the output differs.The output looks the following (The first string is the output in the resulting text file, the second string is the echo'ed output of the variable in the bash script)

q=dB7oUz59.IDBXI:i>ckW4oy3smX&k:-C.[rIf*9w}H=(N93yiB&nk{fP:y0_
q=dB7oUz59.IDBXI:i>ckW4oy3smX&k$s:-C.[rIf*9w}H=(N93yiB&nk{fP:y0_

5A+BwP~l3~<evp.ciTkMYtvmPjyMrL=):Qj1VaMI(,TSS,ZGMcd.m,4W
5A+BwP~l3~<evp.ciTkMYtvmPjyMrL=):Qj1VaMI(@Dk7UNgs,TSS,ZGMcd.m,4W

dX73}i5G1d;L*J=60WHHe<!61Ji_KJ)T5B~b2bCfaNDjBQr_N]}3HS=;GzAaX<gB
dX73}i5G1d;L*J=60WHHe<!61Ji_KJ)T5B~b2bCfaNDjBQr_N]}3HS=;GzAaX<gB

6Ndn(9+:>(6>*rh?B.m),3POp)>sfm8c1rh9vXr~fzZj;]!)kf3#60=M
6Ndn(9+:>(6>*rh?B.m),3POp)>sfm8c1rh9vXr~fzZj;]@YH!)kf3#$=$$ckt=M

FYMI,K|6WutC&dr-3]6)f(>QU-~{vBX>n!J-zq:kK84T|fZ7UW:{1&qU[nwYZLmC
FYMI,K|6WutC&dr-3]6)f(>QU-~{vBX>n!J-zq:kK84T|fZ7UW:{1&qU[nwYZLmC

5A+BwP~l3~<evp.ciTkMYtvmPjyMrL=):Qj1VaMI(,TSS,ZGMcd.m,4W
5A+BwP~l3~<evp.ciTkMYtvmPjyMrL=):Qj1VaMI(@Dk7UNgs,TSS,ZGMcd.m,4W

v1FR8c8}dZD(QGwOrr%M{FSUw*?h.JGI?Ay4tgRVp~l7C5eAxW<w<;c}emeX#S
v1FR8c8}dZD(QGwOrr%M{FSUw*?h.JGI?Ay4tgRVp@s~l7C5eAxW<w<;c}emeX#S

+MGg0=*NrhJ}.qPkk6v[lc)J.uiW1o?LL5t<HTC#Q-hSeqn%-ke!cwL5tk[e
+MGg$|=*NrhJ}.qPkk6v[lc)J.uiW1o?L$55L5t<HTC#Q-hSeqn%-ke!cwL5tk[e

each character dropout was caused by either a $ or @ at the beginning of the group of characters. Is there a way to prevent that behaviour? Best regards Ralf

Using a single quote ' as the delimiter instead of slash / for the substitution suppresses variable interpolation

$ foobar=\$bar; perl -p -e "s'foo'$foobar'"
xx
xx
foo
$bar
^C
$ 

Unfortunately the single quotes that are matched in the substitution now need escaping

foobar=\$bar; perl -p -e "s'\'foo'$foobar\''"
x
x
'foo
$bar'
^C

But that seems to get passed through to Perl OK, without munging the authkey contents with sed

您可以在调用perl之前转义$@

 authey=$(echo -n "$authkey" | sed -re 's/(\$|\@)/\\\1/g')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM