将 shell 脚本中的变量值复制到常量文件

将 shell 脚本中的变量值复制到常量文件

我正在appcenter-pre-build.sh其中执行另一个脚本,具体取决于环境类型。如果环境是UAT那么它将执行uat-pre-build.sh

这是appcenter-pre-build.sh

if [ -z "$ENV_TYPE" ]
then
    echo "You need define the ENV_TYPE variable in App Center"
    exit
fi
echo "ENV_TYPE is : - $ENV_TYPE"    
if [ "UAT" = "$ENV_TYPE" ]
then
    echo "Environment type : UAT"
    ENV_FILE=$APPCENTER_SOURCE_DIRECTORY/ABC/config/uat-pre-build.sh
else
    echo "you need to mentioned correct envrionment type"
    exit
fi
echo "Environment file : $ENV_FILE"
sh "$ENV_FILE"

我有三个不同的脚本取决于构建环境。

  1. uat-预构建.sh
  2. 开发预构建.sh
  3. 产品预构建.sh

在上面的脚本中,我正在读取变量值并尝试将其分配到 ConfigurationHelper.cs 中。

这是我的 uat-p​​re-build.sh

APP_ID=b1a4a39f-4d89-4f04-98d8-2a20eda89aad
# Get ConfigurationHelper.cs from project
APP_CONSTANT_FILE=$APPCENTER_SOURCE_DIRECTORY/helpers/ConfigurationHelper.cs

if [ -e "$APP_CONSTANT_FILE" ]
then
    echo "Updating environment configs in AppConstant.cs"

    sed -i '' 's#ApplicationID = "[-A-Za-z0-9:_./]*"#ApplicationID = "'$APP_ID'"#' $APP_CONSTANT_FILE

    echo "File content:"
    cat $APP_CONSTANT_FILE
else
        echo "Can not locate $APP_CONSTANT_FILE file"
fi  

这是ConfigurationHelper.cs

public class ConfigurationHelper
{
    public static string ApplicationID = string.Empty;
    static ConfigurationHelper(){}

}

我能够执行脚本,但无法将 APP_ID 值分配uat-pre-build.shConfigurationHelper.cs.似乎以下行未正确执行。

sed -i '' 's#ApplicationID = "[-A-Za-z0-9:_./]*"#ApplicationID = "'$APP_ID'"#' $APP_CONSTANT_FILE      

这是应用程序中心的输出。

[command]/bin/bash 

/Users/vsts/agent/2.153.1/work/1/s/ABC.Android/appcenter-pre-build.sh
ENV_TYPE is : - UAT
Environment type : UAT
Environment file : /Users/vsts/agent/2.153.1/work/1/s/ABC/config/uat-pre-build.sh
Updating environment configs in AppConstant.cs
File content:

namespace ABC.Business.Helpers
{
/// <summary>
/// The configuration helper.
/// </summary>
public class ConfigurationHelper
{
    public static string ApplicationID = string.Empty;


    /// <summary>
    /// Initializes static members of the <see cref="ConfigurationHelper"/> class.
    /// </summary>
    static ConfigurationHelper()
    {
    }

    }

}

请帮我解决这个问题。

答案1

您似乎想要替换文件string.Empty中的ConfigurationHelper.cs,但正如您发现的那样,您使用的替换永远不会匹配。你有ApplicationID = "[-A-Za-z0-9:_./]*"。这可以匹配

public static string ApplicationID = "string.Empty";

但由于您已在匹配模式中显式指定了双引号字符,因此它们必须出现在源代码中。

你可以尝试直接匹配ApplicationID = string.Empty。如果您确实需要任一string.Empty或带引号的字符串的复杂性,那么怎么样?ApplicationID = \(string.Empty|".*"\)

相关内容