Fish Shell: 根据$status条件执行?
Fish Shell: Conditional execution based on $status?
在鱼shell中很难找到条件语法。有没有人 link 解释如何使用 ands 和 ors 编写 if?
特别想写
if not $status
do a command
end
在上一个命令返回不成功时执行命令。我该怎么做?
我使用状态变量在提示中显示它(如果它非零)。为此,我使用以下函数:
function __pileon_status_prompt
set -l __status $status
if test $__status != 0
printf '[%s%s%s]' (set_color red) $__status (set_color normal)
end
end
如您所见,我将局部变量设置为 $status
的值,并在条件中检查该变量。
参见 http://fishshell.com/docs/current/commands.html#if and http://fishshell.com/docs/current/tutorial.html#tut_conditionals。
Fish 的 if 结构如下所示:
if COMMAND
# do something if it succeeded
else
# do something if it failed ($status != 0)
end
然后还有not
、and
和or
命令,你可以像
一样使用
if not COMMAND1; or COMMAND2
# and so on
如果你真的想测试一个变量(例如$status),你需要使用test
作为命令,比如
if test $status -eq 0
请记住 $status 在每个命令后都会发生变化,因此如果您需要使用较早命令的状态(在提示中很常见),您需要按照 Joachim Pileborg 所说的进行操作,将其保存到另一个变量中。
此外,test
有一些引号问题(因为它是 fish 中为数不多的要遵守 POSIX 的部分之一)- 如果 test $foo -eq 0
中的 $foo 未定义,请测试将出错,如果它在 test -n $foo
中未定义,则测试将为真(因为 POSIX 要求带有一个参数的测试为真)。
作为旁注,在 2.3.0 之前的 fish 版本中,您需要在带有 and
或 or
的条件周围添加 begin
和 end
,因为它是解释很奇怪。
所以你必须做
如果开始命令;或命令 2;结尾
# 为 status = 0
做点什么
最短的缩写形式是
the_previous_command; or do_a_command
# ..................^^^^^
假设您从 "the_previous_command"
获得 $status
在鱼shell中很难找到条件语法。有没有人 link 解释如何使用 ands 和 ors 编写 if?
特别想写
if not $status
do a command
end
在上一个命令返回不成功时执行命令。我该怎么做?
我使用状态变量在提示中显示它(如果它非零)。为此,我使用以下函数:
function __pileon_status_prompt
set -l __status $status
if test $__status != 0
printf '[%s%s%s]' (set_color red) $__status (set_color normal)
end
end
如您所见,我将局部变量设置为 $status
的值,并在条件中检查该变量。
参见 http://fishshell.com/docs/current/commands.html#if and http://fishshell.com/docs/current/tutorial.html#tut_conditionals。
Fish 的 if 结构如下所示:
if COMMAND
# do something if it succeeded
else
# do something if it failed ($status != 0)
end
然后还有not
、and
和or
命令,你可以像
if not COMMAND1; or COMMAND2
# and so on
如果你真的想测试一个变量(例如$status),你需要使用test
作为命令,比如
if test $status -eq 0
请记住 $status 在每个命令后都会发生变化,因此如果您需要使用较早命令的状态(在提示中很常见),您需要按照 Joachim Pileborg 所说的进行操作,将其保存到另一个变量中。
此外,test
有一些引号问题(因为它是 fish 中为数不多的要遵守 POSIX 的部分之一)- 如果 test $foo -eq 0
中的 $foo 未定义,请测试将出错,如果它在 test -n $foo
中未定义,则测试将为真(因为 POSIX 要求带有一个参数的测试为真)。
作为旁注,在 2.3.0 之前的 fish 版本中,您需要在带有 and
或 or
的条件周围添加 begin
和 end
,因为它是解释很奇怪。
所以你必须做
如果开始命令;或命令 2;结尾 # 为 status = 0
做点什么最短的缩写形式是
the_previous_command; or do_a_command
# ..................^^^^^
假设您从 "the_previous_command"
获得$status