2015-04-16 13 views
6

Qualcuno può aiutarmi a convertire questa funzione bash in fish? Sarebbe anche bello se si potesse spiegare che cosa questi piace "${@%%.app}”, 's/ /.*/g’, "[email protected]\” eccConverti funzione bash in fish's

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 \"[email protected]\"" || echo "$location: $bundleid” 
} 

Grazie mille in anticipo.

+2

Come hai cercato di risolvere il problema da solo? – mcserep

risposta

16

Alcune note sulle differenze:

  • variabili di impostazione
    • bash: var=value
    • pesce: set var value
  • argomenti della funzione
  • funzione variabili locali
    • bash: local var
    • pesce: set -l var
  • condizionali I
    • bash: [[ ... ]] e [ ... ]
    • pesce: test ...
  • condizionali II
    • bash: if cond; then cmds; fi
    • pesce: if cond; cmds; end
  • condizionali III
    • bash: cmd1 && cmd2
    • pesce: cmd1; and cmd2
  • sostituzione di comando
    • bash: output=$(pipeline)
    • pesce: set output (pipeline)
  • sostituzione di processo
    • bash: join <(sort file1) <(sort file2)
    • pesce: join (sort file1 | psub) (sort file2 | psub)

Documentazione

+0

Ancora non riesco a capire l'equivalente di pesce di $ {@ %%. App} '. Credo di sapere che cosa fa restituire tutte le stringhe in '$ @' che contengono .app alla fine. Quindi penso che sarebbe '$ argv [**. App]' nel pesce ma dà errore _Could non expand_. – user14492

+0

'$ {@ %%. App}' restituisce una lista di tutti i parametri posizionali con eventuali estensioni ".app" rimosse. L'equivalente pesce è 'per arg in $ argv; imposta args $ args (echo "$ arg" | sed 's/.app $ //'); end' –

+0

Penso che potresti aver fatto un refuso in 'sed 's/.app $ //'' con $, nel commento sopra. Altrimenti potresti spiegare perché è lì. – user14492