从 PHP 文件读取配置设置并使用 shell 脚本上传整个项目代码的最简洁方法

发布于 2024-10-06 13:00:37 字数 1421 浏览 1 评论 0原文

我是 shell 脚本的新手,因此需要一些关于使用 shell 脚本解析 PHP 文件的想法。

我们的项目是一个 PHP 项目,我正在改进我们的 shell 脚本,该脚本用于将代码上传到生产服务器。

有一个 PHP 配置文件 production.settings.php 需要在上传过程中读取,其中包含一些常量 -

BASE_PATH(产品服务器上项目根目录的路径)
db_hostdb_name 等(prod 数据库的数据库设置 - 用于在上传之前备份数据库)

问题

  • 如何读取常量的值?

    它们的定义如下:

    define("BASE_PATH","/path/to/project/root");
    
  • 如何读取常量的第一个未注释的值?
    注意 - 该常量可能在同一文件中定义多次(让我们假设这种可能性 - 这可能是错误发生的,或者可能存在该行的注释实例)

到目前为止,我只能得到在我的 shell 脚本中使用 grep 包含字符串 define("BASE_PATH") 的行数 -

cd ..
PROJECT_ROOT=$PWD
result= grep -ic 'define("BASE_PATH",' $PROJECT_ROOT'/config/main.settings.php'
echo "see"$result
  • 这种解析方法足够好还是 yml 文件会更好?有任何 shell 命令/片段可以执行此操作,以便我可以通过编写更少的代码来获得结果

更新
检查我的其他问题以了解更多详细信息:-
在 shell 中操作数组(由 php-cli 打印)脚本
将 PHP CLI 打印的值分配给 shell 变量
在 bash shell 脚本中初始化动态变量(变量)

I am a newbie with shell scripting so need a few ideas on parsing a PHP file using a shell script.

Ours is a PHP project and I am improving our shell script which is used to upload code to production server.

There is one PHP config file production.settings.php which needs to be read during upload, for a few constants -

BASE_PATH (path to project root on prod server)
db_host, db_name etc. (database settings of prod database - to be used for taking a backup of the database before upload)

Question

  • How to read the value of the constants?

    They are defined like this:

    define("BASE_PATH","/path/to/project/root");
    
  • How to read the first uncommented value of the constant?
    Note - The constant may be defined more than once in the same file (let's assume the possibilty - this may happen by mistake or there may be commented instances of the line)

So far I am only able to get the number of lines containing the string define("BASE_PATH" using grep in my shell script -

cd ..
PROJECT_ROOT=$PWD
result= grep -ic 'define("BASE_PATH",' $PROJECT_ROOT'/config/main.settings.php'
echo "see"$result
  • Is this method of parsing good enough or a yml file would be better? Is there any shell command/snippet for doing this so that I can get the result by writing lesser amount of code?

Updates
Check my other questions for more details on this:-
Manipulating an array (printed by php-cli) in shell script,
Assigning values printed by PHP CLI to shell variables,
Initiating dynamic variables (variable variables) in bash shell script

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

南…巷孤猫 2024-10-13 13:00:38

如果您熟悉 PHP,那么可以使用 PHP 编写 shell 脚本。如果你走这条路,我会将所有配置设置移动到配置文件中... INI、YAML、XML,无论你的船是否漂浮。然后我将修改定义常量的应用程序的引导程序,以便也从此配置文件中读取。这样您就可以在脚本和应用程序中使用它,而无需更改它。

If youre comforttable with PHP then use PHP to write the shell script. IF you go this route i would move all config settings to a config file... INI, YAML, XML, whetever floats your boat. Then i would modify the bootstrap of the application that defines your constants to also read from this config file. That way you can use it in botht your script and the app without having to change it.

孤独患者 2024-10-13 13:00:37

只需使用 php 执行此操作,然后调用 shell 脚本来调用 php 脚本即可。

假设您在 defs.php 中定义了一堆定义:

define('NAME', 'JOHN');
define('HOBBY', 'FISHING');

然后创建一个 php 脚本 get_defs.php

require_once 'defs.php';
$const = get_defined_constants(true);
foreach($const['user'] as $k => $v) {
   echo "export $k=$v";
}

然后在您的 shell 脚本中,像这样运行它:

`php get_defs.php`

会发生什么,get_defs.php 会输出一堆 export KEY=VALUE,然后 shell 会运行 php get_defs.php 输出的那些命令。

just do it using the php, then call your shell script to invoke the php script.

Assuming you have your bunch of defines defined in defs.php:

define('NAME', 'JOHN');
define('HOBBY', 'FISHING');

then create a php script get_defs.php:

require_once 'defs.php';
$const = get_defined_constants(true);
foreach($const['user'] as $k => $v) {
   echo "export $k=$v";
}

then in your shell script, run it like so:

`php get_defs.php`

What happen is, get_defs.php will output bunch of export KEY=VALUE, then shell will run those commands outputted by your php get_defs.php.

温暖的光 2024-10-13 13:00:37

为什么不直接使用 PHP CLI 进行编码?你是这么理解的吗?另外,也许您可​​以将常量放入 ini 文件中并读取它们?

Why don't you just code with PHP CLI? That's what you understand? Also maybe you could put constants in a ini file and read them?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文