f



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
0
soazine (387)
7/9/2004 8:25:12 PM
comp.os.linux.misc 33599 articles. 1 followers. amosa69 (78) is leader. Post Follow

7 Replies
923 Views

Similar Articles

[PageSpeed] 35

In comp.os.linux.misc Phil Powell <soazine@erols.com> wrote:
> In PHP we have a command "require()" that obtains a file and logically
> places it into another file.

Isn't the same as the $include ?

Davide

-- 
| Zero Defects, n.: The result of shutting down a production line. 
| 
| 
| 
0
7/9/2004 8:32:14 PM
On 9 Jul 2004 20:32:14 GMT, Davide Bianchi
<davideyeahsure@onlyforfun.net> wrote:

>In comp.os.linux.misc Phil Powell <soazine@erols.com> wrote:
>> In PHP we have a command "require()" that obtains a file and logically
>> places it into another file.
>
>Isn't the same as the $include ?

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@databasix 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
===========================================================================
0
gburnore (668)
7/9/2004 8:38:53 PM
["Followup-To:" header set to comp.os.linux.misc.]
On 2004-07-09, Phil Powell wrote:
> 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".

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

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

     Or is that not what you want?

> This is what I have so far and it fails:
>
> if [ -f ivc.cfg ]; then
>  cat ivc.cfg
> fi

     What do you mean, "fail"? Your snippet will do exactly what it is
     written to do: send the contents of ivc.cfg to stdout.

> 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.

     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
0
cfajohnson (1827)
7/9/2004 8:39:41 PM
In comp.os.linux.misc, Phil Powell uttered these immortal words:

> 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

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.
0
andyfraser31 (351)
7/9/2004 8:46:46 PM
In comp.os.linux.misc Gary L. Burnore <gburnore@databasix.com> wrote:
>>Isn't the same as the $include ?
> Almost.
> require() does the same thing include() does with the difference being

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. 
| 
| 
| 
0
7/9/2004 8:50:22 PM
Good grief it was that simple!

Thanx
Phil

"Andy Fraser" <andyfraser31@hotmail.com> wrote in message
news:nqc3s1-j31.ln1@news.linuxuser.org.uk...
> In comp.os.linux.misc, Phil Powell uttered these immortal words:
>
> > 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
>
> 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.


0
soazine (387)
7/10/2004 12:18:32 AM
Why not just use PHP for a shell script, it works better than a bash
script
file = (ivc.cfg);
if(ivc.cfg) {
$Query = "/bin/cat  ivc.cfg";
exec ($Query, $Result, $ReturnValue);
reset ($Result);
while (list($key,$val) = each($Result)) {
print "$val";
}
}

Phil Powell wrote:
> 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

0
7/11/2004 7:41:51 AM
Reply: