Find the answer to your Linux question:
Results 1 to 5 of 5
Hello All , I am running two scripts ( send.sh and talk.sh ) simultaneously i.e at a time I have some if statement to check condition in talk.sh script i.e ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    45

    passing variables

    Hello All ,


    I am running two scripts ( send.sh and talk.sh ) simultaneously i.e at a time

    I have some if statement to check condition in talk.sh script i.e it it is true i am assigning variable as one ( i.e If true data=1 )

    Now I want that data variable in another script that is running send.sh

    how i need access that data variable in send.sh sript .

    please help me out
    thnks in advance

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,568
    I'm not sure about this solution, try to export the variable,

    export $data
    If export not works ,then i think you can write the value to file and read it from there....

    HTH
    - Lakshmipathi.G
    -------------------
    FOSS India Award winning ext3fs Undelete tool and tutorials www.giis.co.in
    First they criticize you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -------------------

  3. #3
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by packet View Post
    Hello All ,


    I am running two scripts ( send.sh and talk.sh ) simultaneously i.e at a time
    Define "simultaneously". Do you mean that you launch either of them from command line? For example, one in one terminal and another in a second terminal. Or do you mean that one is always called from within the other? Specify a bit more. Whether you can access directly to these variables will depend on this, so it's something we need to know.

    I have some if statement to check condition in talk.sh script i.e it it is true i am assigning variable as one ( i.e If true data=1 )
    Each script spawns its own subshell, and hence, has it's own environment, which is only accessible from within that shell instance. If you launch one script in term1, and then another one in term2, the script in term1 will not be able to read any variable set by the script on term2, nor vice-versa. However, if you call the second script from within the first one, you can export the variable, so it's marked to be copied in the environment of subsequent subshells.

    So, if you have two scripts, "a" and "b", with this contents:

    Code:
    # script a
    export a=aaaa
    ./b
    Code:
    # script b
    echo $a
    Then invoking ./a will produce as result "aaaa", as you see the var $a was marked to be exported into child shells, and is "inherited" by them. That's why we can see the var $a from the script "b".

    As you can see, it all depends on how you exactly are using these scripts, and how do you call them.

    If both processes are completely independent, then you are going to have to use files to communicate them, as the other user above already pointed out.

  4. #4
    Just Joined!
    Join Date
    Feb 2008
    Posts
    45
    Thnks for reply Now I am going to explain in detail how I am running two scripts simultaneously

    I am using Os Linux Knoppix5.1 there is an option to run script simultaneously from Autostart i.e if i put the files ( sripts ) in that area it will run at a time after boot up os.

    so I am running two scripts in this way i.e send.sh and talk.sh so the talk.sh as lengthy code and in that i have a if statement in between if that is true then i am assigning the 1 for data variable.( data=1 ) so i need that variable in my send,sh script .

    how I have to do ? is this possible or os their any solution ?

    please reply as early as possible....

    thanks in advance

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by packet View Post
    Thnks for reply Now I am going to explain in detail how I am running two scripts simultaneously

    I am using Os Linux Knoppix5.1 there is an option to run script simultaneously from Autostart i.e if i put the files ( sripts ) in that area it will run at a time after boot up os.
    I assume that this autostart thinggie is a desktop dependent feature (for example, like in ~/.kde/Autostart/ folder or something like that).

    In any case, I guess that both scripts are running independently, and as such they have no way to communicate other than via a file.

    so I am running two scripts in this way i.e send.sh and talk.sh so the talk.sh as lengthy code and in that i have a if statement in between if that is true then i am assigning the 1 for data variable.( data=1 ) so i need that variable in my send,sh script .

    how I have to do ? is this possible or os their any solution ?
    I assume that you want script1 to wait for script2 to complete anything or something like that. A simple example on how to achieve that using inotify (the way to go nowadays).

    Code:
    #!/bin/bash
    # This is script1
    
    inotifywait -mrq /tmp/ --format "%f" --event create | while read file
    do
            if [ "$file" == "${USER}-semaphore" ]
            then
                    echo "Green light on my semaphore, the script will continue now."
                    break
            fi
    done
    
    rm -f "/tmp/${USER}-semaphore"
    echo "You can't see this text until the semaphore is green."
    Code:
    #!/bin/bash
    # This is script2
    
    touch "/tmp/${USER}-semaphore"
    # Those two lines are needed on my bash because
    # it seems to ignore the first round after break for
    # some reason.
    rm -f "/tmp/${USER}-semaphore"
    touch "/tmp/${USER}-semaphore"
    As you see, script1 is going to hang on there forever until script2 creates a given file. We could as well read the contents of that file and act conditionally here etc. etc. I hope you get the idea.

    If the system you are running this on doesn't support inotify, you can always use sleep on your loop, that way you can check for the existence of that file every 10 seconds or so. However if inotify is enabled then it's the way to go because that way you don't waste resources on useless iterations.

Posting Permissions

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