一,
函数的使用
1,
函数是一系列的指令和命令;本身不运行,可以多次调用,可以简化代码;
2,
语法
函数()
{
命令1;命令2;
return #返回值
}
3,
[root@rhel helinbash]# ./function.sh
[root@rhel helinbash]# cat lib.sh
#!/bin/bash
p_red()
{
TEXT="Nice to meet you"
echo -e "\033[34m$TEXT\033[0m"
}
[root@rhel helinbash]#
[root@rhel helinbash]# ./function.sh
Nice to meet you
Nice to meet you
Nice to meet you
[root@rhel helinbash]# cat function.sh
#!/bin/bash
p_red()
{
TEXT="Nice to meet you"
echo -e "\033[34m$TEXT\033[0m"
}
p_red
p_red
p_red
[root@rhel helinbash]#
4, 函数的参数;函数也可以作为一个程序一样运行,接收参数fname $arg1 $arg2;这个shell中位置参数一样,我们可以对$1,$2进行测试
[root@rhel helinbash]# cat function.sh
#!/bin/bash
p_red()
{
TEXT=$1
echo -e "\033[34m$TEXT\033[0m"
}
p_red "Ok"
p_red "wrong"
p_red "nihao"
[root@rhel helinbash]#
[root@rhel helinbash]# cat function.sh
#!/bin/bash
p_red()
{
TEXT=$1
echo -e "\033[34m$TEXT\033[0m"
}
if ping -c2 172.24.254.254 &> /dev/null ; then
p_red "Ok"
else
p_red "wrong"
fi
[root@rhel helinbash]#
5,
[root@rhel helinbash]# ./function.sh
100
100
[root@rhel helinbash]# vim function.sh
[root@rhel helinbash]# ./function.sh
100
105
[root@rhel helinbash]# cat function.sh
#!/bin/bash
p_red()
{
TEXT=$1
echo -e "\033[34m$TEXT\033[0m"
}
max()
{
NUMA=$1
NUMB=$2
if [ $NUMA -gt $NUMB ] ; then
return $NUMA
else
return $NUMB
fi
}
max 99 100
RES=$?
echo $RES
max 105 76
RES=$?
echo $RES
#if ping -c2 172.24.254.254 &> /dev/null ; then
# p_red "Ok"
#else
# p_red "wrong"
#fi
[root@rhel helinbash]#
6,
[root@rhel helinbash]# ./testfunction.sh
100
100
[root@rhel helinbash]# cat testfunction.sh
#!/bin/bash
source ./function.sh
max 100 98
RES=$?
echo $RES
p_red $RES
[root@rhel helinbash]# cat function.sh
#!/bin/bash
p_red()
{
TEXT=$1
echo -e "\033[34m$TEXT\033[0m"
}
max()
{
NUMA=$1
NUMB=$2
if [ $NUMA -gt $NUMB ] ; then
return $NUMA
else
return $NUMB
fi
}
[root@rhel helinbash]#
二, getopts
1,
用来检测命令行中传递给脚本的有效参数;通常用在while loop中。
2, 语法:
getopts
包含了要识别的选项字符;如果字符前存在"-"(横杠),而此选项期待着有一个参数,它们之间用空格空开。
3, 每次调用getopts的时候,它将下一选项设置给当选项需要参数时,getopts将参数设置给变量OPTARG如果选项不正确,返回? 给
4, 备份数据库脚本框架
backup [-s xxx] [-d xxx] [-u xxx] [-p xxx]
[root@rhel helinbash]# ./backupdb.sh -u student
backup source database --> test to dir ---> /tmp by user ---> student
[root@rhel helinbash]# ./backupdb.sh -u student -d /var/tmp
backup source database --> test to dir ---> /var/tmp by user ---> student
[root@rhel helinbash]# ./backupdb.sh -u student -d /var/tmp -s mysql
backup source database --> mysql to dir ---> /var/tmp by user ---> student
[root@rhel helinbash]# vim .backupdb.sh
[root@rhel helinbash]# vim backupdb.sh
[root@rhel helinbash]# ./backupdb.sh -u student -d /var/tmp -s mysql -h 127.0.0.1
./backupdb.sh: illegal option -- h
./backupdb.sh [ -s src ] [ -d dst ] [ -u user ]
[root@rhel helinbash]# cat backupdb.sh
#!/bin/bash
SOURCE="test"
DEST="/tmp"
USER="root"
while getopts s:d:u: OPT
do
case $OPT in
"s")
SOURCE=$OPTARG
;;
"d")
DEST=$OPTARG
;;
"u")
USER=$OPTARG
;;
\?)
echo "$0 [ -s src ] [ -d dst ] [ -u user ]"
exit -1
esac
done
echo " backup source database --> $SOURCE to dir ---> $DEST by user ---> $USER"
[root@rhel helinbash]#
1,
函数是一系列的指令和命令;本身不运行,可以多次调用,可以简化代码;
2,
语法
函数()
{
命令1;命令2;
return #返回值
}
3,
[root@rhel helinbash]# ./function.sh
[root@rhel helinbash]# cat lib.sh
#!/bin/bash
p_red()
{
TEXT="Nice to meet you"
echo -e "\033[34m$TEXT\033[0m"
}
[root@rhel helinbash]#
[root@rhel helinbash]# ./function.sh
Nice to meet you
Nice to meet you
Nice to meet you
[root@rhel helinbash]# cat function.sh
#!/bin/bash
p_red()
{
TEXT="Nice to meet you"
echo -e "\033[34m$TEXT\033[0m"
}
p_red
p_red
p_red
[root@rhel helinbash]#
4, 函数的参数;函数也可以作为一个程序一样运行,接收参数fname $arg1 $arg2;这个shell中位置参数一样,我们可以对$1,$2进行测试
[root@rhel helinbash]# cat function.sh
#!/bin/bash
p_red()
{
TEXT=$1
echo -e "\033[34m$TEXT\033[0m"
}
p_red "Ok"
p_red "wrong"
p_red "nihao"
[root@rhel helinbash]#
[root@rhel helinbash]# cat function.sh
#!/bin/bash
p_red()
{
TEXT=$1
echo -e "\033[34m$TEXT\033[0m"
}
if ping -c2 172.24.254.254 &> /dev/null ; then
p_red "Ok"
else
p_red "wrong"
fi
[root@rhel helinbash]#
5,
[root@rhel helinbash]# ./function.sh
100
100
[root@rhel helinbash]# vim function.sh
[root@rhel helinbash]# ./function.sh
100
105
[root@rhel helinbash]# cat function.sh
#!/bin/bash
p_red()
{
TEXT=$1
echo -e "\033[34m$TEXT\033[0m"
}
max()
{
NUMA=$1
NUMB=$2
if [ $NUMA -gt $NUMB ] ; then
return $NUMA
else
return $NUMB
fi
}
max 99 100
RES=$?
echo $RES
max 105 76
RES=$?
echo $RES
#if ping -c2 172.24.254.254 &> /dev/null ; then
# p_red "Ok"
#else
# p_red "wrong"
#fi
[root@rhel helinbash]#
6,
[root@rhel helinbash]# ./testfunction.sh
100
100
[root@rhel helinbash]# cat testfunction.sh
#!/bin/bash
source ./function.sh
max 100 98
RES=$?
echo $RES
p_red $RES
[root@rhel helinbash]# cat function.sh
#!/bin/bash
p_red()
{
TEXT=$1
echo -e "\033[34m$TEXT\033[0m"
}
max()
{
NUMA=$1
NUMB=$2
if [ $NUMA -gt $NUMB ] ; then
return $NUMA
else
return $NUMB
fi
}
[root@rhel helinbash]#
二, getopts
1,
用来检测命令行中传递给脚本的有效参数;通常用在while loop中。
2, 语法:
getopts
包含了要识别的选项字符;如果字符前存在"-"(横杠),而此选项期待着有一个参数,它们之间用空格空开。
3, 每次调用getopts的时候,它将下一选项设置给当选项需要参数时,getopts将参数设置给变量OPTARG如果选项不正确,返回? 给
4, 备份数据库脚本框架
backup [-s xxx] [-d xxx] [-u xxx] [-p xxx]
[root@rhel helinbash]# ./backupdb.sh -u student
backup source database --> test to dir ---> /tmp by user ---> student
[root@rhel helinbash]# ./backupdb.sh -u student -d /var/tmp
backup source database --> test to dir ---> /var/tmp by user ---> student
[root@rhel helinbash]# ./backupdb.sh -u student -d /var/tmp -s mysql
backup source database --> mysql to dir ---> /var/tmp by user ---> student
[root@rhel helinbash]# vim .backupdb.sh
[root@rhel helinbash]# vim backupdb.sh
[root@rhel helinbash]# ./backupdb.sh -u student -d /var/tmp -s mysql -h 127.0.0.1
./backupdb.sh: illegal option -- h
./backupdb.sh [ -s src ] [ -d dst ] [ -u user ]
[root@rhel helinbash]# cat backupdb.sh
#!/bin/bash
SOURCE="test"
DEST="/tmp"
USER="root"
while getopts s:d:u: OPT
do
case $OPT in
"s")
SOURCE=$OPTARG
;;
"d")
DEST=$OPTARG
;;
"u")
USER=$OPTARG
;;
\?)
echo "$0 [ -s src ] [ -d dst ] [ -u user ]"
exit -1
esac
done
echo " backup source database --> $SOURCE to dir ---> $DEST by user ---> $USER"
[root@rhel helinbash]#
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29611940/viewspace-1180985/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29611940/viewspace-1180985/