Operating System - HP-UX
1830218 Members
2069 Online
109999 Solutions
New Discussion

How to pass a name through a variable.

 
SOLVED
Go to solution
Reynaldo Torres
Advisor

How to pass a name through a variable.

Good morning all.

I'm trying to pass a name through a variable for example.

NAME=Robert; export NAME

If I do this from the command line is not a problem, but if I try to pass this in a script, I get the following message. "The parameter is not set" when I recall the variable with the echo command "echo $NAME".
This should be simple, but for some reason I can not make it work. If you can help me, I will really appreciated because I need to pass a few. I will give points to the answers.

Thanks in advance,

RT.
Reynaldo Torres
6 REPLIES 6
Michael Schulte zur Sur
Honored Contributor

Re: How to pass a name through a variable.

Hi Reynaldo,

can you post us, how you do it now?

greetings,

Michael
Steven E. Protter
Exalted Contributor

Re: How to pass a name through a variable.

In shell scripting you can pass variables on the command line as follows

newscriptname $NAME

The value in $NAME will be available to the new script as $1

It can then be transferred to any variable you need as follows:

NAME=$1
export NAME

The script will then be able to use it any way it needs.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Dave La Mar
Honored Contributor

Re: How to pass a name through a variable.

RT -
export NAME=Robert
echo $NAME

Best regards,
dl
"I'm not dumb. I just have a command of thoroughly useless information."
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to pass a name through a variable.

Firstly, you can do this in one step:
export NAME=Robert BUT that is still not going to do what I think you are trying to do. You seem to be trying to export a variable in a child process to a parent. That will never work because the parent never inherits from the child.

What you can do is pass the data through a temporary file or use the "." (dot) to include the script so that it actually runs in the foreground.

e.g.

export NAME=Robert
save this (along with anything else) in a file (e.g. /usr/local/bin/myscript.sh)

Now from your main script simply
. /usr/local/bin/myscript.sh

That will fix you BUT myscript.sh must not contain an exit or return statement because that will have the effect of exiting the foreground process (since they are now one and the same).
If it ain't broke, I can fix that.
John Kittel
Trusted Contributor

Re: How to pass a name through a variable.

Here is what is happening...

When you are running your script, the variable gets defined in a subshell, which is then not available in the original shell.

If you source the script, it will get run in the current shell, and the variable will be defined.

for example, the script I have named t, defines name=fred and exports it.

# cat t
name=fred
export name
#

from the command line I define name=john, export it, then echo it.

# name=john
# export name
# echo $name
john
#

If I run the script in a subshell, name in the current shell remains = john.

# ./t
# echo $name
john
#

If I run the script in the current shell, name will be changed.

# . ./t
# echo $name
fred
#



- John
Umapathy S
Honored Contributor

Re: How to pass a name through a variable.

Clay has given a clear picture.

You can put all the variables inside a script that you want to export and source the same to the shell. It will be available for that shell. For that
. ./<script_having_vars>.sh

You can pass arguments to a shell script also which will be assigned to $1 ... $n depending on the number of args you pass.
HTH,
Umapathy

Arise Awake and Stop NOT till the goal is Reached!