
Cambié a fish
Shell y estoy bastante contento con él. No entendí cómo puedo manejar los valores booleanos. Logré escribir config.fish
que se ejecuta tmux
en ssh
(ver:¿Cómo puedo iniciar tmux automáticamente en fish shell mientras me conecto a un servidor remoto a través de ssh?) conexión, pero no estoy satisfecho con la legibilidad del código y quiero aprender más sobre fish
Shell (ya leí el tutorial y revisé la referencia). Quiero que el código se vea así (sé que la sintaxis no es correcta, solo quiero mostrar la idea):
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
Sé que puedo almacenar $status
es y compararlos test
como números enteros, pero creo que es feo y aún más ilegible. Entonces el problema es reutilizar $status
es y usarlos en if
y test
.
¿Cómo puedo lograr algo como esto?
Respuesta1
Puedes estructurar esto como una cadena if/else. Es posible (aunque difícil de manejar) usar inicio/fin para poner una declaración compuesta como una condición 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
Un estilo más agradable son los modificadores booleanos. comienzo/fin toma el lugar del paréntesis:
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"
(El primer comienzo/final no es estrictamente necesario, pero mejora la claridad en mi opinión).
Factorizar funciones es una tercera posibilidad:
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"