Convert bash function to fish's [closed]

折月煮酒 提交于 2019-11-28 20:38:24

问题


Can someone help me convert this bash function to fish? It would also be nice if you could explain what these do like "${@%%.app}”, 's/ /.*/g’, "$@\” etc.

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”
}

Thanks very much in advance.


回答1:


Some notes on the differences:

  • setting variables
    • bash: var=value
    • fish: set var value
  • function arguments
    • bash: "$@"
    • fish: $argv
  • function local variables
    • bash: local var
    • fish: set -l var
  • conditionals I
    • bash: [[ ... ]] and [ ... ]
    • fish: test ...
  • conditionals II
    • bash: if cond; then cmds; fi
    • fish: if cond; cmds; end
  • conditionals III
    • bash: cmd1 && cmd2
    • fish: cmd1; and cmd2
    • fish (as of fish 3.0): cmd1 && cmd2
  • command substitution
    • bash: output=$(pipeline)
    • fish: set output (pipeline)
  • process substitution
    • bash: join <(sort file1) <(sort file2)
    • fish: join (sort file1 | psub) (sort file2 | psub)

Documentation

  • bash: https://www.gnu.org/software/bash/manual/bashref.html
  • fish: http://fishshell.com/docs/current/index.html


来源:https://stackoverflow.com/questions/29667714/convert-bash-function-to-fishs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!