Find the answer to your Linux question:
Results 1 to 5 of 5
Supposing I have two shell scripts called file1.sh and file2.sh If I hv a function called show_info() defined in file1.sh , is there a way I can use that same ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux User
    Join Date
    May 2003
    Location
    CA
    Posts
    370

    functions in shell scripts.


    Supposing I have two shell scripts called
    file1.sh and file2.sh
    If I hv a function called show_info() defined in file1.sh , is there a way I can use that same function from file2.sh? both the shell scripts are in the same
    directory...so is there a way to use a function defined in one shell script in another one??
    Fixing Unix is better than working with Windows.
    http://nikhilk.homedns.org/projects/index.html

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Nope. The way most people solve this is to have only the functions in a third file, and then source that file.

  3. #3
    Linux User
    Join Date
    May 2003
    Location
    CA
    Posts
    370
    ok...so can I place all the functions in one file and if I source it, then can i use those functions in any of the shell scripts that I have in a particular directory?
    can you pls provide a simple example of how it would work?

    much appreciated.
    Nik
    Fixing Unix is better than working with Windows.
    http://nikhilk.homedns.org/projects/index.html

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Check out the scripts in /etc/init.d - they use that trick.

  6. #5
    Linux User
    Join Date
    May 2003
    Location
    CA
    Posts
    370
    cool...thnks for the tip...the scripts work now and can call all the functions from a common source file.
    Fixing Unix is better than working with Windows.
    http://nikhilk.homedns.org/projects/index.html

Posting Permissions

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