생선 껍질에서 부울을 사용하는 방법은 무엇입니까?

생선 껍질에서 부울을 사용하는 방법은 무엇입니까?

나는 fish쉘로 전환했고 그것에 매우 만족했습니다. 부울을 어떻게 처리할 수 있는지 이해하지 못했습니다. 나는 다음에서 config.fish실행되는 글을 작성했습니다 (참조:tmuxsshSSH를 통해 원격 서버에 연결하는 동안 피쉬 쉘에서 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보기 흉하고 읽기가 더 어렵다고 생각합니다. 따라서 문제는 $statuses를 재사용하고 ifand 에서 사용하는 것입니다 test.

어떻게 하면 이와 같은 결과를 얻을 수 있습니까?

답변1

이것을 if/else 체인으로 구성할 수 있습니다. 복합문을 if 조건으로 넣기 위해 start/end를 사용하는 것이 (비록 다루기 힘들긴 하지만) 가능합니다:

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의 명확성을 향상시킵니다.)

기능을 분해하는 것은 세 번째 가능성입니다.

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"

관련 정보