bash腳本編程之在bash腳本中使用選項

[root@localhost ~]# 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



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

[root@localhost ~]# 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


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

再判斷如果有內容就直接打開 沒內容才寫入

[root@localhost ~]# 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:讓腳本可以獲取選項


[root@localhost ~]# 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


[root@localhost ~]# bash opttest.sh  -b

b

[root@localhost ~]# 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


[root@localhost ~]# bash opttest.sh  -d "haha"

d

haha

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

在所有選項最前面輸入:表示不顯示錯誤信息

#!/bin/bash

getopts ":b:d:" OPT

echo $OPT

echo $OPTARG

[root@localhost ~]# 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

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

[root@localhost ~]# bash  a -d "hello" /tmp/a

OPTIND:選項索引,這裏表示  -d , "hello"  , /tmp/a這三個位置變量

-1代表不包含/tmp/a

shift $[$OPTIND-1] 


最終完善版:

寫一個腳本:用於創建腳本,並cat註釋信息,並檢查語法錯誤。如果語法錯誤則。提示用戶是否打開修改

[root@localhost ~]#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

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


                         

           


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章