我想以这样一种方式使用systemctl命令
systemctl start (以及stop和restart)也会为同一服务触发systemctl statussystemctl命令都按其运行方式运行。我目前有一个基本的解决方案,其形式是
function sys --wraps systemctl -d "Start service and show its status"
systemctl restart $argv
systemctl status $argv
end但是,不仅我经常忘记使用它,而且它是极其有限的。
我相信,首先要对第一个参数作出有条件的决定,然后要么链式systemctl <parameter 1> <parameter 2>,要么只运行systemctl <parameter 1> ...。
我被困在条件(if the command is systemctl and first argument is one of ['stop', 'start', 'restart'] then ...),但也取决于扩展和内存是否会工作。
发布于 2018-09-05 17:17:16
我被困在这个条件下(如果命令是systemctl,并且第一个参数是“停止”、“开始”、“重新启动”,那么.)
在简单的案例1中,这实际上非常简单,特别是在不需要检查systemctl的情况下--您想要运行sys start,而不是sys systemctl start,对吗?
因此,条件变成:
if contains -- $argv[1] start stop restart
systemctl $argv
systemctl status $argv[2..-1]
else
systemctl $argv
end可以简化为
systemctl $argv
if contains -- $argv[1] start stop restart
systemctl status $argv[2..-1]
end我想以这样的方式使用systemctl命令
这听起来您希望有一个“真”包装器函数,其名称与基础命令相同。这是可能的,只是每次调用基础命令时都需要指定command $thething。
还请记住,fish函数通常是可用的,即使shell不是交互式的,所以如果您有任何脚本调用包装的东西,它们将最终调用该函数。
所以你做了一件事
# --wraps isn't necessary because the name is the same.
function systemctl
# without `command`, this will be an infinite loop
command systemctl $argv
if contains -- $argv[1] start stop restart
command systemctl status $argv[2..-1]
end
end扩展等功能将保留下来。
这里不需要做任何事情,因为扩展发生在调用函数之前。
1:备选方案存在一个普遍的问题。如果执行systemctl --user start,命令是start,但这不是第一个参数!您可以跳过以-开头的所有参数,以确定命令,但也有一些选项接受参数(例如,systemctl --host status start)。这里的一般解决方案基本上是不可能的,所以您可以做的最好的事情是类似fish的argparse解析,它需要添加工具支持的所有选项,然后重新执行参数解析。
https://stackoverflow.com/questions/52189964
复制相似问题