函数定义
函数定义
要定义函数,请使用以下语法:
name() compound_command ## POSIX compliant
## see the bash man page for def. of a compound command
function name { ## ksh style works in bash
command1
command2
}
function name() { ## bash-only hybrid
command1
command2
}
function name { command1; command2; commandN;}
name() { command1; command2; commandN;}
其中,name 是函数的名称,“command1; command2;”是函数中使用的命令的列表。您需要将名称替换为实际的函数名称,例如 delete_account:
rollback(){
...
}
add_user(){
...
}
delete_user(){
...
}
案例:NAS 挂载
定义一个名为 mount_nas 和 umount_nas 的函数:
# function to mount NAS device
mount_nas(){
# define variables
NASMNT=/nas10
NASSERVER="nas10.nixcraft.net.in"
NASUSER="vivek"
NASPASSWORD="myNasAccountPassword"
[ ! -d $NASMNT ] && /bin/mkdir -p $NASMNT
mount | grep -q $NASMNT
[ $? -eq 0 ] || /bin/mount -t cifs //$NASSERVER/$NASUSER -o username=$NASUSER,password=$NASPASSWORD $NASMNT
}
# function to unmount NAS device
umount_nas(){
NASMNT=/nas10
mount | grep -q $NASMNT
[ $? -eq 0 ] && /bin/umount $NASMNT
}
您可以在 Shell 脚本的开头键入函数:
#!/bin/bash
# define variables
NASMNT=/nas10
....
..
....
# define functions
function umount_nas(){
/bin/mount | grep -q $NASMNT
[ $? -eq 0 ] && /bin/umount $NASMNT
}
# another function
functiom mount_nas(){
command1
command2
}
....
...
### main logic ##
[ $? -eq 0 ] && { echo "Usage: $0 device"; exit 1; }
...
.....
# When you wish to access function, you use the following format:
umount_nas
函数移除
要取消设置或删除该功能,请使用 unset 命令,如下所示:
unset -f functionName
unset -f hello
declare
函数参数
Shell 函数具有自己的命令行参数。使用变量 $1,$2 .. $n 访问传递给函数的参数。语法如下:
name(){
arg1=$1
arg2=$2
command on $arg1
}
要调用该函数,请使用以下语法:
name foo bar
- name = function name.
- foo = Argument # 1 passed to the function (positional parameter # 1).
- bar = Argument # 2 passed to the function.
创建一个名为 fresh.sh 的函数:
#!/bin/bash
# write a function
fresh(){
# t stores $1 argument passed to fresh()
t=$1
echo "fresh(): \$0 is $0"
echo "fresh(): \$1 is $1"
echo "fresh(): \$t is $t"
echo "fresh(): total args passed to me $#"
echo "fresh(): all args (\$@) passed to me -\"$@\""
echo "fresh(): all args (\$*) passed to me -\"$*\""
}
# invoke the function with "Tomato" argument
echo "**** calling fresh() 1st time ****"
fresh Tomato
# invoke the function with total 3 arguments
echo "**** calling fresh() 2nd time ****"
fresh Tomato Onion Paneer
- 可以通过以下方式访问所有函数参数或参数 $1, $2, $3,…, $N.
$0
始终指向外壳脚本名称。$*
or$@
保存传递给函数的所有参数或参数。$#
保留传递给函数的位置参数的数量。
#!/bin/bash
# funcback.sh : Use $FUNCNAME
backup(){
local d="$1"
[[ -z $d ]] && { echo "${FUNCNAME}(): directory name not specified"; exit 1; }
echo "Starting backup..."
}
backup $1
创建一个 shell 脚本以确定给定名称是文件还是目录(cmdargs.sh):
#!/bin/bash
file="$1"
# User-defined function
is_file_dir(){
# $f is local variable
local f="$1"
# file attributes comparisons using test i.e. [ ... ]
[ -f "$f" ] && { echo "$f is a regular file."; exit 0; }
[ -d "$f" ] && { echo "$f is a directory."; exit 0; }
[ -L "$f" ] && { echo "$f is a symbolic link."; exit 0; }
[ -x "$f" ] && { echo "$f is an executeble file."; exit 0; }
}
# make sure filename supplied as command line arg else die
[ $# -eq 0 ] && { echo "Usage: $0 filename"; exit 1; }
# invoke the is_file_dir and pass $file as arg
is_file_dir "$file"
# Sample outputs
./cmdargs.sh /etc/resolv.conf
./cmdargs.sh /bin/date
./cmdargs.sh $HOME
./cmdargs.sh /sbin
/etc/resolv.conf is a regular file.
/bin/date is a regular file.
/home/vivek is a directory.
/sbin is a directory.