Re: Passing variables from shell script to another
- From: floyd@xxxxxxxxxx (Floyd L. Davidson)
- Date: Mon, 31 Mar 2008 07:29:02 -0800
Hi all,
I've just started to learn shell programming. I have 2 scripts and
wont to pass some variables from script1 to script2
exp:
Script1 is:
#!/bin/sh
echo "Enter 3 data"
read tmp1 tmp2 tmp3
. ./script2.sh $tmp1 $tmp2 $tmp3
exit 0
script2 is:
#!/bin/sh
tst_fct (){
var1=$1
var2=$2
var3=$3
echo "var1=$var1, var2=$var2, var3=$var3"
}
exit 0
As noted by Neil, that isn't doing what you were
thinking it was, but there is still even more to
it than mentioned!
Consider that you are "calling" script2.sh with the
"source" command, which means the file script2.sh is
executed by the _current_ shell process. That means
passing the three variables as arguments is redundant,
because of course your shell already knows about them...
You can call tst_fct like this,
tst_fct $tmp1 $tmp2 $tmp3
And it will be the same as using "$1 $2 $3".
If you execute file script2.sh as a sub-shell however,
then you could use it exactly as you have there to pass
the values. You could also export the variables, thus
making them available to sub-shells and avoid passing
them, and then do it the same as for a sourced file.
script2 is:
#!/bin/sh
tst_fct (){
var1=$1
var2=$2
var3=$3
echo "var1=$var1, var2=$var2, var3=$var3"
}
exit 0
When you do call your function, keep in mind that you
have a new, and distinctly different, set of positional
parameters! Outside the function definition, but within
the script, $1 references the first argument to the
script when it is called on the command line. But
inside the function definition that same symbol
references the first argument to the function when it is
called within the script!
Another potential conflict would be the use of the "set"
command within a script, which would change the values
of the positional parameters for the script or function,
depending on where it is used.
Here's a short script, which you can name "foo" and call
as "./foo 1 2 3" to see a demontration of the different
values for positional parameters. Look at the output,
and compare it to where each echo command in the script
is.
It may be confusing at first, but once you figure out
what is happening, it becomes very clear and quite
useful.
#!/bin/bash
if [ $# -lt 3 ]
then
echo "Requires three arguments."; exit 1;
fi
echo -e "script arguments:\t\t\t<${1}> <${2}> <${3}>"
function fct() {
echo -e "fct() arguments:\t\t\t<${1}> <${2}> <${3}>"
fctl="${3} ${2} ${1}"
set ${fctl}
echo -e "fct() arguments after 'set' in 'fct':\
\t<${1}> <${2}> <${3}>"
}
( #Execute this command list in a subshell
echo -e "subshell arguments:\t\t\t<${1}> <${2}> <${3}>"
cmdl="${3} ${2} ${1}"
set ${cmdl}
echo -e "subshell arguments after 'set' call:\
\t<${1}> <${2}> <${3}>"
fct "${1}" "${2}" "${3}"
fct one two three
echo -e "subshell arguments after 'fct' call:\
\t<${1}> <${2}> <${3}>"
)
# back to the parent shell
echo -e "script arguments at end of script:\
\t<${1}> <${2}> <${3}>"
exit 0
--
Floyd L. Davidson <http://www.apaflo.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) floyd@xxxxxxxxxx
Nezhate <mazouz.nezhate@xxxxxxxxx> wrote:
.
as result I got:
$ ./script1.sh
Enter 3 data
foo bar nothing
var1=, var2=, var3=
why this doesn't work ?
thanks in advance
- References:
- Passing variables from shell script to another
- From: Nezhate
- Passing variables from shell script to another
- Prev by Date: Re: Some "media keys" not detected by xev?
- Next by Date: Re: Some "media keys" not detected by xev?
- Previous by thread: Re: Passing variables from shell script to another
- Next by thread: Some "media keys" not detected by xev?
- Index(es):
Relevant Pages
|