【问题标题】:How can I find a specific value in a file and store into a variable in a bash script?如何在文件中找到特定值并将其存储到 bash 脚本中的变量中?
【发布时间】:2013-10-16 07:41:05
【问题描述】:

我正在努力实现以下目标:

我有一个包含这一行的 config.php 文件:

define('URL', 'www.website.com');

在我的 bash 脚本中,我想在 config.php 中搜索,找到 URL 的值并将其存储到变量中.

知道怎么做吗?

【问题讨论】:

  • 一个可接受的解决方案是围绕 config.php 创建一个 PHP 包装器,它以更易于解析的形式输出您的常量?例如。名称、值

标签: php bash variables search


【解决方案1】:

以下可能对您有用:

awk -F"[']" '/define/ && /URL/{print $4}' config.php

为了给一个变量赋值,比如说:

var=$(awk -F"[']" '/define/ && /URL/{print $4}' config.php)

【讨论】:

    【解决方案2】:

    使用grep -P:

    grep -oP "^ *define *\( *'URL'[^']+'\K[^']+" config.php
    www.website.com
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-12
      • 2016-07-10
      • 1970-01-01
      • 2018-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多