Find the answer to your Linux question:
Results 1 to 10 of 10
I have two scripts: input.sh Code: #component components_list() { _component1="server" _component2="client" } #files server() { _description="servercomponent" _file1="bin/vinfo.exe" _file2="Welcome.html" } client() { _description="clientcomponent" _file1="Welcome.html" } My requirement is to find out ...
  1. #1
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59

    accessing variable defined in a function in another script

    I have two scripts:
    input.sh
    Code:
    #component
    components_list()
    {
    _component1="server"
    _component2="client"
    }
    #files
    server()
    {
    _description="servercomponent"
    _file1="bin/vinfo.exe"
    _file2="Welcome.html"
    }
    
    client()
    {
    _description="clientcomponent"
    _file1="Welcome.html"
    }
    My requirement is to find out the number of _file variables defined in a particular function like in the above example for server function its 2 and for client function its 1.

  2. #2
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59
    Is it not possible to do this?

  3. #3
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Well, of course it's possible. The question is, how do you intend to share the information between these two processes? You could use a file, or a pipe, for example.

  4. #4
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59
    I plan to call that function from another script and then accessing the just the number of _file variables into a variable and then start reading the values one by one.
    Any mechanism will do.

  5. #5
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Quote Originally Posted by sandeep t View Post
    I plan to call that function from another script
    It is totally unclear what this is supposed to mean. Will one script source the other? Will you execute the other script in a subshell? What does the script return?

  6. #6
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59
    Yes, one script will source the other script.
    This is the another script which will source the input.sh
    build.sh
    Code:
    . input.sh
    #get the No of _file variables defined in functions 'server' and 'client' and access their values one-by-one

  7. #7
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    Well then, what's your problem? You are effectively running a single script, regardless of whether the functions were defined in this file or that file. Put the information in a global variable and access it from anywhere in your script.

  8. #8
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59
    the problem is the variable names are same in all the functions. if you see _file1 is defined in both the functions.
    Can you please tell me how to achieve my objective?

  9. #9
    Linux Newbie
    Join Date
    Jul 2008
    Posts
    181
    No. This is broken as designed.

  10. #10
    Just Joined!
    Join Date
    Apr 2007
    Posts
    59
    Yeah, may be my design is wrong. One can't have the same variables in the more than one functions.
    Anyways, can I atleast access the variables names and their values?
    build.sh
    Code:
    . input.sh
    #get variable names and values defined in server functions

Posting Permissions

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