pass arguments in bash

  • Follow


Hello, 

When I worked with bash script, I got stuck with a problem which is
about passing arguments between scripts. Please look at below, there
are two simple  scripts named "test.sh" and "sub_test.sh", and
"sub_test.sh" will be invoked in "test.sh".

#### test.sh ######
#!/bin/sh
SAVED_ARGS="$@"
../sub_test.sh $SAVED_ARGS

#### sub_test.sh #######
#!/bin/sh
while [ $# != 0 ]; do
    echo $1;
    shift;
done

Run "test.sh" with this command line:
  $ ./test.sh a "b1 b2"
I got this output:
a
b1
b2

We can see that when executes "./sub_test.sh $SAVED_ARGS", the two
arguments from "test.sh" is be interpreted into three arguments for
"sub_test.sh". But ideally, I still want that "sub_test.sh" gets the
original *two* arguments. And the output would be:
a
b1 b2

As a solution, I can use IFS variable. And the "test.sh" can be
rewritten as below:

##### test.sh #####
#!/bin/sh
IFS="
"
SAVED_ARGS="$@"
../sub_test.sh $SAVED_ARGS

But seems that IFS is not a reasonable way, as it is a big problem for
security. And can not work in (my current) Solaris platform. So, does
anybody have good idea for this?

Thanks a lot in advance!
- Wipall
0
Reply wipall 9/3/2004 8:27:03 AM

wipall <wipall@gmail.com> wrote:
> But seems that IFS is not a reasonable way, as it is a big problem for
> security. And can not work in (my current) Solaris platform. So, does
> anybody have good idea for this?

Have you tried Sun tech support?  They would be the best source for this
kind of security problem with Solaris.  After all, it's their machine,
and you're paying for their support.

-- 
William Park <opengeometry@yahoo.ca>
Open Geometry Consulting, Toronto, Canada
0
Reply William 9/3/2004 9:09:00 AM


2004-09-3, 01:27(-07), wipall:
> Hello, 
>
> When I worked with bash script, I got stuck with a problem which is
> about passing arguments between scripts. Please look at below, there
> are two simple  scripts named "test.sh" and "sub_test.sh", and
> "sub_test.sh" will be invoked in "test.sh".
>
> #### test.sh ######
> #!/bin/sh
> SAVED_ARGS="$@"
> ./sub_test.sh $SAVED_ARGS
[...]

In bash 3 and above, you could use arrays:

SAVED_ARGS=("$@")

(it doesn't work properly in previous versions because of a bug
when arguments may be of the form [<n>]=foo).

With POSIX and Bourne shells, best is to use functions when you
need to modify the positional parameters.

instead of:

#! /bin/sh
SAVED_ARGS="$*"

set a b c # altering positional parameters

set -f
../sub_test.sh $SAVED_ARGS

Do:

#! /bin/sh

my_altering_function() {
set a b c # altering positional parameters
}
my_altering_function

../sub_test.sh "$@"

You can also save the arguments this way:

SAVED_ARGS=`nawk "
  BEGIN {
    for (i=1; i < ARGC; i++) {
      gsub(/'/, \"'\\\\\\\\''\",ARGV[i])
      printf \"'%s' \", ARGV[i]
    }
    exit
  }" "$@"`

Then, to restore them:

eval "set x $SAVED_ARGS"; shift

And remember to use "$@" to pass them around.

-- 
Stephane
0
Reply Stephane 9/3/2004 11:21:57 AM

2 Replies
355 Views

(page loaded in 0.067 seconds)

Similiar Articles:













7/22/2012 8:21:31 PM


Reply: