Include files in shell script.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • littlemaster
    New Member
    • Apr 2010
    • 25

    Include files in shell script.

    I have one configuration file. It will be used by perl and shell script. From perl using 'require' I have included the configuration file. Using require, include statement I tried in shell script , but it was not working.
    How to include a file and read value from that file using shell script?
  • littlemaster
    New Member
    • Apr 2010
    • 25

    #2
    Using 'source' we can acheive teh including of files in shell script.

    Example:

    filename: config
    var=260

    filename: a.sh
    Code:
    source config
    echo $var
    when we run the a.sh file, we can get the 'var' value as, 250..
    Last edited by numberwhun; May 10 '10, 12:16 PM. Reason: Please use code tags!

    Comment

    • numberwhun
      Recognized Expert Moderator Specialist
      • May 2007
      • 3467

      #3
      Your probably wondering why I moved your posting. You mentioned in it that you are trying to do the same in Perl and shell scripting, but that the shell scripting is what is not working.

      Because that makes this a shell scripting question, I have moved it to the Unix forums to get an answer. Next time, please select the best appropriate forum based on the contents of your questioin.

      Now, your question.....

      Including the file in the script using the source or '.' commands (which do the same thing) is one way. If you wanted to actually read the file, line by line, you could also do something like this:

      Code:
      while read line
      do
        echo $line
      done < infile
      Hope that helps!

      Comment

      • konsolebox
        New Member
        • Sep 2010
        • 1

        #4
        Shell Script Loader

        Hi! Just in case you still need that function in shell script, I had made a project just for the solution. Here's the link:

        Shell Script Loader
        Google Search

        With it you can have something like this:
        Code:
        #!/bin/sh
        
        # load the helper script
        . ./loader.sh
        
        # include other scripts
        include script1.sh
        include script2.sh
        
        ....
        The files script1.sh and script2.sh will only be loaded once and can also be specified in other scripts.

        If you need an example project that already uses it you may get an example package from PlayShell.

        Comment

        Working...