函数是任何编程语言的一个重要组成部分,它包含一个代码块。通过调用一个函数,可以多次执行相同的代码,通过使用函数可以避免重复编写相同代码的要求。一个函数可以被定义为无参数和有参数。函数的参数可以是强制性的和可选的。可以为bash函数的参数设置默认值。本教程介绍了在bash函数中声明可选参数和使用默认值的方法。
例1:使用可选参数计算总和
本例展示了定义带有缺省值的可选参数的方法。用下面的脚本创建一个bash文件来计算两个数字的和。在该脚本中,名为sum()的函数包含两个带有默认值的可选参数。如果在调用该函数时没有给出参数,将计算默认值的总和。如果在调用函数时给出了一个参数,那么将计算该参数值与第二个默认值的总和。如果在调用函数时给了两个参数,那么将计算参数值的总和。接下来,函数的调用有不带任何参数的,有一个参数的,有两个参数的。
#!/bin/bash
#Declare function with mandatory and optional argument
functionsum()
{
#Set the values
num1=${1:-10}
num2=${2:-20}
#Return true if no argument is given
if [ $# -lt1 ]; then
echo "The optional argument values are: $num1, $num2."
#Return true if one argument is given
elif [ $# -lt2 ]; then
#Add new line
echo
echo "The optional argument value is: $num2."
else
#Add new line
echo
echo "There is no optional argument."
num1=$1
num2=$2
fi
#Calculate the sum of two numbers
sum=$((num1+num2))
echo "The sum of $num1 and $num2 is $sum"
}
#Call function without any argument
sum
#Call function with one argument
sum 40
#Call function with two arguments
sum 70 30
输出
执行上述脚本后会出现以下输出。当调用函数时没有任何参数,两个默认值的总和被打印出来,即30(10+20)。当调用有一个参数的函数时,打印出参数值(40)和第二个默认值(20)的总和,即60。当调用有两个参数的函数时,两个参数值的总和被打印为100(70+30)。
例2:使用默认值认证用户
默认值
用下面的脚本创建一个bash文件,当函数被调用而没有任何参数时,用默认的用户名和密码来认证用户。这个名为Authenticate()的函数包含两个可选的参数,即默认的用户名和密码。当这个函数在没有任何参数的情况下被调用时,默认值将被用来验证用户,并且将打印出访客登录的成功信息。当这个函数被调用时,带有有效的用户名和密码,管理员登录的成功信息将被打印。当这个函数被调用时,如果有一个无效的用户名和密码,将打印出错误信息。
#!/bin/bash
#Declare function for authentication
functionAuthenticate()
{
#Set the values
username=${1:-guest}
password=${2:-12345}
#Return true if no argument is given
if [[ $username == 'admin'&& $password == 'secret' ]]; then
echo "You have logged in as Administrator."
#Return true if one argument is given
elif [[ $username == 'guest'&& $password == '12345' ]]; then
echo "You have logged in as Guest."
else
echo "Invalid username and password."
fi
}
#Call function without any argument
Authenticate
#Call function with valid username and password
Authenticate admin secret
#Call function with invalid username and password
Authenticate fahmida 1234
输出
执行上述脚本后会出现以下输出。
例3:根据默认值计算奖金
用下面的脚本创建一个bash文件,在函数没有给出参数值的情况下,根据默认值计算奖金。在执行该脚本后,将从用户那里获取三个输入值。它们是基本工资、房屋租金和医疗津贴。名为 calculate_salary()的函数将根据该函数的参数值或默认值计算出奖金数额。工资总额将通过添加基本工资、房租、医疗和奖金数额的值来计算。calculate_salary()函数在调用时没有任何参数,或者有一个参数。
#!/bin/bash
#Take basic, house rent and medical allowance of an employee
echo "Enter basic Salary:"
read basic
echo "Enter house rent:"
read rent
echo "Enter medical allowance:"
read medical
#Declare function to calculate salary with bonus
functioncalculate_salary()
{
#Set the value
bonus=${1:-5}
#Calculate bonus
bonusAmount=$((basic*bonus/100))
#Calculate total salary
total=$((basic+rent+medical+bonusAmount))
#Print total salary with the bonus amount
echo "The total salary with $bonus % bonus is $total"
}
#Call function without the percentage of bonus
calculate_salary
#Call function with the percentage of bonus
calculate_salary 10
输出
执行该脚本后会出现以下输出。当函数被调用而没有任何参数时,默认值5被用来计算基于基本金额的奖金。当函数被调用时,参数值为10,奖金数额是根据这个值计算的。接下来,基于5%的奖金和10%的奖金的总工资已经打印出来。
总结
本教程通过三个不同的例子介绍了在函数中使用带有默认值的可选参数。默认值可以是数字或字符串。读完本教程后,bash用户将清楚在函数中使用带缺省值的可选参数的目的。