Find the answer to your Linux question:
Results 1 to 3 of 3
I am running a script that can run as many as 10 simultaneous times. During my testing, i noticed that if the script starts while another is in process, it ...
  1. #1
    Just Joined!
    Join Date
    Feb 2009
    Posts
    9

    $1 variable in multiple instances of same script

    I am running a script that can run as many as 10 simultaneous times.

    During my testing, i noticed that if the script starts while another is in process, it inherits the $1 variable, which is always different.

    For example, the script starts with "scripthere.sh entry1", anther one starts almsot at the same time "scripthere.sh entry2". As the script is running, the second instance inherits $1 from the first script. So instead of $1 entry2, it shows up as entry1.

    Anyone know how I can store $1 so that its unique to that instance of the script only?

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    That should not be happening. Parameters to the script are unique to that instance:

    Code:
    bricka@apollo14 ~/test/bash $ ./two_instances.sh entry1 & ./two_instances.sh entry2
    [1] 23969
    23970: entry2
    23969: entry1
    [1]+  Done                    ./two_instances.sh entry1
    Here we see that 23969 starts first, but 23970 outputs first. Their $1 variables are different, however.

    Maybe you're getting confused about what which script is? You can print out the $$ variable in order to see the PID of a process.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Just Joined!
    Join Date
    Feb 2009
    Posts
    9
    Banged my head against this for a couple hours, made the post, and then figured it out shortly after of course.

    My script was writing a temp file so it could be mailed using sendmail. The temp file was a static name so when the second instance ran, it would overwrite the one created from the first instance, which would then send out the modified version.

    So simple, staring me in the face the whole time. Sorry about that!

Posting Permissions

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