For example:
In Perl:
@array = (1,2,3);
system ("/tmp/a.sh @array" );
In my shell script, how do I handle this array in shell script? How do I handle the shell script to receive the arguments, and how do I use that array variable in shell script?
This:
is equivalent to the shell command:
you can see this by simply printing out what you pass to system:
a.shshould handle them like any other set of shell arguments.To be safe, you should bypass the shell and pass the array in as arguments directly:
Doing this passes each element of
@arrayin as a separate argument rather than as a space separated string. This is important if the values in@arraycontain spaces, for example:where
count_args.shis:you’ll see that in the first one it gets 6 arguments and the second it gets 2.
A short tutorial on handling arguments in a shell program can be found here.
Anyhow, why write one program in Perl and one in shell? It increases complexity to use two languages and shell has no debugger. Write them both in Perl. Better yet, write it as a function in the Perl program.