是否可以根据不同的操作系统或可用命令设置 FISH 别名?

问题描述 投票:0回答:1

我看到在 Fish shell 中定义别名,并且我知道如何设置别名和/或函数,

但是是否可以根据操作系统设置不同的别名目标?也许使用函数?

我相信我所经历的并不标准。我目前正在为我的所有系统(例如 macOS 和 Linux(Arch 或 Debian))使用一种 FISH 设置(点文件)。我注意到有时对于某些程序,它在不同的操作系统上是不一样的。

例如,我尝试使用

bat
来替换
cat
,在 Archlinux 上,
bat
是一个可以从
pacman
安装的程序,但在 Debian 上,如果你用
bat
安装
apt install bat
,您将得到
batcat

我当前的别名设置是

alias cat='bat' 

但是使用此设置,Debian 会抛出错误。

我想做的是可以根据不同的操作系统使用不同的程序。或者如果可能的话,使用第一个可用的命令(如果可用)。类似的东西

function cat 
  if os == 'debian': use batcat
  elseif os == 'archlinux': use bat
end

或者可能更好的东西

function cat
  try to use bat first, if does not exist, try to use batcat, if not fallback to cat
end

我相信在ZSH上,我们可以做

alias cat="bat | cat"
进行OR运算。但好像FISH上不可用

最好的选择是什么?

fish
1个回答
0
投票

好吧,经过一番挖掘后,我想出了这个:

#!/bin/fish

function func arg1 args2 arg3
    switch (uname)
    case ArchLinux
        command "$arg1"
    case Debian
        command "$arg2"
    case MacOS
        command "$arg3"
    case '*'
        echo Hi, stranger!
    end
end
# you execute the command with this
func bat batcat idk_mac

我还没有测试过这个,所以你必须尝试一下。

以下是我发现的一些链接,它们给了我一些想法: https://fishshell.com/docs/current/language.html#the-switch-statement

https://github.com/fish-shell/fish-shell/issues/6150#issuecomment-536593949

© www.soinside.com 2019 - 2024. All rights reserved.