shell指令碼 之 函式返回值

2021-08-26 10:19:09 字數 744 閱讀 3410

shell 函式返回值

只允許返回數字,若不是則報 line 6: return: haha: numeric argument required

若是寫了return ,則返回return 語句跟的數值,若是沒有return語句則返回最後乙個命令的執行結果。

用例子說話吧

1 #!/bin/bash

2 3 returnnum()

6 7 returnlascommand()

10 11 returnstring=""

12 13 returnstring()

16 17 returnstring1()

20 21 r1=$(returnnum)

22 echo 'r1' $?

23 24 r2=$(returnlascommand)

25 echo 'r2' $?

26 27 returnstring

28 echo 'r3' $returnstring

29 30 returnstring1

inter12@inter12:~/myshell/shell/declare$ ./mm2 

r1 10

r2 0

r3 haha

./mm2: line 18: return: haha: numeric argument required

可以看到返回字串的時候報錯了!若是我們需要返回乙個字串怎麼半呢?那只能實用全域性變數的方式了!就是r3的方式

shell 指令碼獲取介面返回值

bin bash echo 任務 start result code echo 任務result if result code 1 then echo success exit 0 else echo failed exit 1 fi我這邊介面的返回值就是 int 型別的引數,所有返回值 resul...

shell函式返回值不是

shell中美元符號的使用 找了半天原因,就在上篇博文裡,我在用shell寫素數時,發現了乙個問題,老師所說的 代表函式返回值的說法並不準確,所以查了一下,然後給用 大家展示一下具體效果 最後執行的命令的結束 返回值 函式呼叫之後執行了一條 echo 這是乙個很簡單的shell 乙個函式,引數為 i...

函式之返回值

返回值 將函式中運算的結果通過 return defadd a,b result a b print result return hello 100 呼叫函式 x,y add 2,6 print x,y return 返回值 1.return後面可以是乙個引數 接受的時候x add 1,2 2.re...