Operating System - HP-UX
1833877 Members
1630 Online
110063 Solutions
New Discussion

How set variable in scripts?

 
SOLVED
Go to solution
N Gopal
Frequent Advisor

How set variable in scripts?

Hi,

I am running one scripts:

myscripts.scr

in this scripts i am caling one other scripts
say "otherscript.scr"
which should set some flag say
set_flag=1 or 0 as per condition.

After that i want to use this flag again in main script(myscripts.scr) like
if [ set_flag = 1 ] ; then
echo ......
fi;

Basically it is something like
myscripts.scr
(

...code..
otherscript.scr
(
this will set flag
)

if set_flag = 1 then ...

how to define this set_flag variable. If i export it then it will not be updated by inside script.

Can some one please suggest how to do this?

Thanks


5 REPLIES 5
Sandman!
Honored Contributor

Re: How set variable in scripts?

If I understand your requirement correctly then within your script you can do something like...

let set_flag=1

if [ $set_flag -eq 1 ]; then
echo set_flag is 1
else
echo set_flag is $set_flag
fi
N Gopal
Frequent Advisor

Re: How set variable in scripts?

Hi Sand,

I will explain my question again.

run myscripts.scr (

..........code start......

call other script.scr
(
------code start-----
if today is first working day then
set_flag = 1
else set_flag=0
-----code completed-----
)

---control back to main scripts

if [ set_flag = 1 ]; then

echo "it is first working day"
else
echo "it is not first working day"


...........code completed ......

Then how to define set_flag in main code so that it can be changed by inside script and result be available again for main script.

Thanks

Sandman!
Honored Contributor
Solution

Re: How set variable in scripts?

Well I see the folly I have made. IMHO when returning from the called script i.e. otherscript.scr code it so that you return the value of the desired variable as in:

myscript.scr
(
...
otherscript.scr
set_flag=$?
if [ $set_flag -eq 1 ]; then
echo "it is first working day"
else
echo "it is not first working day"
fi
...
)

otherscript.scr
(
...
if [ today is first working day ]; then
let set_flag=1
else
let set_flag=0
fi
...
return $set_flag
)
Dennis Handly
Acclaimed Contributor

Re: How set variable in scripts?

As Sandman said, you can return values from scripts in 2 ways. (Three if you want to use a file.)

You can set the exit status with return and test $?.
You can echo something and capture the output in the caller:
var=$(therscript.scr)
N Gopal
Frequent Advisor

Re: How set variable in scripts?

Thanks to all,

I got answer from Sendman's reply. My problem got solved.

Thanks again