1

I'm having some troubles trying to insert a bash variable inside my perl code. The purpose of the code is to execute a certain perl printing command into a terminal using a bash script file. I have simplified my problem to the code below:

i=5
myvar=$(perl -e 'print "AAA" x ($i)')
echo "$myvar"

Having a variable i with 5 as its value, I want to print the string "AAA" as many times as the value of i, which is 5 in this example. To do this I want to use the given perl code. Currently, the perl code cannot read the content from my variable i. This is most likely due to the fact that " " is not being used for the part where I ask the value of i.

Switching the ' and the " is not really a posibility as the String "AAA" will be replaced by hexadecimal codes to be used for shellcode. Using the $ to execute the perl code is required to execute it in the terminal as an argument of a function. And of course, the value of i will increment/change in a loop so I cannot just use 5 instead of i.

Any help to make the perl code able to read the value of i or to work around this problem of the annotations?

1

Use this to enable variable expansion:

myvar=$(perl -e 'print "AAA" x ('"$i"')')
  • Thx, this works indeed! Must of been too frustrated with my appointment and trying to figure out how the script had to be written to see this solution as it is quite simple and recognizable! – Whitekang Dec 9 '15 at 20:04
  • Thank you very much, I was struggling with this. In hindsight it's semi-obvious. – user7000 Nov 20 '16 at 4:19
1

Perl has a rudimentary form of option parsing built-in. It works much like awk's -v option, but it's uglier

myvar=$(perl -s -e 'print "AAA" x ($j)' -- -j="$i")

That passes the shell's $i into perl as a perl variable $j. The double hyphen is required so that the -j is not gobbled up by the perl binary itself.

0

The problem is the type of quoting you're using. Single quotes cause shell variable expansion not to be done.

i=5
myvar=$(perl -e "print 'AAA' x ($i)")
echo "$myvar"

This code works as intended.

  • Indeed, it works as intended for the example but I cannot use it in my terminal as this way my hexadecimal code won't be converted to shellcode. For example switching the string AAA with \x90 will just make it print \x90 instead of the NOP – Whitekang Dec 9 '15 at 20:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.