Results 1 to 9 of 9

Thread: exported variables bash

  1. #1
    Join Date
    Jun 2009
    Location
    Timisoara, Romania
    Posts
    190

    Question exported variables bash

    Hello all,

    I have a question regarding exported variables.

    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.

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: exported variables bash

    Exports only work in one direction, from parent to child.

  3. #3
    Join Date
    Jun 2009
    Location
    Timisoara, Romania
    Posts
    190

    Default Re: exported variables bash

    So I have to declare the variable in both scripts? Or if I declare only in the first script and I export it is ok?

    Quote Originally Posted by ken_yap View Post
    Exports only work in one direction, from parent to child.

  4. #4
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: exported variables bash

    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.

  5. #5
    Join Date
    Jun 2009
    Location
    Timisoara, Romania
    Posts
    190

    Default Re: exported variables bash

    Quote Originally Posted by ken_yap View Post
    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.

  6. #6
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,683
    Blog Entries
    4

    Default Re: exported variables bash

    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.

  7. #7

    Default Re: exported variables bash

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    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/

    iQIcBAEBAgAGBQJKcvx/AAoJEF+XTK08PnB5x0MQAJtwW++3JaeqWtRclwWUbEl3
    pE0xmdCpZ4+z9KJ1VtP2yDajgoP2m2BWUz9W47Yn3x3qz16MJnxfe0O1DlJaX23D
    +vLHqejD/89j+kMb4omlxIo9QWnZ98sO6ySTwluGOAON7Ybu9bqqCyWtGYjo7Wme
    Urbf5CpXC9o/JkR3QfEMia+2VX1CKtwPh44ct5tFkqZO8VwmYQ7apKTNxbkHt4ZP
    absm5eDDSNzks/Hzo7mge5m9tfKO1N4/8sJP2uHLGJNVtLwhO35+Gj35k5NPCs7V
    Az5p9BunHfxqVB6mxOWwHSxyaAtWeQHN12QSvFysMiCgRMslJ5kTsnuWjjAV1fTZ
    MDVm8T+f+TlP51qMHrO6B+tFPZBFwu0tklWs2EK1BxA082rUmKFmF133C7ZwPdhb
    nhg8n02ijLE76o+giWwlx7wmqYhZq4V5bt3ng1oFyoou41YKl05ukFM31B/fSUft
    tyNixJY+j0tKgH+NqnWcV1uW1jb3HvlKWexqQ/hea8hXanHOBstXonFV25ebMZZF
    63OV3yeJB1uMc/g+kbpvxZaJNitiX7+xvGKaDtgoNt4+ifOKjcvcryQDMUcKn1NH
    5sszsNvRzbekP9sC9kPvXJu5xpDFZ443IQoxGS/jGjUwsZPHlMrFLqA5IP2qwwfy
    YWYW/q5MGeZvwz0oKHCz
    =XL2I
    -----END PGP SIGNATURE-----

  8. #8

    Default Re: exported variables bash

    Intrigued by this perhaps someone could tell me the why or why nots using named pipes I achieved this?

    Parent
    Code:
    $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
    Code:
    $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
    Code:
    $./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
    Man first, have a try at Info, have a look at Wiki, if all that fails Scroogle!!!!!
    If I've helped click on the Rep button I don't know what it does but it sounds cool.

  9. #9
    Join Date
    Jun 2009
    Location
    Timisoara, Romania
    Posts
    190

    Default Re: exported variables bash

    Thanks for the replies.I'll try to use the variant with the exit status.It's easier for me.

    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •