函數的格式
使用 function定義一個函數
function 函數名()
{
函數體
}
#!/bin/bash
function sum()
{
s=0;
s=$[$1 + $2];
echo "求和的結果爲: "$s;
}
read -p "請輸入需要求和的第一個數: num1=" num1
read -p "請輸入需要求和的第二個數: num2=" num2
sum $num1 $num2
運行結果:
[root@controller test_shell]# sh fun.sh
請輸入需要求和的第一個數: num1=2
請輸入需要求和的第二個數: num2=2
求和的結果爲: 4
先定義後使用,否則會報錯