
Mudei para fish
shell e estou muito feliz com isso. Não entendi como posso lidar com booleanos. Consegui escrever config.fish
que executa tmux
em ssh
(veja:Como posso iniciar o tmux automaticamente no fish shell enquanto me conecto ao servidor remoto via ssh), mas não estou satisfeito com a legibilidade do código e quero aprender mais sobre fish
o shell (já li o tutorial e consultei a referência). Quero que o código fique assim (sei que a sintaxe não está correta, só quero mostrar a ideia):
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
Eu sei que posso armazenar $status
es e compará-los test
como números inteiros, mas acho que é feio e ainda mais ilegível. Portanto, o problema é reutilizar $status
es e usá-los em if
and test
.
Como posso conseguir algo assim?
Responder1
Você pode estruturar isso como uma cadeia if/else. É possível (embora complicado) usar o início/fim para colocar uma instrução composta como uma condição 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
Um estilo melhor são os modificadores booleanos. início/fim tomam o lugar dos parênteses:
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"
(O primeiro início/fim não é estritamente necessário, mas melhora a clareza da IMO.)
Fatorar funções é uma terceira possibilidade:
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"