Fish Shellでブール値を使用する方法は?

Fish Shellでブール値を使用する方法は?

シェルに変えfishたのにとても満足しています。ブール値の処理方法を理解していません。config.fish実行されるtmuxコードを書いていますssh(参照:SSH経由でリモートサーバーに接続するときにFish Shellで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

私は$statusesを保存してtest整数と比較できることを知っていますが、見苦しく、読みにくいと思います。したがって、問題はesを再利用して$statusandで使用することです。iftest

どうすればそのような目標を達成できますか?

ベストアンサー1

if/else チェーンで構成できます。 (不便ですが)以下の条件のように複合文で開始/終了を使用することが可能です。

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"

(最初の開始/終了は必ずしも必要ではありませんが、私の考えでは明確さが向上します。)

関数分解は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"

おすすめ記事