我切换到fishshell 并且对它非常满意。我不知道如何处理布尔值。我设法写config.fish,执行tmux上ssh(见:我怎样才能在鱼贝自动启动TMUX而通过ssh连接到远程服务器)连接,但我不是很满意代码的可读性,并希望了解更多关于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
Run Code Online (Sandbox Code Playgroud)
我知道我可以存储$statuses 并将它们与test整数进行比较,但我认为它很丑陋,甚至更不可读。所以问题是重用$statuses并在ifand中使用它们test。
我怎样才能实现这样的目标?
您可以将其构建为 if/else 链。可以(虽然笨拙)使用 begin/end 将复合语句作为 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
Run Code Online (Sandbox Code Playgroud)
更好的风格是布尔修饰符。开始/结束代替括号:
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"
Run Code Online (Sandbox Code Playgroud)
(第一个开始/结束不是绝对必要的,但可以提高 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"
Run Code Online (Sandbox Code Playgroud)