您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
马哥linux运维学习笔记-bash脚本编程之在bash脚本中使用选项
发布时间:2019-03-11 17:17:36编辑:雪饮阅读()
cat的使用方法-创建文件
cat不仅仅用来查看文件,也可以用来创建文件
[root@mail ~]# cat > 1.txt << EOF
> 看例子是最快的熟悉方法
> EOF
[root@mail ~]# cat 1.txt
看例子是最快的熟悉方法
date的宏-T
直接显示时间 (24 小时制)
[root@mail ~]# date "+%T"
20:34:27
vim的"+"
vim的"+"若不指定行号,则直接打开文件到文件末尾,否则打开文件到指定行
[root@mail ~]# vim +8 input.sh
[root@mail ~]# vim + input.sh
getopts的使用
[root@mail ~]# cat opt.sh
#!/bin/bash
getopts "bd" OPT
echo $OPT
查看不同选项、参数下的执行结果
[root@mail ~]# sh opt.sh
?
[root@mail ~]# sh opt.sh -b
b
[root@mail ~]# sh opt.sh -d
d
[root@mail ~]# sh opt.sh -b -d
b
[root@mail ~]# sh opt.sh -c
opt.sh: illegal option -- c
?
[root@mail ~]# sh opt.sh -b aaa
b
[root@mail ~]# sh opt.sh -d aaa
d
总结:getopts可以在脚本内定义可以接收的短选项,且一次只能接收一个选项
OPTARG与参数的使用
[root@mail ~]# cat opt.sh
#!/bin/bash
getopts "bd:" OPT
echo $OPT":"$OPTARG
查看不同选项、参数的执行结果
[root@mail ~]# sh opt.sh
?:
[root@mail ~]# sh opt.sh -b
b:
[root@mail ~]# sh opt.sh -b aaa
b:
[root@mail ~]# sh opt.sh -d
opt.sh: option requires an argument -- d
?:
[root@mail ~]# sh opt.sh -d aaa
d:aaa
总结:当getopts定义选项时,若某个选项后面跟随冒号,则表示该选项在使用时候必须给予参数,变量OPTARG的值等于当前getopts所调用的选项的值。
屏蔽选项、参数不合法时的默认报错并自定义错误信息及选项的遍历抽取
[root@mail ~]# cat opt.sh
#!/bin/bash
while getopts ":b:d:" SWITCH;do
case $SWITCH in
b) echo "the option is b." ;;
d) echo "the option is d." ;;
\?) echo "Wrong option." ;;
esac
done
查看不同选项、参数的执行结果
[root@mail ~]# sh opt.sh -b
[root@mail ~]# sh opt.sh -b aa
the option is b.
[root@mail ~]# sh opt.sh -d aa
the option is d.
[root@mail ~]# sh opt.sh -d
[root@mail ~]# sh opt.sh -c
Wrong option.
[root@mail ~]# sh opt.sh -c aa
Wrong option.
总结:当getopts定义可接收选项时只要在最前面可接收选项之前加入冒号,则可避免选项、参数不合法时抛出默认的错误信息
optind
[root@mail ~]# cat optindex.sh
#!/bin/bash
while getopts ":b:d:" SWITCH;do
case $SWITCH in
b) echo "the option is b."
echo $OPTARG'>'$OPTIND ;;
d) echo "the option is d."
echo $OPTARG'>'$OPTIND ;;
\?) echo "Wrong option." ;;
esac
done
查看执行结果
[root@mail ~]# sh optindex.sh -b aa
the option is b.
aa>3
[root@mail ~]# sh optindex.sh -b aa -d cc
the option is b.
aa>3
the option is d.
cc>5
总结:
optind是整个脚本接收的数据遍历中当前数据的下一个选项或参数的索引
一个可以创建脚本的脚本
[root@mail ~]# cat mkscript.sh
#!/bin/bash
#name:mkscript
#Description:Create script
#Author:Magedu
#Version:0.0.1
#Datatime:03/02/12 14:42:00
#Usage:mkscript FILENAME
cat > $1 << EOF
#!/bin/bash
#Name:`basename $1`
#Description:
#Author:Magedu
#Version:0.0.1
#Datatime:`date "+%F %T"`
#Usage:`basename $1`
EOF
vim +8 $1
一个可以创建脚本的脚本(当待创建脚本文件不存在时才创建,否则仅打开)
[root@mail ~]# cat mkscript.sh
#!/bin/bash
#name:mkscript
#Description:Create script
#Author:Magedu
#Version:0.0.1
#Datatime:03/02/12 14:42:00
#Usage:mkscript FILENAME
if ! grep "[^[:space:]]" $1 &> /dev/null;then
cat > $1 << EOF
#!/bin/bash
#Name:`basename $1`
#Description:
#Author:Magedu
#Version:0.0.1
#Datatime:`date "+%F %T"`
#Usage:`basename $1`
EOF
fi
vim + $1
一个可以创建脚本的脚本(新增语法错误检查)
当新建的脚本中有语法错误时若用户退出则提示用户是否真的要退出
[root@mail ~]# cat mkscript.sh
#!/bin/bash
#name:mkscript
#Description:Create script
#Author:Magedu
#Version:0.0.1
#Datatime:03/02/12 14:42:00
#Usage:mkscript FILENAME
if ! grep "[^[:space:]]" $1 &> /dev/null;then
cat > $1 << EOF
#!/bin/bash
#Name:`basename $1`
#Description:
#Author:Magedu
#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 8;;
*)
vim + $1
;;
esac
done
chmod +x $1
这里需要注意:由于用父脚本创建的子脚本,所以子脚本退出时相当于又回到了父脚本的代码环境中了,所以该脚本可以捕获到子脚本退出。
一个可以创建脚本的脚本(通过选项补全description)
[root@mail ~]# cat mkscript.sh
#!/bin/bash
#name:mkscript
#Description:Create script
#Author:Magedu
#Version:0.0.1
#Datatime:03/02/12 14:42:00
#Usage:mkscript FILENAME
while getopts ":d:" SWITCH; do
case $SWITCH in
d)
echo $OPTARG
DESC=$OPTARG ;;
\?)
echo "Usage:mkscript [-d DESCRIPTION] FILENAME" ;;
esac
done
shift $[$OPTIND-1]
if ! grep "[^[:space:]]" $1 &> /dev/null;then
cat > $1 << EOF
#!/bin/bash
#Name:`basename $1`
#Description:$DESC
#Author:Magedu
#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 8;;
*)
vim + $1
;;
esac
done
chmod +x $1
注意:
(1)使用格式如:
[root@mail ~]# ./mkscript.sh -d "nideongde" test10.sh
选项、参数调用顺序不对是不行的
(2)这里由于while取-d参数时是通过遍历抽取的,索引最终就会到末尾,那么最下面原本$1应该[root@mail ~]# ./mkscript.sh test10.sh中的test10.sh,而若不将索引重新移动到最开始处则$1就是'-d'那么按照上面脚本的运行流程,接下来就会运行命令'vim -d'这样就不符合逻辑了。所以才用shift将索引向左移动.
关键字词:linux,bash,选项,参数,脚本