局部变量与返回
局部变量与返回
局部变量
默认情况下,所有变量都是全局变量。在函数中修改变量会在整个脚本中对其进行更改。这可能会导致问题。例如,创建一个名为 fvar.sh 的 shell 脚本:
#!/bin/bash
create_jail(){
d=$1
echo "create_jail(): d is set to $d"
}
d=/apache.jail
echo "Before calling create_jail d is set to $d"
create_jail "/home/apache/jail"
echo "After calling create_jail d is set to $d"
# Sample outputs
Before calling create_jail d is set to /apache.jail
create_jail(): d is set to /home/apache/jail
After calling create_jail d is set to /home/apache/jail
您可以使用本地命令创建本地变量,语法为:
local var=value
local varName
function name(){
local var=$1
command1 on $var
}
local 命令只能在功能内使用。它使变量名的可见范围仅限于该函数及其子函数。以下是上述脚本的更新版本:
# global d variable
d=/apache.jail
# User defined function
create_jail(){
# d is only visible to this fucntion
local d=$1
echo "create_jail(): d is set to $d"
}
echo "Before calling create_jail d is set to $d"
create_jail "/home/apache/jail"
echo "After calling create_jail d is set to $d"
# Sample outputs
Before calling create_jail d is set to /apache.jail
create_jail(): d is set to /home/apache/jail
After calling create_jail d is set to /apache.jail
在以下示例中:
- 声明命令用于创建称为 PASSWD_FILE 的常量变量。
- 函数 die() 在所有其他函数之前定义。
- 您可以从同一脚本或其他函数调用一个函数。例如,从 is_user_exist() 中调用 die()。
- 所有函数变量都是局部的。这是一个好的编程习惯。
#!/bin/bash
# Make readonly variable i.e. constant variable
declare -r PASSWD_FILE=/etc/passwd
#
# Purpose: Display message and die with given exit code
#
die(){
local message="$1"
local exitCode=$2
echo "$message"
[ "$exitCode" == "" ] && exit 1 || exit $exitCode
}
#
# Purpose: Find out if user exits or not
#
does_user_exist(){
local u=$1
grep -qEw "^$u" $PASSWD_FILE && die "Username $u exists."
}
#
# Purpose: Is script run by root? Else die..
#
is_user_root(){
[ "$(id -u)" != "0" ] && die "You must be root to run this script" 2
}
#
# Purpose: Display usage
#
usage(){
echo "Usage: $0 username"
exit 2
}
[ $# -eq 0 ] && usage
# invoke the function is_root_user
is_user_root
# call the function is_user_exist
does_user_exist "$1"
# display something on screen
echo "Adding user $1 to database..."
# just display command but do not add a user to system
echo "/sbin/useradd -s /sbin/bash -m $1"
函数返回
在数学中,函数 ƒ 输入 x,然后返回输出 f(x)
。在计算机中,shell 函数名称可以输入 $1,然后将值(true 或 false)返回给脚本。换句话说,您可以从具有退出状态的函数中返回。return 命令使函数退出,返回值由 N 指定,语法为:
return N
如果未指定 N,则返回状态为最后一条命令的状态。return 命令终止该功能。当返回值是最后执行的命令的返回值时,则不需要返回命令。
#!/bin/bash
# version 1.0
# Purpose: Determine if current user is root or not
is_root_user(){
[ $(id -u) -eq 0 ]
}
# invoke the function
# make decision using conditional logical operators
is_root_user && echo "You can run this script." || echo "You need to run this script as a root user."
以下是同一脚本的更新版本。此版本使用称为 TRUE 和 FALSE 的声明命令创建常量变量。
#!/bin/bash
# version 2.0
# define constants
declare -r TRUE=0
declare -r FALSE=1
# Purpose: Determine if current user is root or not
is_root_user(){
# root user has user id (UID) zero.
[ $(id -u) -eq 0 ] && return $TRUE || return $FALSE
}
is_root_user && echo "You can run this script." || echo "You need to run this script as a root user."
您不能从函数中返回单词或其他任何内容。但是,您可以使用 echo 或 printf 命令轻松将输出发送回脚本。
#!/bin/bash
# Variables
domain="CyberCiti.BIz"
out=""
##################################################################
# Purpose: Converts a string to lower case
# Arguments:
# $@ -> String to convert to lower case
##################################################################
function to_lower()
{
local str="$@"
local output
output=$(tr '[A-Z]' '[a-z]'<<<"${str}")
echo $output
}
# invoke the to_lower()
to_lower "This Is a TEST"
# invoke to_lower() and store its result to $out variable
out=$(to_lower ${domain})
# Display back the result from $out
echo "Domain name : $out"