1753789 Members
7540 Online
108799 Solutions
New Discussion юеВ

Re: To pass a paramenter

 
chinnaga
Advisor

To pass a paramenter

Hi Guys,

I have a problem to figure out how to pass parameters.

How do I pass value from one shell script to another shell script. The scripts are in C shell.

These are 2 different scripts which will be invoked separetely.

Could you pls help me with this.

Thanks in advance
7 REPLIES 7
Peter Godron
Honored Contributor

Re: To pass a paramenter

Hi,
something like:
$ cat a.sh
#!/usr/bin/sh
echo "this is shell 1 : parameter $1"
b.sh "$1"
echo "this is shell 1 again"

$ cat b.sh
#!/usr/bin/sh
echo "this is shell 2 : parameter $1"

example:
$ ./a.sh 4
this is shell 1 : parameter 4
this is shell 2 : parameter 4
this is shell 1 again

Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.

So far you have only awarded points to one of 10 answers !
chinnaga
Advisor

Re: To pass a paramenter

Hi Peter,

Thanks for u'r answer....but my scripts are called seprately.

For example

a.sh
set var = 1
echo $var

now I want to use this $var in another shell say b.sh, b.sh is not inside a.sh.

Can I do this in C shell scripts.
Dennis Handly
Acclaimed Contributor

Re: To pass a paramenter

>now I want to use this $var in another shell say b.sh, b.sh is not inside a.sh.

You can't do this with scripts. You can only do this if you source the file. And you need to be careful what you add to these files.

>Can I do this in C shell scripts?

The real shell syntax is:
$ . a.sh

The scummy C shell syntax is:
% source a.sh
spex
Honored Contributor

Re: To pass a paramenter

Hello,

Use a temp file as an intermediary. Have script1 write the value of $var to the file, and have check if the temp file exists, and assign the stored value to $var if it does.

PCS

Peter Nikitka
Honored Contributor

Re: To pass a paramenter

Hi,

a more detailled example for csh (I recommend using tcsh - available at the porting center - as a compatible superset of csh).

a.csh:
set avar=$1
echo "set avar=$avar" >! /tmp/myavar
...

b.csh:
if (-f /tmp/myavar) then
source /tmp/myavar
else
exit 1
endif

set bvar=hello.$avar
echo $bvar
...


mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
chinnaga
Advisor

Re: To pass a paramenter

Thank you guys ... for the answers. I will use the method to store it in a intermdiate file and then read the value in the next shell.
Dennis Handly
Acclaimed Contributor

Re: To pass a paramenter

You have only assigned points to 1 out of 13 responses. You really should open them all up and assign them as appropriate:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33