Bash-Profilbefehle mit mehreren Problemen

396
Flowinho

Schönen Tag!

Ich versuche gerade, den folgenden Alias ​​zum Laufen zu bringen. 'git_fetch' und 'git_tree' sind benutzerdefinierte Aliasnamen, also machen Sie sich keine Sorgen.

alias git_workspace='osascript -e 'tell application "Terminal"' -e 'tell application "System Events" to tell process "Terminal" to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear && git_fetch\" in selected tab of the front window" -e 'end tell' &> /dev/null && osascript -e 'tell application "Terminal"' -e 'tell application "System Events" to tell process "Terminal" to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear && git_tree\" in selected tab of the front window" -e 'end tell' &> /dev/null' 

(Ich habe Zeilenumbrüche hinzugefügt, um die Lesbarkeit zu erhöhen.)

alias git_workspace='osascript -e 'tell application "Terminal"'  -e 'tell application "System Events" to tell process "Terminal"  to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear  && git_fetch\" in selected tab of the front window" -e 'end tell'  &> /dev/null  && osascript -e 'tell application "Terminal"' -e 'tell application "System Events"  to tell process "Terminal" to keystroke "n" using command down'  -e "do script with command \"cd `pwd`;clear && git_tree\"  in selected tab of the front window" -e 'end tell' &> /dev/null' 

Ich kann das aber nicht zum Laufen bekommen. Das erneute Laden von ~ / .profile führt zu:

-bash: alias: application: not found -bash: alias: Terminal -e tell: not found -bash: alias: application: not found -bash: alias: System Events: not found -bash: alias: to: not found -bash: alias: tell: not found -bash: alias: process: not found -bash: alias: Terminal: not found -bash: alias: to: not found -bash: alias: keystroke: not found -bash: alias: n: not found -bash: alias: using: not found -bash: alias: command: not found -bash: alias: down -e "do script with command \"cd `pwd`;clear && git_fetch\" in selected tab of the front window" -e end: not found -bash: alias: tell &> /dev/null && osascript -e tell: not found -bash: alias: application: not found -bash: alias: Terminal -e tell: not found -bash: alias: application: not found -bash: alias: System Events: not found -bash: alias: to: not found -bash: alias: tell: not found -bash: alias: process: not found -bash: alias: Terminal: not found -bash: alias: to: not found -bash: alias: keystroke: not found -bash: alias: n: not found -bash: alias: using: not found -bash: alias: command: not found -bash: alias: down -e "do script with command \"cd `pwd`;clear && git_tree\" in selected tab of the front window" -e end: not found -bash: alias: tell &> /dev/null: not found 

Kann mir jemand helfen? :)

EDIT : Ich habe die Lösung gefunden!

alias git_workspace='osascript -e "tell application \"Terminal\"" -e "tell application \"System Events\" to tell process \"Terminal\" to keystroke \"n\" using command down" -e "do script with command \"cd `pwd`;clear && git_fetch\" in selected tab of the front window" -e "end tell" &> /dev/null && osascript -e "tell application \"Terminal\"" -e "tell application \"System Events\" to tell process \"Terminal\" to keystroke \"n\" using command down" -e "do script with command \"cd `pwd`;clear && git_tree\" in selected tab of the front window" -e "end tell" &> /dev/null'

0
Ich habe die Lösung gefunden! TY zu all dem hat vielleicht in diese geschaut. Flowinho vor 7 Jahren 0

2 Antworten auf die Frage

0
Argonauts

Ein Alias ​​ist dafür nicht in der richtigen Lösung - die geschachtelten Fluchten, die erforderlich sind, werden (werden) nicht mehr verwaltet. Ich würde dies als Funktion implementieren, indem ich eine Bash-Funktion namens Heredoc in einer Funktion benutze :

