bash脚本编程之在bash脚本中使用选项

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了bash脚本编程之在bash脚本中使用选项相关的知识,希望对你有一定的参考价值。

[[email protected] ~]# vim a

#!/bin/bash

# Name:abc

# Description:Create script

# Author:mylinux

# Version:0.0.1

# Datatime:03/02/12 14:42:00

# Usage:mkscript FILENAME

cat >$1  <<EOF

#!/bin/bash

# Name: `basename $1`

# Description:

# Author: mylinux

# Version: 0.0.1

# Datatime: `date +‘%F %T‘`

# Usage: `basename $1`


EOF


vim +8 $1



---------------------

[[email protected] ~]# bash a test1.sh 运行此脚本自动生成

#!/bin/bash

# Name: test1.sh

# Description:

# Author: mylinux

# Version: 0.0.1

# Datatime: 2016-09-17 18:16:45

# Usage: test1.sh


-----------------------------------~              

再判断如果有内容就直接打开 没内容才写入

[[email protected] ~]# vim a


#!/bin/bash

# Name:abc

# Description:Create script

# Author:mylinux

# Version:0.0.1

# Datatime:03/02/12 14:42:00

# Usage:mkscript FILENAME

if grep "[^[:space:]]" $1 &>/dev/null;then  #grep非空白行

vim + $1        +表示打开最后一行

else

cat >$1  <<EOF

#!/bin/bash

# Name: `basename $1`

# Description:

# Author: mylinux

# Version: 0.0.1

# Datatime: `date +‘%F %T‘`

# Usage: `basename $1`


EOF


vim +8  $1

fi

---------------------------------------

保存退出后自动检查语法。

#!/bin/bash

# Name:abc

# Description:Create script

# Author:mylinux

# Version:0.0.1

# Datatime:03/02/12 14:42:00

# Usage:mkscript FILENAME

if grep "[^[:space:]]" $1 &>/dev/null;then

vim + $1

else

cat >$1  <<EOF

#!/bin/bash

# Name: `basename $1`

# Description:

# Author: mylinux

# Version: 0.0.1

# Datatime: `date +‘%F %T‘`

# Usage: `basename $1`


EOF


vim +8  $1

fi

until bash -n $1 &>/dev/null ;do

read -p "Syntax,error,q|Q for quiting, others for editing: " OPT

case $OPT in

q|Q)

 echo "quit."

 exit 1

 ;;

*)

vim + $1

;;

esac

done

chmod +x $1

----------------------------------------------

getopts:让脚本可以获取选项


[[email protected] ~]# bash a  opttest.sh

#!/bin/bash

# Name: opttest.sh

# Description:

# Author: mylinux

# Version: 0.0.1

# Datatime: 2016-09-13 01:57:50

# Usage: opttest.sh

getopts "bd" OPT

echo $OPT


[[email protected] ~]# bash opttest.sh  -b

b

[[email protected] ~]# bash opttest.sh  -d

d


getopts 默认情况下只能获取一个选项



OPTARG:表示某一个变量的参数,需要带参数的选项后面必须带: 

例如getopts "bd:" OPT

getopts "b:d:" OPT




#!/bin/bash

# Name: opttest.sh

# Description:

# Author: mylinux

# Version: 0.0.1

# Datatime: 2016-09-13 01:57:50

# Usage: opttest.sh

getopts "bd:" OPT

echo $OPT

echo $OPTARG


[[email protected] ~]# bash opttest.sh  -d "haha"

d

haha

--------------------------------

在所有选项最前面输入:表示不显示错误信息

#!/bin/bash

getopts ":b:d:" OPT

echo $OPT

echo $OPTARG

[[email protected] ~]# bash opttest.sh  -c

a b

c

-----------------------------

让一个脚本获取多个参数

#!/bin/bash

# Name: opttest.sh

# Description:

# Author: mylinux

# Version: 0.0.1

# Datatime: 2016-09-13 01:57:50

# Usage: opttest.sh

function USAGE {

echo "Usage: opttest.sh [-d argu] [-b argu]"

}

while getopts ":b:d:" RT;do

case $RT in

b) echo "The options is b."

   echo $OPTARG

   ;;

