fish
に切り替えました シェルとそれに非常に満足しています。ブール値をどのように処理できるかわかりませんでした。なんとかconfig.fish
を書くことができました tmux
を実行します ssh
で (参照:ssh経由でリモートサーバーに接続しているときにfishシェルでtmuxを自動的に起動するにはどうすればよいですか)接続ですが、コードの可読性に満足しておらず、fish
について詳しく知りたいです。 シェル(私はすでにチュートリアルを読み、リファレンスを調べました)。コードをそのように見せたい(構文が正しくないことはわかっているので、アイデアを示したいだけです):
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
と比較します 整数としてですが、醜くてさらに読みにくいと思います。したがって、問題は$status
を再利用することです esし、if
で使用します およびtest
。
どうすればこのようなことを達成できますか?
承認された回答:
これをif/elseチェーンとして構成できます。開始/終了を使用して複合ステートメントを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
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"
(最初の開始/終了は厳密には必要ありませんが、IMOの明確さを向上させます。)
関数を因数分解することは3番目の可能性です:
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"