2016-11-19 44 views
0

我試着去重定向我的輸出來代替我的文件的內容,但如果我這樣做是完全不爲什麼我的awk重定向不起作用?

#!/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 
+2

創建一個演示您的問題的[mcve]。我相信你可以減少我們需要看的東西,所以幫助我們來幫助你。不要將awk或任何其他命令的腳本放在雙引號('awk「腳本」')中,總是單個('awk'腳本'),正如我們在前面問題的答案中所示的那樣。使用'$(cmd)',而不是過時的'''cmd''語法。除非你有一個特定的目的,並且完全理解不這樣做的所有含義和注意事項,否則總是雙重引用每個shell變量(例如'「ssh_config_path」',而不是'$ ssh_config_path')。首先解決所有這些問題。 –

+0

它對我來說很好。有錯誤嗎?它是否創建一個空文件並顯示在屏幕上? –

回答

2

這是令人失望你如何遠離你給出的答案,改變了方向,但在任何情況下,這裏是您的腳本的正確語法(未經檢驗的,因爲你沒有提供任何樣品輸入/輸出):

#!/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" 

而且會造成與預期輸出的文件是空的問題。

整個事情當然應該只寫成一個簡單的awk腳本。