d) echo "The options is d."

   echo $OPTARG

   ;;

*) USAGE

   ;;

esac

done

-----------------------------------------------------------

[[email protected] ~]# bash  a -d "hello" /tmp/a

OPTIND:选项索引,这里表示  -d , "hello"  , /tmp/a这三个位置变量

-1代表不包含/tmp/a

shift $[$OPTIND-1] 


最终完善版:

写一个脚本:用于创建脚本,并cat注释信息,并检查语法错误。如果语法错误则。提示用户是否打开修改

[[email protected] ~]#vim a

#!/bin/bash

# Name:abc

# Description:Create script

# Author:mylinux

# Version:0.0.1

# Datatime:03/02/12 14:42:00

# Usage:mkscript FILENAME

while getopts ":d:" OPTA;do

  case $OPTA in

     d)

       DESC=$OPTARG

       shift $[$OPTIND-1];;

     *)

       echo "Usage:mkscript [-d DESCRIPTION] FILENAME"

       shift $[$OPTIND-1];;

  esac

done

if ! grep "[^[:space:]]" $1 &>/dev/null;then

cat > $1 <<EOF

#!/bin/bash

# Name: `basename $1`

# Description: $DESC 

# Author: mylinux

# Version: 0.0.1

# Datatime: `date +‘%F %T‘`

# Usage: `basename $1`


EOF

fi


vim + $1


until bash -n $1 &>/dev/null ;do

  read -p "Syntax,error,q|Q for quiting, others for editing: " OPT

  case $OPT in

    q|Q)

       echo "quit."

       exit 1 ;;

    *)

       vim + $1;;

  esac

done

chmod +x $1

-----------------------------------------------------------------------

getopts: 

OPTARG

OPTIND

----------------------------------------

写一个脚本getinterface.sh,脚本可以接受选项(i,I,a)完成以下任务:

1.使用以下形式:getinterface.sh [-i interface|I IP|-a]

2.当用户使用-i选项时,显示其指定网卡的IP地址;

3.当用户使用-I选项时,显示其后面的IP地址所属的网络接口

#4.当用户单独使用-a选项时,显示所有网络接口及其IP地址(lo除外)


#!/bin/bash

# Name: getinterface.sh

# Description: Get ethernet information 

# Author: mylinux

# Version: 0.0.1

# Datatime: 2016-09-13 07:34:56

# Usage: getinterface.sh

function SHOWIP {

if ! ifconfig|grep -o "^[^[:space:]]\{1,\}"|grep $1 &>/dev/null;then

 return 13

fi

echo -n "${1}: "

ifconfig $1 |grep -o "inet addr:[0-9\.]\{1,\}" |cut -d: -f 2      # \{1,\}表示引用前面至少1次

echo

}


SHOWETHER () {

if ! ifconfig  |grep -o "inet addr:[0-9\.]\{1,\}" |cut -d: -f 2|grep $1 &>/dev/null ;then

  return 14

fi

echo -n "${1}: "

ifconfig |grep -B 1 "$1" |grep -o "^[^[:space:]]\{1,\}"       #grep -B显示grep到的结果和结果的上一行

echo

}

USAGE() {

echo "getinterface.sh <-i interface|-I IP>"

}

while getopts ":i:I:" SWICH; do

case $SWICH in

i)

SHOWIP $OPTARG

[ $? -eq 13 ] && echo "Wrong ehtercard"

;;

I)

SHOWETHER $OPTARG

[ $? -eq 14 ] && echo "Wrong IP."

;;

*)

USAGE

;;

esac

done

----------------------------------


                         

           


本文出自 “运维成长路” 博客,谢绝转载!

以上是关于bash脚本编程之在bash脚本中使用选项的主要内容,如果未能解决你的问题,请参考以下文章

Lubuntu中的基本菜单BASH脚本

将我的 bash 脚本变成守护进程的选项

第七课-第二讲 07_02_bash脚本编程之六 使用脚本选项及组合条件测试

第七课-第二讲 07_02_bash脚本编程之六 使用脚本选项及组合条件测试

在 bash 脚本中嵌入简短的 python 脚本

收到错误mailx:非法选项-在bash脚本中添加附件时