perl and bash variable substitution, with hexadecimal characters and repeat count

不打扰是莪最后的温柔 提交于 2020-02-29 06:32:06

问题


alex@d120432:~$ echo $0
bash
alex@d120432:~$ perl -e 'print "\x41\x42\x43\x44\x0a"x2'
ABCD
ABCD
alex@d120432:~$ i=2
alex@d120432:~$ echo $i
2
alex@d120432:~$ perl -e 'print "\x41\x42\x43\x44\x0a" x $i'
alex@d120432:~$

Is it possible to get the same output in the second perl command? I cannot find correct syntax to use i as repeat count.


回答1:


You will have to escape inner double quotes and use double quotes in shell for variable expansion:

perl -e "print \"\x41\x42\x43\x44\x0a\" x $i"

ABCD
ABCD

Following variant may also work:

perl -e 'print "\x41\x42\x43\x44\x0a" x '$i



回答2:


While Perl uses the double-quote for interpolation, just like the shell, Perl provides the qq{} alternative.

The {} delimiters may be any pair you wish. Using this construct can eliminate the need to escape quote characters in the situation described; for example:

perl -e "print qq(\x41\x42\x43\x44\x0a) x $i"


来源:https://stackoverflow.com/questions/60074869/perl-and-bash-variable-substitution-with-hexadecimal-characters-and-repeat-coun

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!