I have two scripts: one main script that calls a second one repeatedly.
On the second one I exported a variable COUNT but on every call to the second script the variable is reinitialized.
The idea is that on every call to the second script the COUNT is incremented but I cannot read the last value.
I used this syntax:
declare -i COUNT
export COUNT
Why this kind of behavior?
How can i solve this problem.
I think I understand.
So If I wanna remember the value of the variable I have to save it to a file or something.I thought that exporting the variable in the first script will remember the variable value between to separate runs.
Generally you should not think of scripts as sharing a common variable space. This is because scripts are separate processes, which are quite different animals to procedures or functions in other programming languages.
The shell language allows you to define functions, you should try those.
No, you can do that differently. When you exit from the nested (child)
script you could have it return, as its return code or other output, the
value to be set back on the source script. For example:
<code filename=“script0.sh”>
#!/bin/bash
export TESTME=0
SCRIPT0RETURN=0
echo $TESTME
let TESTME=$TESTME+1
echo $TESTME
let TESTME=$TESTME+1
…/script1.sh
SCRIPT0RETURN=$?
let TESTME=$TESTME+$SCRIPT0RETURN
echo $TESTME
…/script1.sh
SCRIPT0RETURN=$?
let TESTME=$TESTME+$SCRIPT0RETURN
echo $TESTME
…/script1.sh
SCRIPT0RETURN=$?
let TESTME=$TESTME+$SCRIPT0RETURN
echo $TESTME
</code>
<code filename=“script1.sh”>
#!/bin/bash
echo script1 start $TESTME
let TESTME=$TESTME+1
echo script1 end and return $TESTME
exit $TESTME
</code>
The following output is created when running ./script0.sh:
<quote>
ab@mybox:~/Desktop> ./script0.sh
0
1
script1 start 2
script1 end and return 3
5
script1 start 5
script1 end and return 6
11
script1 start 11
script1 end and return 12
23
2009-07-31 08:13:08 Jobs:0 Err:0
ab@mybox:~/Desktop>
</quote>
Notice the values grow faster over time because of the larger values sent
to, and returned from, script1. That should show you what to do from
here, I think.
Good luck.
ionpetrache wrote:
> ken_yap;2020099 Wrote:
>> It means that changes to the variable in the child script cannot affect
>> the variable of the same name in the parent. For example:
>>
> Code:
> --------------------
> > > parent.sh:
> > export FOO
> > FOO=1
> > echo $FOO
> > ./child.sh
> > echo $FOO
> --------------------
> Code:
> --------------------
> > > child.sh:
> > echo $FOO
> > FOO=2
> > echo $FOO
> --------------------
>> You will get 1, 1, 2, 1. Think of it as the child getting it’s own
>> copy of the variable.
>
> I think I understand.
> So If I wanna remember the value of the variable I have to save it to a
> file or something.I thought that exporting the variable in the first
> script will remember the variable value between to separate runs.
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
Intrigued by this perhaps someone could tell me the why or why nots using named pipes I achieved this?
Parent
$cat parent
#!/bin/bash
if -p "namedpipe" ]
then rm namedpipe
else
echo "Doesn't Exist"
fi
mkfifo namedpipe
this="LOTS AND LOTS"
echo $this
echo $this > namedpipe &
./child
echo "before pipe( $this )in Parent"
this=$(cat namedpipe)
echo "$this in Parent"
Child
$cat child
#!/bin/bash
this="The Wrong One"
echo "$this in Child"
this=$(cat namedpipe)
echo "$this in Child"
this="The Modified One"
echo "$this in Child"
echo $this > namedpipe &
Output
on running
$./parent
LOTS AND LOTS in Parent
The Wrong One in Child
LOTS AND LOTS in Child
The Modified One in Child
before pipe( LOTS AND LOTS )in Parent
The Modified One in Parent