2

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?

4
  • You're going to have to give some more detail. What do you mean by "handle"? What do you expect the shell script to receive as arguments? Commented May 26, 2010 at 8:24
  • I dont know to how recive the perl array arguments Commented May 26, 2010 at 8:25
  • do you want to pass all the array elements to bash script? Commented May 26, 2010 at 8:26
  • Yes . I want to pass all elements into bash script Commented May 26, 2010 at 8:30

4 Answers 4

5

This:

my @array = (1,2,3);   
system ("/tmp/a.sh @array" );

is equivalent to the shell command:

/tmp/a.sh 1 2 3

you can see this by simply printing out what you pass to system:

print "/tmp/a.sh @array";

a.sh should 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:

system "/tmp/a.sh", @array;

Doing this passes each element of @array in as a separate argument rather than as a space separated string. This is important if the values in @array contain spaces, for example:

my @array = ("Hello, world", "This is one argument");
system "./count_args.sh @array";
system "./count_args.sh", @array;

where count_args.sh is:

#!/bin/sh

echo "$# arguments"

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Does passing as separate arguments to system also help when there are other "special" characters like quotes? I have a syscall right now that is just escaping all the special characters to pass as a single string to system, but I'm wondering if passing as distinct args to system() would make it work w/o the escapes.
@Herms: Yes, by passing an array instead of a single string, you don't have to "escape" or quote the arguments.
2

The shell script receives its arguments in $@:

#!/bin/sh
# also works for Bash
for arg  # "in $@" is implied
do
    echo "$arg"
done

In Bash, they can also be accessed using array subscripts or slicing:

#!/bin/bash
echo "${@:2:1}"    # second argument
args=($@)
echo "${args[2]}"  # second argument
echo "${@: -1}"    # last argument
echo "${@:$#}"     # last argument

Comments

1

like this:

in Perl:

   @a=qw/aa bb cc/;
   system("a.sh ".join(' ',@a));

in shell (a.sh):

#!/bin/sh
i=1;
while test "$1"
do
    echo argument number $i = $1
    shift
    i=`expr $i + 1`
done

2 Comments

It's better to use the list form of Perl's system. You can just say system( 'a.sh', @a )
It's better to use the list form of system unless you actually want shell interpolation of your arguments. In that case, it's probably better to design your code so that you don't need it and can use the list form of system.
-2

if you want to pass all array elements once:

my @array = qw(1 2 3);

system("/tmp/a.sh". join(" ", @array));

If you want to pass array elements one by one:

my @array = qw(1 2 3);
foreach my $arr (@array) {
     system("/tmp/a.sh $arr");
}

3 Comments

How to handle this array variable in shell script ?
Unless someone has mucked with $", "@array" is equivalent to join(" ", @array)
You don't need that join, and you should use system in the list form.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.