Reputation: 577
I have a perl script test.pl which reads arguments from the command line
#!/usr/bin/perl -w
for( @ARGV ) { print( "$_\n" ) } ;
This works well when I pass parameters like "a b" "c d" e on the command line
The result is:
a b
c d
e
If I declare a variable
X='"a b" "c d" e'
and then run
test.pl $X
I get
"a
b"
"c
d"
e
I need to run the perl script from various shell scripts as a helper function and the parameters are calculated by those scripts.
The parameter count is not fixed so I have to pass it as a list.
Alas I cannot find a way to get the perl program handle my parameters as desired.
I have thought of passing the parameters through file but then the manual invocation of the perl script from the command line becomes awkward.
How can I get the perl script preserve the spaces in the paramters?
Upvotes: 4
Views: 1940
Reputation: 577
Thanks @all for the hints. Parameter passing from shells seems to be not as easy as I had thought. Thus I have added another parameter to my perl program, which tells it to read the parameters from a file instead of the command line. This works like desired and circumvents shell interpretations.
Upvotes: 0
Reputation: 2331
The problem is the shell. executing
./test.pl "$X"
will give one argument to the shell. One solution would be to use
/bin/bash -c "./test.pl $X"
HTH Georg
Upvotes: 2
Reputation: 78105
If you're using bash you might consider using an array variable to hold the arguments to prevent the loss of information when the variable is substituted into the test.pl
command invocation:
declare -a X=("a b" "c d" e)
test.pl "${X[@]}"
If it helps, you can build up the array piece-by-piece:
X=("a b")
X+=("c d")
X+=("e")
Upvotes: 2