How to do PHP "require()" or TCL "source" in bash script

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    How to do PHP "require()" or TCL "source" in bash script

    I'm sorry but I can't figure out how to explain this any better than
    this.

    In PHP we have a command "require()" that obtains a file and logically
    places it into another file.

    I cannot figure out how to do this in bash script as the requirement
    is necessary for a migration script to obtain the code from a .cfg
    file and then be able for the "parent" script to run the code it
    "imported" from the .cfg file, much like PHP's require() or TCL's
    "source".

    This is what I have so far and it fails:

    if [ -f ivc.cfg ]; then
    cat ivc.cfg
    fi

    Anyone really know bash script well please help, it's a barrier for me
    not to be able to get just this one piece working.

    Thanx
    Phil
  • Davide Bianchi

    #2
    Re: How to do PHP "require() " or TCL "source&qu ot; in bash script

    In comp.os.linux.m isc Phil Powell <soazine@erols. com> wrote:[color=blue]
    > In PHP we have a command "require()" that obtains a file and logically
    > places it into another file.[/color]

    Isn't the same as the $include ?

    Davide

    --
    | Zero Defects, n.: The result of shutting down a production line.
    |
    |
    |

    Comment

    • Gary L. Burnore

      #3
      Re: How to do PHP &quot;require() &quot; or TCL &quot;source&qu ot; in bash script

      On 9 Jul 2004 20:32:14 GMT, Davide Bianchi
      <davideyeahsure @onlyforfun.net > wrote:
      [color=blue]
      >In comp.os.linux.m isc Phil Powell <soazine@erols. com> wrote:[color=green]
      >> In PHP we have a command "require()" that obtains a file and logically
      >> places it into another file.[/color]
      >
      >Isn't the same as the $include ?[/color]

      Almost.

      require() does the same thing include() does with the difference being
      if include() fails the script can still continue. If require() fails,
      the script must end.

      There's also include_once() and require_once() with the same
      capabilities as their non _once'd countrparts and the nicly added
      feature of not reloading the same require or include more than once.


      --
      gburnore@databa six dot com
      ---------------------------------------------------------------------------
      How you look depends on where you go.
      ---------------------------------------------------------------------------
      Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
      | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
      DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
      | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
      Black Helicopter Repair Svcs Division | Official Proof of Purchase
      =============== =============== =============== =============== ===============
      Want one? GET one! http://signup.databasix.com
      =============== =============== =============== =============== ===============

      Comment

      • Chris F.A. Johnson

        #4
        Re: How to do PHP &quot;require() &quot; or TCL &quot;source&qu ot; in bash script

        ["Followup-To:" header set to comp.os.linux.m isc.]
        On 2004-07-09, Phil Powell wrote:[color=blue]
        > I'm sorry but I can't figure out how to explain this any better than
        > this.
        >
        > In PHP we have a command "require()" that obtains a file and logically
        > places it into another file.
        >
        > I cannot figure out how to do this in bash script as the requirement
        > is necessary for a migration script to obtain the code from a .cfg
        > file and then be able for the "parent" script to run the code it
        > "imported" from the .cfg file, much like PHP's require() or TCL's
        > "source".[/color]

        What's wrong with bash's "source" command?

        [ -f ivc.cfg ] && source ivc.cfg

        Or is that not what you want?
        [color=blue]
        > This is what I have so far and it fails:
        >
        > if [ -f ivc.cfg ]; then
        > cat ivc.cfg
        > fi[/color]

        What do you mean, "fail"? Your snippet will do exactly what it is
        written to do: send the contents of ivc.cfg to stdout.
        [color=blue]
        > Anyone really know bash script well please help, it's a barrier for me
        > not to be able to get just this one piece working.[/color]

        The source command will only work if the contents of the cg file
        are a valid shell script, e.g., "variable=value ". Otherwise, you
        will have to parse the file:

        [ -f ivc.cfg ] && while read -r line
        do
        : parse each line here; code depends on format of the line
        done < ivc.cfg

        --
        Chris F.A. Johnson http://cfaj.freeshell.org/shell
        =============== =============== =============== =============== =======
        My code (if any) in this post is copyright 2004, Chris F.A. Johnson
        and may be copied under the terms of the GNU General Public License

        Comment

        • Andy Fraser

          #5
          Re: How to do PHP &quot;require() &quot; or TCL &quot;source&qu ot; in bash script

          In comp.os.linux.m isc, Phil Powell uttered these immortal words:
          [color=blue]
          > In PHP we have a command "require()" that obtains a file and logically
          > places it into another file.
          >
          > I cannot figure out how to do this in bash script as the requirement
          > is necessary for a migration script to obtain the code from a .cfg
          > file and then be able for the "parent" script to run the code it
          > "imported" from the .cfg file, much like PHP's require() or TCL's
          > "source".
          >
          > This is what I have so far and it fails:
          >
          > if [ -f ivc.cfg ]; then
          > cat ivc.cfg
          > fi[/color]

          I'm no bash script expert but I get by. I would say that that's along the
          right lines. This works for me:

          [ -f ivc.cfg ] || exit 1
          .. ivc.cfg

          If the file's not there the script exits with a value of 1 otherwise it
          reads and executes ivc.cfg.

          Then there's:

          if [ -f ivc.cfg ]
          then
          . ivc.cfg
          else
          echo "Required file not found."
          exit 1
          fi

          If the file's not there the script prints a message and exits with a value
          of 1 otherwise it reads and executes ivc.cfg.

          --
          Andy.

          Comment

          • Davide Bianchi

            #6
            Re: How to do PHP &quot;require() &quot; or TCL &quot;source&qu ot; in bash script

            In comp.os.linux.m isc Gary L. Burnore <gburnore@datab asix.com> wrote:[color=blue][color=green]
            >>Isn't the same as the $include ?[/color]
            > Almost.
            > require() does the same thing include() does with the difference being[/color]

            No, no. I was talking about the $include in a bash script. Not in PHP.

            Davide

            --
            | Everybody wants to go to heaven, but nobody wants to die.
            |
            |
            |

            Comment

            Working...