簡體   English   中英

用可選參數編​​寫魚殼腳本

[英]Writing A Fish Shell Script With an Optional Argument

我有一個fish shell腳本,其默認行為是在完成后發送電子郵件。 我想修改它以響應命令行中的nomail參數。 因此,例如,正常運行腳本會生成一封電子郵件:

michaelmichael: ~/bin/myscript

但如果使用nomail開關運行,它將不會發送確認電子郵件:

michaelmichael: ~/bin/myscript nomail

如果我使用nomail參數運行腳本,它運行正常。 沒有nomail$argv是未定義的,它會拋出錯誤。 我已經搜索了魚殼文檔,但似乎找不到任何可行的東西。 這是我到目前為止所擁有的

switch $argv
  case nomail
    ## Perform normal script functions
  case ???
    ## Perform normal script functions
    mailx -s "Script Done!"
end

運行此命令會引發以下錯誤:

switch: Expected exactly one argument, got 0

顯然它需要一個參數,我只是不知道告訴它不接受任何參數的語法,或者它是否存在的語法。

我猜這是非常基本的,但我只是不太了解shell腳本。

像這樣包裝你的switch語句:

if set -q argv
    ...
end

另外,我認為你的默認情況應該是case '*'

如果您更喜歡使用switch語句,那么它也是可能的:

switch (echo $argv)
  case nomail
    ## Perform normal script functions
  case '*'
    ## Perform normal script functions
    mailx -s "Script Done!"
end

暫無
暫無

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

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