shell函数
为什么要使用shell函数
linux别的的作用
-
[[email?protected] ~]# alias
-
alias cp=‘cp -i‘
-
alias l.=‘ls -d .* --color=auto‘
-
alias ll=‘ls -l --color=auto‘
-
alias ls=‘ls --color=auto‘
-
alias mv=‘mv -i‘
-
alias rm=‘rm -i‘
-
alias vi=‘vim‘
-
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
设置别名与使用
-
[[email?protected] ~]# alias cdone=‘cd /‘
-
[[email?protected] ~]# alias
-
alias cdone=‘cd /‘
-
alias cp=‘cp -i‘
-
alias l.=‘ls -d .* --color=auto‘
-
alias ll=‘ls -l --color=auto‘
-
alias ls=‘ls --color=auto‘
-
alias mv=‘mv -i‘
-
alias rm=‘rm -i‘
-
alias vi=‘vim‘
-
alias which=‘alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde‘
-
[[email?protected] ~]# cdone
-
[[email?protected] /]# pwd
-
/
函数也是具有和别名类似的功能。函数的作用就是把程序里多次调研相同的代码部分定义成一份,然后为这一份代码起个名字,其它所有的重复调用这部分代码都只用调用这个名字就可以。当需要修改这部分重复代码时,只需要改变函数体内的一份代码即可实现调用修改。
使用函数的优势:
1、把相同的程序段定义成函数,可以减少整个程序的代码量。
2、增加程序的可读性,以及便于管理。
3、可实现程序功能模块化,不同的程序使用函数模块化。
4、让程序代码结构更清晰。
shell函数语法
语法格式:
-
简单语法格式
-
函数名(){
-
??指令...
-
??return n
-
}
-
规范语法格式
-
function 函数名(){
-
??指令...
-
??return n
-
}
注意:shell的返回值是exit,函数里用return输出返回值。
shell函数的执行
调用函数
1)直接执行函数名即可(不带括号)。
函数名
注意:1、执行函数时,函数后的小括号不需要。2、函数定义及函数体必须在要执行的函数名的前面定义,shell的执行从上到下按行执行。
2)带参数的函数执行方法。
函数名 参数1 参数2
注意:函数的传参和脚本的传参类似,只是脚本名换成函数名即可。
函数后接的参数说明:1、shell的位置参数($1、$2、$3、$4、$5、$#、$*、$?、[email?protected])都可以是函数的参数。2、此时父脚本的参数临时地被函数参数所掩盖或隐藏。3、$0比较特殊,它仍然是父脚本的名称。4、当函数完成时,原来的命令行脚本的参数即恢复。5、在shell函数里面,return命令功能与shell里的exit类似,作用是跳出函数。6、在shell函数体里使用exit会退出整个shell脚本,而不是退出shell函数。7、return语句会返回一个退出值(返回值)给调用函数的程序。8、函数的参数变量是在函数体里面定义,如果是普通变量,一般使用local i定义。
shell函数范例
开发脚本建立两个简单函数并调用执行。
-
[[email?protected] /]# cat fun01.sh
-
#!/bin/bash
-
test_fun(){
-
??echo "i am shell fun."
-
}
-
test_fun
-
[[email?protected] /]# sh fun01.sh
-
i am shell fun.
调用其它脚本文件中的函数。
-
[[email?protected] /]# cat fun01.sh
-
#!/bin/bash
-
. /fun02.sh
-
test_fun(){
-
??echo "i am shell fun."
-
}
-
test_fun
-
test_fun02
-
[[email?protected] /]# cat fun02.sh
-
#!/bin/bash
-
test_fun02(){
-
??echo "i am shell fun02."
-
}
-
[[email?protected] /]# sh fun01.sh
-
i am shell fun.
-
i am shell fun02.
传参
-
[[email?protected] /]# cat fun01.sh
-
#!/bin/bash
-
. /fun02.sh
-
test_fun(){
-
??echo "i am shell fun."
-
}
-
test_fun
-
test_fun02 $1
-
[[email?protected] /]# cat fun02.sh
-
#!/bin/bash
-
test_fun02(){
-
??echo "i am shell $1."
-
}
-
[[email?protected] /]# sh fun01.sh golden
-
i am shell fun.
-
i am shell golden.
函数传参转成参数命令行传输,对任意指定url判断是否异常。
-
[[email?protected] /]# curl -I -m 3 -o /dev/null -s -w %{http_code} www.baidu.com
-I 仅测试HTTP头
-m 3 最多查询3秒
-o /dev/null 屏蔽原有输出信息
-s silent 模式,不输出任何东西
-w %{http_code} 控制额外输出
-
[[email?protected] ~]# cat check_url.sh
-
#!/bin/bash
-
[ -f /etc/init.d/functions ]&& . /etc/init.d/functions
-
usage(){
-
??echo "USAGE:$0 url."
-
??exit 1
-
}
-
RETVAL=0
-
check(){
-
??wget -I 10 --spider -t 2 $1 &>/dev/null
-
??RETVAL=$?
-
??if [ $RETVAL -eq 0 ];then
-
????action "$1 url" /bin/true
-
??else
-
????action "$1 url" /bin/false
-
??fi
-
??return $RETVAL
-
}
-
main(){
-
??if [ $# -ne 1 ];then
-
????usage
-
??fi
-
??check $1
-
}
-
main $*
-
[[email?protected] ~]# sh check_url.sh www.baidu.com
-
www.baidu.com url [ OK ]
-
[[email?protected] ~]# sh check_url.sh www.baiduxxxx.com
-
www.baiduxxxx.com url [FAILED]
给字符串加颜色。
-
[[email?protected] ~]# cat color.sh
-
#!/bin/bash
-
RED_COLOR=‘E[1;31m‘
-
GREEN_COLOR=‘E[1;32m‘
-
YELLOW_COLOR=‘E[1;33m‘
-
BLUE_COLOR=‘E[1;34m‘
-
PINK=‘E[1;35m‘
-
RES=‘E[0m‘
-
echo -e "$RED_COLOR red $RES"
-
echo -e "$GREEN_COLOR GREEN $RES"
-
echo -e "$YELLOW_COLOR YELLOW $RES"
-
echo -e "$BLUE_COLOR BLUE $RES"
-
echo -e "$PINK PINK $RES"
输出结果。

传2个参数,颜色名称和内容,输出带颜色的内容。
-
[[email?protected] ~]# cat color_str.sh
-
#!/bin/bash
-
RED=‘E[1;31m‘
-
GREEN=‘E[1;32m‘
-
YELLOW=‘E[1;33m‘
-
BLUE=‘E[1;34m‘
-
PINK=‘E[1;35m‘
-
RES=‘E[0m‘
-
usage(){
-
??echo "USAGE:$0 color contents."
-
??exit 1
-
}
-
color(){
-
??if [ "$1" = "red" ];then
-
????echo -e "${RED}$2 $RES"
-
??elif [ "$1" = "green" ];then
-
????echo -e "${GREEN}$2 $RES"
-
??elif [ $1 = "yellow" ];then
-
????echo -e "${YELLOW}$2 $RES"
-
??elif [ "$1" = "blue" ];then
-
????echo -e "${BLUE}$2 $RES"
-
??elif [ "$1" = "pink" ];then
-
????echo -e "${PINK}$2 $RES"
-
??else
-
????echo "$2"
-
??fi
-
}
-
main(){
-
??if [ $# -ne 2 ];then
-
????usage
-
??fi
-
??color $1 $2
-
}
-
main $*
输出结果。

case结构条件句
case结构条件句语法
case语句实际上就是规范的多分支if语句。
-
case "字符串变量" in
-
??值1) 指令1...
-
;;
-
??值2) 指令2...
-
;;
-
??*) 指令3...
-
esac
case结构条件句范例
根据用户的输入判断是哪个数字。如果用户输入数字输出对应输入的数字,如果是其他内容返回不正确。
-
[[email?protected] ~]# cat case.sh
-
#!/bin/bash
-
usage(){
-
??echo "USAGE:$0 number."
-
??exit 1
-
}
-
case_fun(){
-
??case $1 in
-
????[1-3])
-
??????echo $1
-
??;;
-
????*)
-
??????echo "input error."
-
??esac
-
}
-
main(){
-
??case $# in
-
????1) case_fun $1
-
??;;
-
????*) usage
-
??esac
-
}
-
main $*
输出结果。
-
[[email?protected] ~]# sh case.sh
-
USAGE:case.sh number.
-
[[email?protected] ~]# sh case.sh 1
-
1
-
[[email?protected] ~]# sh case.sh 2
-
2
-
[[email?protected] ~]# sh case.sh 3
-
3
-
[[email?protected] ~]# sh case.sh 4
-
input error.
执行脚本打印一个水果菜单:
1、apple
2、pear
3、banana
4、cherry
当用户选择水果的时候,打印选择水果是什么,并给水果单词加上颜色。
-
[[email?protected] ~]# cat fruit.sh
-
#!/bin/bash
-
RED=‘E[1;31m‘
-
GREEN=‘E[1;32m‘
-
YELLOW=‘E[1;33m‘
-
BLUE=‘E[1;34m‘
-
PINK=‘E[1;35m‘
-
RES=‘E[0m‘
-
FLICKER=‘E[31;5m‘
-
usage(){
-
??echo -e "${FLICKER}Pls select the exist num behind. ${RES}"
-
??exit 1
-
}
-
choice(){
-
??case $num in
-
????1) echo -e "${BLUE}apple${RES}"
-
??;;
-
????2) echo -e "${GREEN}pear${RES}"
-
??;;
-
????3) echo -e "${YELLOW}banana${RES}"
-
??;;
-
????4) echo -e "${RED}cherry${RES}"
-
??;;
-
????*) usage
-
??esac
-
}
-
main(){
-
??choice $num
-
}
-
echo "
-
??1、apple
-
??2、pear
-
??3、banana
-
??4、cherry"
-
read -t 10 -p "Pls input a num:" num
-
main $num
echo输出字符串显示不同颜色。
-
[[email?protected] ~]# echo -e "