簡體   English   中英

在魚殼中調用另一個函數

[英]Calling another function in fish shell

我收到有關如何在zsh的功能轉換成魚功能出色答卷。 現在我還有一個問題。 如何從另一個函數調用該函數,傳遞參數?

我試過這個:

function ogf
  echo "Cloning, your editor will open when clone has completed..."
  source (env TARGET_DIRECTORY=~/students EDITOR=$EDITOR clone_git_file -ts $argv[1] | psub)
end

function wogf
  env EDITOR=webstorm ogf "$argv[1]"
end

但我得到“env:ogf:沒有這樣的文件或目錄”。

目標只是為這次執行更改EDITOR環境變量,然后調用ogf

env命令只能運行其他外部命令。 它不能調用shell內置函數或函數; 無論外殼是魚,還是其他東西。 解決方案是使用--no-scope-shadowing標志定義要調用的函數,並在調用函數中使用set -l

function ogf --no-scope-shadowing
  echo "Cloning, your editor will open when clone has completed..."
  source (env TARGET_DIRECTORY=~/students EDITOR=$EDITOR clone_git_file -ts $argv[1] | psub)
end

function wogf
  set -l EDITOR webstorm
  ogf $argv
end

另一種選擇是編寫您的函數以使用其自己的參數,如下所示:

function ogf
  echo "Cloning, your editor will open when clone has completed..."
  source (env TARGET_DIRECTORY=~/students EDITOR=$argv[2] clone_git_file -ts $argv[1] | psub)
end

function wogf
  ogf $argv[1] 'webstorm'
end

也許這是一個關於如何在傳遞參數時調用另一個函數的更簡單的例子:

function foo
  bar "hello" "world"
end

function bar
  echo $argv[1]
  echo $argv[2]
end

然后調用foo將打印:

$ foo
hello
world

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM