简体   繁体   English

当执行完全相同的代码时,awk命令具有不同的行为。 为什么?

[英]awk command has different behaviors when executing the exact same code. Why?

I have created a little shellscript that is capable of receiving a list of values such as "MY_VAR_NAME=var_value MY_VAR_NAME2=value2 ...", separated by spaces only. 我创建了一个小shellscript,它能够接收值列表,例如“ MY_VAR_NAME = var_value MY_VAR_NAME2 = value2 ...”,仅用空格分隔。 There should be also the possibility to use values such as MY_VAR_NAME='' or MY_VAR_NAME= (nothing). 还应该可以使用诸如MY_VAR_NAME =''或MY_VAR_NAME =之类的值(无)。

These values are then used to change the value inside a environment variables file, for example, MY_VAR_NAME=var_value would make the script change the MY_VAR_NAME value inside the .env file to var_value, without changing anything else about the file. 这些值然后用于更改环境变量文件中的值,例如,MY_VAR_NAME = var_value将使脚本将.env文件中的MY_VAR_NAME值更改为var_value,而无需更改文件的其他任何内容。

The env file has the following configuration: env文件具有以下配置:

NODE_ENV=development  
APP_PATH=/media  
BASE_URL=http://localhost:3000  
ASSETS_PATH=http://localhost:3000  
USE_CDN=false  
APP_PORT=3000  
WEBPACK_PORT=8080  
IS_CONNECTED_TO_BACKEND=false  
SHOULD_BUILD=false  
USE_REDUX_TOOL=false  
USE_LOG_OUTPUT_AS_JSON=false  
ACCESS_KEY_ID=  
SECRET_ACCESS_KEY=  
BUCKET_NAME=  
BASE_PATH=  
MIX_PANEL_KEY=  
RDSTATION_KEY=  
RESOURCE_KEY=  
SHOULD_ENABLE_INTERCOM=false  
SHOULD_ENABLE_GTM=false  
SHOULD_ENABLE_UTA=false  
SHOULD_ENABLE_WOOTRIC=false

I have debugged my script, and found out that this is the point where sometimes it has a problem 我调试了脚本,发现这有时会出现问题

cat .envtemp | awk -v var_value="$VAR_VALUE" \
                    -v var_name="$VAR_NAME" \
                    -F '=' '$0 !~ var_name {print $0} $0 ~ var_name {print $1"="var_value}' | tee .envtemp

This piece of code sometimes outputs to .envtemp the proper result, while sometimes it just outputs nothing, making .envtemp empty 这段代码有时会向.envtemp输出正确的结果,而有时则什么也不输出,从而使.envtemp为空

The complete code i am using is the following: 我正在使用的完整代码如下:

function change_value(){
    VAR_NAME=$1
    VAR_VALUE=$2

    cat .envtemp | awk -v var_value="$VAR_VALUE" \
                    -v var_name="$VAR_NAME" \
                    -F '=' '$0 !~ var_name {print $0} $0 ~ var_name {print $1"="var_value}' | tee .envtemp 

    ls -l -a .env*
}

function manage_env(){
    for VAR in $@
    do
        var_name=`echo $VAR | awk -F '=' '{print $1}'`
        var_value=`echo $VAR | awk -F '=' '{print $2}'`
        change_value $var_name $var_value
    done
}

function main(){
    manage_env $@

    cat .envtemp > .env

    exit 0
}

main $@

Here is an example script for recreating the error. 这是用于重新创建错误的示例脚本。 It does not happen every time, and when it happens, it is not always with the same input. 它不会每次都发生,并且当它发生时,并非总是具有相同的输入。

#!/bin/bash
ENV_MANAGER_INPUT="NODE_ENV=production BASE_URL=http://qa.arquivei.com.br ASSETS_PATH=https://d4m6agb781hapn.cloudfront.net USE_CDN=true WEBPACK_PORT= IS_CONNECTED_TO_BACKEND=true ACCESS_KEY_ID= SECRET_ACCESS_KEY= BUCKET_NAME=frontend-assets-dev BASE_PATH=qa"
cp .env.dist .env

#Removes comment lines. The script needs a .envtemp file.
cat .env.dist | grep -v '#' | grep -v '^$' > .envtemp

./jenkins_env_manager.sh ${ENV_MANAGER_INPUT}

Have you tried use two files: 您是否尝试过使用两个文件:

mv .envtemp .envtemp.tmp
cat .envtemp.tmp | awk ... | tee .envtemp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Ubuntu和Mac中的awk命令中执行switch / case时出错 - Errors when executing switch/case in awk command in Ubuntu and Mac 在Jenkins中执行SSH命令时,bash配置文件与以相同用户通过SSH连接时不同 - When executing an SSH command in Jenkins, the bash profile is different to when connecting over SSH with the same user awk 内联命令和完整脚本有不同的输出 - awk inline command and full script has different output 为什么同一命令在存储到变量与输出到标准输出时会有所不同? - Why is the same command different when stored to variable vs output to stdout? awk在不同文件上一一执行相同的命令 - awk execute same command on different files one by one awk - 命令当文件没有记录时如何处理 - awk - command how to handle when file has no records 为什么我的代码中的此命令给出的结果与终端中的同一命令不同? - Why is this command within my code giving different result than the same command in terminal? 为什么这个 AWK 命令在填充控制台宽度时删除一个字符? - Why this AWK command deletes a character when the console width is filled? 为什么在Ruby on Rails中使用“ awk”命令不将任何内容返回变量? - Why 'awk' command is not returning anything to variable when used in Ruby on rails? awk和sed同时执行 - Awk and sed command on the same time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM