
我切換到fish
shell 並且對此感到非常滿意。我不明白如何處理布林值。我設法編寫了config.fish
執行的tmux
程式碼ssh
(參見:如何在透過 ssh 連接到遠端伺服器時在 Fish shell 中自動啟動 tmux)連接,但我對程式碼的可讀性不滿意,並且想了解有關fish
shell 的更多資訊(我已經閱讀了教程並瀏覽了參考資料)。我希望程式碼看起來像這樣(我知道語法不正確,我只是想展示這個想法):
set PPID (ps --pid %self -o ppid --no-headers)
if ps --pid $PPID | grep ssh
set attached (tmux has-session -t remote; and tmux attach-session -t remote)
if not attached
set created (tmux new-session -s remote; and kill %self)
end
if !\(test attached -o created\)
echo "tmux failed to start; using plain fish shell"
end
end
我知道我可以儲存$status
es 並將它們與test
整數進行比較,但我認為它很醜陋,甚至更難以閱讀。所以問題是重複使用es 並在和$status
中使用它們。if
test
我怎樣才能實現這樣的目標?
答案1
您可以將其建構為 if/else 鏈。可以(儘管不方便)使用 begin/end 將複合語句作為 if 條件:
if begin ; tmux has-session -t remote; and tmux attach-session -t remote; end
# We're attached!
else if begin; tmux new-session -s remote; and kill %self; end
# We created a new session
else
echo "tmux failed to start; using plain fish shell"
end
更好的風格是布林修飾符。 begin/end 代替括號:
begin
tmux has-session -t remote
and tmux attach-session -t remote
end
or begin
tmux new-session -s remote
and kill %self
end
or echo "tmux failed to start; using plain fish shell"
(第一個開始/結束並不是嚴格必要的,但在我看來可以提高清晰度。)
分解函數是第三種可能性:
function tmux_attach
tmux has-session -t remote
and tmux attach-session -t remote
end
function tmux_new_session
tmux new-session -s remote
and kill %self
end
tmux_attach
or tmux_new_session
or echo "tmux failed to start; using plain fish shell"