[erlang-questions] Re: how do I get the actual list of arguments passed to an escript?

Steve Vinoski vinoski@REDACTED
Mon Apr 4 16:53:20 CEST 2011

On Mon, Apr 4, 2011 at 9:41 AM, Roberto Ostinelli <roberto@REDACTED> wrote:
>>   ~ >./args one two "three  and four are one argument"
>>  Args=["one","two","three","and","four","are","one","argument"]
>> The escript manpage (e.g. http://www.erlang.org/doc/man/escript.html)
>> claims
>>  the main/1 function will be called with a list of strings
>>  representing the arguments given to the script (not changed or
>>  interpreted in any way).
>> which is why I was expecting
>>  Args=["one","two","three  and four are one argument"]
>> How can I get the arguments without Erlang changing and interpreting them?
>> Matt
> ./args one two 'three and four are one argument'

For the purposes of this issue, there's no difference between using
double quotes and single quotes to enclose that final argument. The
primary difference between the quote types has to do with
interpretation of the $ special character -- it's interpreted within
double quotes but not within single quotes. Since that special
character does not appear here, each should work equally well, and on
my machines with R14B02 and bash versions 3.2.48(1)-release and
4.1.5(1)-release, that's exactly the case. Csh does some weird stuff
with quoting when compared to Bourne-derived shells, but for me this
case even works with csh.

The usual cause of this problem is a script that fails to use "$@",
exactly like that, to pass arguments to a secondary program. For
example, save this to /tmp/foo:

echo '$*:'
awk 'BEGIN { for (i = 1; i < ARGC; i++) print ARGV[i] }' </dev/null $*
echo '$@:'
awk 'BEGIN { for (i = 1; i < ARGC; i++) print ARGV[i] }' </dev/null $@
echo '"$@":'
awk 'BEGIN { for (i = 1; i < ARGC; i++) print ARGV[i] }' </dev/null "$@"

and run:

/tmp/foo one two "three four five"
/tmp/foo one two 'three four five'

You'll see that both cases are the same, and in both cases only the
last call to awk maintains whitespace in the third argument.

Since escript is not a shell script, I doubt the lack of "$@" is at
fault here. But escript does do a lot of command-line argument
manipulation, so perhaps it's getting something wrong for Matthias's
case. Like I said above, though, it works fine on my machines (Macbook
Pro 10.6.7 and Ubuntu 10.10, two bash versions as noted above).


More information about the erlang-questions mailing list