I am running Kubuntu 20.04. The perl version is shown by perl -v to be
perl 5, version 30, subversion 0 (v5.30.0) built for x86_64-linux-gnu-thread-multi (with 50 registered patches, see perl -V ...
I have made apt-get update and with Muon package manager I have all installed perl packages upgraded.
But after that, perl is 5 as before, I have not got perl 6.
I need to use the command qx{..}, i.e. backtick command in order to get the result of linux xxd utility.
On that way I use in a konsole terminal the shell bash with following script, with intentionally an error in the variable $WEx
:
perl -we '$WEx=q{"date|"<>"}; print "\nWEx: ~", $WEx, "~\n\n"; $WEa=<<"EOT";
echo -n \\\\\\"$WEx\\\\\\";
EOT
print "\nWEa: ~", $WEa, "~\n";
print qx! $WEa !, "\n\n";'
The result are these lines:
two lines showing WEx
and WEa
and an error message:
WEx: ~"date|"<>"~
WEa: ~echo -n \\\""date|"<>"\\\";
~
sh: 2: Syntax error: Unterminated quoted string
==
The error message comes from this this code:
qx! $WEa !
That means, in a spawned shell the command line contained in the variable $WEa is to be executed.
i.e. this shell command:
echo -n \\\""date|"<>"\\\";
i.e. the shell should execute the command "echo", and "echo" has to output the text in the argument that follows.
"echo" should output the text literally without regard what it is, if a command or not, if a command with failure.
But instead, the text is executed as if being a command, and because the false double quote, the error message is given.
I have tried many days with various writings and various codes.
The shell allways cannot handle the line given by the backtick command correcly.
What I am making false?
CodePudding user response:
echo -n \\\""date|"<>"\\\";
is not a valid shell command and the shell is handling it correctly when it announces a syntax error. How to fix the problem depends on what you are trying to do. That is not clear. (Also unclear is what this has to do with getting the result of xxd
.)
One possibility is that you are trying to generate a shell command that will print the value of the $WEx
Perl variable. If that is the case, the easiest shell code to use is
echo -n '"date|"<>"'
(a semicolon is not needed at the end in shell code). You need to modify your Perl code to put this code in the $WEa
variable. Unfortunately, modifying the existing code to add single quotes is a bit fiddly because it's all inside shell single quotes. See How to escape single quotes within single quoted strings for details of how to do it.
Possible code (without the debugging output) is:
perl -we '$WEx=q{"date|"<>"};
$WEa=qq{echo -n '\''$WEx'\''};
print qx{$WEa}, "\n\n";'
CodePudding user response:
Hurra, thank you very much, we have a solution. It is given by "pjh" with given code called
Possible code (without the debugging output) is:...
The reason of my trials was on the way to pass a perl variable with an arbitrary text into a backtick command where it is piped to shell utility xxd.
Here is my working version using the given code:
perl -we '$WEx=q{"date|"<>"}; $WEa=qq{echo -n '''$WEx''' | xxd -g1}; print qx{$WEa}, "\n\n";'
This works, gives the correct hex code.
The key of the solution and my rationale is: despite of the fact that the total perl code is delimited by single quotes, inside of the perl code single quotes can be used if in qq{}.
Thank you very mutch. antondhidh