shell中getops的用法_[17]shell中的函数使用和getopts使用及备份脚本框架实现

一,函数的使用

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 --&gt test  to  dir ---&gt /tmp by user ---&gt student

[root@rhel helinbash]# ./backupdb.sh -u student -d /var/tmp

backup source database --&gt test  to  dir ---&gt /var/tmp by user ---&gt student

[root@rhel helinbash]# ./backupdb.sh -u student -d /var/tmp -s mysql

backup source database --&gt mysql  to  dir ---&gt /var/tmp by user ---&gt 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 --&gt $SOURCE  to  dir ---&gt $DEST by user ---&gt $USER"

[root@rhel helinbash]#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值