By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 904 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
In comp.os.linux.misc Phil Powell <so*****@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.
|
|
|
Jul 17 '05 #2

P: n/a
On 9 Jul 2004 20:32:14 GMT, Davide Bianchi
<da************@onlyforfun.net> wrote:
In comp.os.linux.misc Phil Powell <so*****@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
================================================== =========================
Jul 17 '05 #3

P: n/a
["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
Jul 17 '05 #4

P: n/a
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.
Jul 17 '05 #5

P: n/a
In comp.os.linux.misc Gary L. Burnore <gb******@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.
|
|
|
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.