Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert bash function to fish's [closed]

Tags:

bash

shell

fish

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.

like image 631
user14492 Avatar asked Apr 16 '15 07:04

user14492


People also ask

Do bash scripts work in fish?

Regular bash scripts can be used in fish shell just as scripts written in any language with proper shebang or explicitly using the interpreter (i.e. using bash script.sh ). However, many utilities, such as virtualenv, modify the shell environment and need to be sourced, and therefore cannot be used in fish.

Can bash functions return values?

A bash function can return a value via its exit status after execution. By default, a function returns the exit code from the last executed command inside the function. It will stop the function execution once it is called. You can use the return builtin command to return an arbitrary number instead.

How do I run a bash function in terminal?

To invoke a bash function, simply use the function name. Commands between the curly braces are executed whenever the function is called in the shell script. The function definition must be placed before any calls to the function.

How do I run a fish shell script?

To be able to run fish scripts from your terminal, you have to do two things. Add the following shebang line to the top of your script file: #!/usr/bin/env fish . Mark the file as executable using the following command: chmod +x <YOUR_FISH_SCRIPT_FILENAME> .


1 Answers

Some notes on the differences:

  • setting variables
    • bash: var=value
    • fish: set var value
  • functions
    • bash
      funcName() {
          ...
      }
      
    • fish
      function funcName
          ...
      end
      
  • function arguments
    • bash: "$@", "$1", "$2", ...
    • fish: $argv, $argv[1], $argv[2], ...
  • function local variables
    • bash: local var
    • fish: set -l var
  • conditionals I
    • bash: [[ ... ]] and test ... and [ ... ]
    • fish: test ... and [ ... ], no [[ ... ]]
  • 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 and https://fishshell.com/docs/current/fish_for_bash_users.html
like image 120
glenn jackman Avatar answered Oct 27 '22 17:10

glenn jackman