【问题标题】:Why does my awk redirection not work?为什么我的 awk 重定向不起作用?
【发布时间】:2017-04-02 23:05:30
【问题描述】:

我正在尝试重定向我的输出以替换我的文件的内容,但如果我这样做,它根本不会改变我的输出

#!/bin/bash

ssh_config_path="$HOME/.ssh/config"
temp_ssh_config_path="$HOME/.ssh/config_temporary"

new_primary_username=$1
curr_primary_username=`awk '/^Host github\.com$/,/#Username/{print $2}' $ssh_config_path | tail -1`
new_user_name=`awk "/^Host github-$new_primary_username$/,/#Name/{print $2}" $ssh_config_path | tail -1 | sed 's/#Name //' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
new_user_email=`awk "/^Host github-$new_primary_username$/,/#Email/{print $2}" $ssh_config_path | tail -1 | sed 's/#Email //' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`

echo "Switching from $curr_primary_username to $new_primary_username"
echo "Setting name to $new_user_name"
echo "Setting email to $new_user_email"

awk "
 !x{x=sub(/github-$new_primary_username/,\"github.com\")}
 !y{y=sub(/github\.com/,\"github-$curr_primary_username\")}
 1" $ssh_config_path > temp_ssh_config_path && mv temp_ssh_config_path ssh_config_path

但如果我这样做,我会在终端屏幕上得到正确的输出

#!/bin/bash

ssh_config_path="$HOME/.ssh/config"
temp_ssh_config_path="$HOME/.ssh/config_temporary"

new_primary_username=$1
curr_primary_username=`awk '/^Host github\.com$/,/#Username/{print $2}' $ssh_config_path | tail -1`
new_user_name=`awk "/^Host github-$new_primary_username$/,/#Name/{print $2}" $ssh_config_path | tail -1 | sed 's/#Name //' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`
new_user_email=`awk "/^Host github-$new_primary_username$/,/#Email/{print $2}" $ssh_config_path | tail -1 | sed 's/#Email //' | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'`

echo "Switching from $curr_primary_username to $new_primary_username"
echo "Setting name to $new_user_name"
echo "Setting email to $new_user_email"

awk "
 !x{x=sub(/github-$new_primary_username/,\"github.com\")}
 !y{y=sub(/github\.com/,\"github-$curr_primary_username\")}
 1" $ssh_config_path

【问题讨论】:

  • 创建一个 minimal reproducible example 来演示您的问题。我相信您可以减少我们需要查看的内容,从而帮助我们为您提供帮助。切勿将任何 awk 脚本或任何其他命令用双引号 (awk "script") 括起来,始终使用单引号 (awk 'script'),正如我们在之前问题的答案中向您展示的那样。使用$(cmd),而不是过时的`cmd` 语法。除非您有特定的目的并完全理解不这样做的所有含义和注意事项,否则请始终对每个 shell 变量(例如 "$ssh_config_path",而不是 $ssh_config_path)进行双引号。首先解决所有这些问题。
  • 我觉得没问题。有错误吗?它是创建一个空文件并显示到屏幕上吗?

标签: bash awk io-redirection


【解决方案1】:

令人失望的是,您与给出的答案相差甚远,但无论如何这里是您的脚本的正确语法(未经测试,因为您没有提供任何示例输入/输出):

#!/bin/bash

ssh_config_path="$HOME/.ssh/config"
temp_ssh_config_path="$HOME/.ssh/config_temporary"

new_primary_username="$1"
curr_primary_username=$(awk 'f&&/#Username/{print $2; exit} /^Host github\.com$/{f=1}' "$ssh_config_path")
new_user_name=$(awk -v npu="$new_primary_username" 'f&&/#Name/{print $2; exit} $0~"^Host github-"npu"$"{f=1}' "$ssh_config_path")
new_user_email=$(awk -v npu="$new_primary_username" 'f&&/#Email/{print $2; exit} $0~"^Host github-"npu"$"{f=1}' "$ssh_config_path")

echo "Switching from $curr_primary_username to $new_primary_username"
echo "Setting name to $new_user_name"
echo "Setting email to $new_user_email"

awk -v npu="$new_primary_username" -v cpu="$curr_primary_username" '
 !x{x=sub("github-"npu,"github.com")}
 !y{y=sub(/github\.com/,"github-"cpu)}
1' "$ssh_config_path" > temp_ssh_config_path && mv temp_ssh_config_path "$ssh_config_path"

通过这样做,我注意到您的最后一句话是:

mv temp_ssh_config_path ssh_config_path

当你的意思可能是:

mv temp_ssh_config_path "$ssh_config_path"

这会导致您预期的输出文件为空的问题。

当然,整个事情应该只写成 1 个简单的 awk 脚本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-13
    • 2015-03-19
    • 1970-01-01
    相关资源
    最近更新 更多