2016-11-19 3 views
0

Im Versuch, meine Ausgabe zu ersetzen den Inhalt meiner Datei zu umleiten, aber wenn ich das tue es für meine Ausgabe auf alleWarum funktioniert meine awk-Umleitung nicht?

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

aber wenn ich das bekomme ich die auf meinem Terminal-Bildschirm korrekte Ausgabe zu tun ändern

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

Erstellen Sie ein [mcve], das Ihr Problem veranschaulicht. Ich bin mir sicher, dass Sie reduzieren können, was wir uns anschauen müssen, und helfen Sie uns, Ihnen zu helfen. Schließen Sie niemals ein Skript für awk oder einen anderen Befehl in Anführungszeichen ('awk" script "') ein, immer einzeln ('awk 'script''), wie wir Ihnen in den Antworten auf Ihre vorherigen Fragen gezeigt haben. Verwenden Sie '$ (cmd)', nicht die veraltete '\' cmd \ '' Syntax. Zitiere immer jede Shellvariable (z. B. '$ ssh_config_path'', nicht '$ ssh_config_path'), es sei denn, du hast einen bestimmten Zweck im Sinn und verstehst alle Implikationen und Vorbehalte, wenn du dies nicht tust. Behebe das alles zuerst. –

+0

Es sieht für mich ok aus. Gibt es einen Fehler? Wird eine leere Datei erstellt und auf dem Bildschirm angezeigt? –

Antwort

2

Es ist enttäuschend, wie weit haben Sie aus den Antworten schwenkte man auf jeden Fall gegeben wurden, aber hier ist die korrekte Syntax für das Skript (nicht getestet, da Sie keine Probe Eingabe/Ausgabe vorsah):

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

Dadurch, dass ich bemerkt, dass Ihre letzte Aussage war:

mv temp_ssh_config_path ssh_config_path 

wenn Sie wahrscheinlich gemeint:

mv temp_ssh_config_path "$ssh_config_path" 

und das wäre ein Problem mit dem erwarteten Ausgabedatei leer ist verursacht haben.

Die ganze Sache sollte natürlich als nur ein einfaches awk-Skript geschrieben worden sein.

Verwandte Themen