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: pass arguments in bash - comp.unix.programmerHello, 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 ... How pass a newline character as an argument to a command? (in bash ...pass arguments in bash - comp.unix.programmer How pass a newline character as an argument to a command? (in bash ... linux - bash: how to pass command line arguments ... how to pass command line arguments to makefile - comp.unix ...pass arguments in bash - comp.unix.programmer how to pass command line arguments to makefile - comp.unix ... pass arguments in bash - comp.unix.programmer how to pass ... How to pass enum as default argument - comp.lang.c++.moderated ...pass arguments in bash - comp.unix.programmer How to pass enum as default argument - comp.lang.c++.moderated ... pass arguments in bash - comp.unix.programmer How to pass ... bash: how to flush the output from command `tee` - comp.unix.shell ...pass arguments in bash - comp.unix.programmer bash: how to flush the output from command `tee` - comp.unix.shell ... pass arguments in bash - comp.unix.programmer bash ... importing functions from an external bash script - comp.unix.shell ...pass arguments in bash - comp.unix.programmer importing functions from an external bash script - comp.unix.shell ... pass arguments in bash - comp.unix.programmer Hello ... expect keys per bash - comp.unix.shell... principle, it is better to use single quotes rather than >> double quotes, *unless* you want to have variables expanded inside >> them, when passing arguments from bash. Pass command line parameters when running an application from a ...how to pass OK prompt boot parameters from a telnet session ... Bash script - telnet - comp.unix.shell Pass command line parameters when running an application from a ... How to set bash IFS to default? - comp.unix.shellpass arguments in bash - comp.unix.programmer Hello, When I worked with bash script, I got stuck with ... output would be: a b1 b2 As a solution, I can use IFS ... instead ... Acrord32 command line arguments - comp.text.pdfpass arguments in bash - comp.unix.programmer Acrord32 command line arguments - comp.text.pdf pass arguments in bash - comp.unix.programmer Acrord32 command line arguments ... How to pass arguments to a Bash-script - Linux Operating System ...You can write a bash script such that it receives arguments specified when the script is called from the command line. This method is used when a script has to ... Pass arguments into a function - Bash Shell Scripting Directory ...Shell functions have their own command line argument. Use variable $1, $2..$n to access argument passed to the function. The syntax is as follows: 7/22/2012 8:21:31 PM
|