git_workspace () { ( osascript <<-'EOF' tell application "Terminal"'  tell application "System Events" to tell process "Terminal" to keystroke "n" using command down do script with command "cd `pwd`;clear && git_fetch" in selected tab of the front window end tell EOF  if [ $? -eq 0 ]; then osascript <<-'EOF' tell application "Terminal" tell application "System Events" to tell process "Terminal" to keystroke "n" using command down do script with command "cd `pwd`;clear && git_tree" in selected tab of the front window end tell EOF fi ) &>/dev/null } 
  • Die osascriptHeredoc-Eingabe wird als Datei und nicht als String-Argument angezeigt. Das -eArgument ist daher nicht erforderlich.
  • Die Heredoc-Anweisung gibt den Text genau so aus, wie er zwischen den Markierungen <<EOFund geschrieben wurde EOF, mit den folgenden Vorbehalten:
    • In der Standardeinstellung ein heredoc wird interpretiert die \, $ und `Zeichen. Um dies zu deaktivieren, setzen Sie den Heredoc- <<EOFMarker in Anführungszeichen,<<'EOF'
    • Alles zwischen den beiden Markern wird ausgegeben, einschließlich des führenden Leerraums. Um das Einrücken zu ermöglichen (der Vorteil liegt darin, dass lesbarer Code anstelle von Skriptfunktionen verwendet wird), entfernt bash alle führenden Tabulatoren aus dem Text, wenn der Heredoc-Operator ein Bindestrich-Argument hat <<-'EOF'.
    • In jedem Fall muss sich das schließende heredoc-Tag in einer neuen Zeile ohne führenden Leerzeichen (Registerkarten oder sonstiges) befinden.

Wenn Sie diesen Ansatz verwenden, können Sie die osascript-Befehle genau so eingeben, wie sie nativ ausgegeben würden, ohne dass Sie Probleme mit der Erweiterung / Ersetzung von bash beheben müssen.

Ich habe beide Befehlssequenzen in einer Subshell eingeschlossen, um das Umleiten der Ausgabe zu erleichtern.

Wenn Sie möchten, um zu sehen, was der Text / Argument aussehen osascript, ändert vorübergehend die Funktion durch Ersetzen osascriptmit catund die Umleitung auf / dev / null entfernen. Da die Funktion jetzt geschrieben ist, sieht die Ausgabe folgendermaßen aus:

tell application "Terminal"'  tell application "System Events" to tell process "Terminal" to keystroke "n" using command down do script with command "cd `pwd`;clear && git_fetch" in selected tab of the front window end tell tell application "Terminal" tell application "System Events" to tell process "Terminal" to keystroke "n" using command down do script with command "cd `pwd`;clear && git_tree" in selected tab of the front window end tell 
Das ist ein schöner und sauberer Ansatz, den ich auf jeden Fall in anderen Anwendungsfällen verwenden möchte. TY für die ausführliche Erklärung. Flowinho vor 7 Jahren 0
0
Cestarian

Nun, das Problem ist, dass das Format des Alias ​​ungefähr so ​​ist:

alias thing='commands' alias thingy="commands" 

Mit anderen Worten müssen die Befehle des Alias ​​in Anführungszeichen eingeschlossen werden.

Sie haben folgendes:

alias git_workspace='osascript -e 'tell application "Terminal"' -e 'tell application "System Events" to tell process "Terminal" to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear && git_fetch\" in selected tab of the front window" -e 'end tell' &> /dev/null && osascript -e 'tell application "Terminal"' -e 'tell application "System Events" to tell process "Terminal" to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear && git_tree\" in selected tab of the front window" -e 'end tell' &> /dev/null' 

Nun gibt es ein schwerwiegendes Problem, dass in diesem übermäßig langen Befehl zu viele Anführungszeichen dazwischen stehen. Dein Alias ​​wird also wirklich so gelesen

alias git_workspace='osascript -e ' 

Und da Sie nach diesem Bit alle folgenden nachlaufenden Befehle haben, wird Ihre bash_profile-Datei zerstört.

Der einfachste Weg, dies zu umgehen, was ich persönlich immer benutze, ist das Skript in einer separaten Datei zu erstellen, wobei der folgende Code im Wesentlichen in eine leere Datei eingefügt wird.

osascript -e 'tell application "Terminal"' -e 'tell application "System Events" to tell process "Terminal" to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear && git_fetch\" in selected tab of the front window" -e 'end tell' &> /dev/null && osascript -e 'tell application "Terminal"' -e 'tell application "System Events" to tell process "Terminal" to keystroke "n" using command down' -e "do script with command \"cd `pwd`;clear && git_tree\" in selected tab of the front window" -e 'end tell' &> /dev/null 

Sie können der Datei git_workspace.sh oder git_workspace oder einem beliebigen anderen Namen den Namen geben. Dann müssen Sie die Ausführungsberechtigungen für die Datei vergeben

chmod +x git_workspace.sh 

Testen Sie anschließend das Skript, um sicherzustellen, dass es funktioniert, indem Sie es wie folgt ausführen

/path/to/git_workspace.sh 

Wenn es wie beabsichtigt ausgeführt wird, gehen Sie zu Ihrer bash-Profildatei und erstellen Sie diesen Alias

alias git_workspace='/path/to/git_workspace.sh' 

Und dann wird es funktionieren. Es gibt natürlich viele andere Möglichkeiten, aber das finde ich am einfachsten. Ich habe ein .scripts-Verzeichnis in meinem Heimatverzeichnis erstellt und alle Skripts eingefügt, die ich im Laufe der Jahre erstellt habe. Dies macht es auch einfach, meine Skripts zu sichern, falls ich den Computer wechseln oder mein Betriebssystem neu installieren möchte.

Ja, das war genau das Problem. Ich habe es geschafft, das Problem zu lösen, indem ich die Anführungszeichen (mit ein wenig Vim-Syntax-Hervorhebung) richtig entging, aber es in ein anderes Shell-Skript zu setzen, ist der Weg, den ich schätze. Flowinho vor 7 Jahren 0