bash - 将 bash 函数转换为 fish 函数

标签 bash shell fish

<分区>

有人可以帮我把这个 bash 函数转换成 fish 吗?如果你能解释一下它们的作用就好了 "${@%%.app}”, 's//.*/g', “$@\”

bid() {
    local shortname location

    # combine all args as regex
    # (and remove ".app" from the end if it exists due to autocomplete)
    shortname=$(echo "${@%%.app}"|sed 's/ /.*/g')
    # if the file is a full match in apps folder, roll with it
    if [ -d "/Applications/$shortname.app" ]; then
        location="/Applications/$shortname.app"
    else # otherwise, start searching
        location=$(mdfind -onlyin /Applications -onlyin ~/Applications -onlyin /Developer/Applications 'kMDItemKind==Application'|awk -F '/' -v re="$shortname" 'tolower($NF) ~ re {print $0}'|head -n1)
    fi
    # No results? Die.
    [[ -z $location || $location = "" ]] && echo "$1 not found, I quit" && return
    # Otherwise, find the bundleid using spotlight metadata
    bundleid=$(mdls -name kMDItemCFBundleIdentifier -r "$location")
    # return the result or an error message
    [[ -z $bundleid || $bundleid = "" ]] && echo "Error getting bundle ID for \"$@\"" || echo "$location: $bundleid”
}

非常感谢。

最佳答案

关于差异的一些说明:

  • 设置变量
    • 狂欢:var=value
    • fish :set var value
  • 职能
    • 狂欢
      funcName() {
          ...
      }
      
    • fish
      function funcName
          ...
      end
      
  • 函数参数
    • 狂欢:"$@" , "$1" , "$2" , ...
    • fish :$argv , $argv[1] , $argv[2] , ...
  • 函数局部变量
    • 狂欢:local var
    • fish :set -l var
  • 条件句我
    • 狂欢:[[ ... ]]test ...[ ... ]
    • fish :test ...[ ... ] , 没有 [[ ... ]]
  • 条件二
    • 狂欢:if cond; then cmds; fi
    • fish :if cond; cmds; end
  • 条件三
    • 狂欢:cmd1 && cmd2
    • fish :cmd1; and cmd2
    • fish (从 fish 3.0 开始):cmd1 && cmd2
  • 命令替换
    • 狂欢:output=$(pipeline)
    • fish :set output (pipeline)
  • 进程替换
    • 狂欢:join <(sort file1) <(sort file2)
    • fish :join (sort file1 | psub) (sort file2 | psub)

文档

关于bash - 将 bash 函数转换为 fish 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29667714/

相关文章:

string - KornShell Printf - 填充字符串

bash - 将子目录中的特定文件复制到一个目录中

bash - 我的 bash 变量中到底是什么

linux - 在 Linux 上的 vi 编辑器中查找并替换字符串?

linux - 通过代理服务器 ssh 到远程机器以运行带参数的脚本

fish - 有条件地在 fish 中设置局部变量

fish - fish shell 中的 for 循环最多只能执行 3 次

php - Fish Shell - 如何在 PATH 中设置变量路径

linux - 使用 Linux 在目录中使用通配符查找文件

java - SubGit Java 堆空间