设为首页 收藏本站
查看: 735|回复: 0

linux shell和Expect逝去的事情简介

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2016-4-12 09:25:20 | 显示全部楼层 |阅读模式
1.1  shell一些小总结
1
2
3
4
    1, 零宽断言
    2, 打印菜单
    3, getopts选项简介
    4, 脚本查询ip地理位置



1.1.1  grep  Zero-Width Assertions (零宽断言)

意思就是取出得到你想要的东西,去掉你不关心的东西
1
2
    -o: 表示精确匹配
    -P: 表示使用pcre的正则表达式进行匹配



    1.先行断言: 表示匹配表达式前面的位置

1
2
3
[iyunv@bj-idc-15 ~]# echo "cooking sing" | grep -oP '[a-z]*(?=ing)'   
cook
s



    上面例子:  (?=ing) 这个就是断言,意思当这个断言存在的时候,进行判断匹配,匹配到的
    对象是它前面的字符串.当然前面的字符串你也需要用正则表达式匹配,例如:([a-z])*,由于
    正则是贪婪的,所以断言会一直从右面匹配到不能匹配的时候结束.


    2.后发断言: 表示匹配表达式后面的位置
1
2
[iyunv@bj-idc-15 ~]# echo "abcdefg  abca" | grep -oP '(?<=abc).*'   
defg  abca



    上面的例子: (?<=abc)为断言,跟上面的相反,它是从最左端开始匹配
小小实战

(1)取ip地址
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[iyunv@bj-idc-15 learn]# ifconfig
eth0      Link encap:Ethernet  HWaddr 06:66:91:E7:9A:74  
          inet addr:10.10.11.15  Bcast:10.10.11.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1234086 errors:0 dropped:0 overruns:0 frame:0
          TX packets:829131 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:1182619038 (1.1 GiB)  TX bytes:88948632 (84.8 MiB)
          Interrupt:246
lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:62861848 errors:0 dropped:0 overruns:0 frame:0
          TX packets:62861848 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:10133085846 (9.4 GiB)  TX bytes:10133085846 (9.4 GiB)




取出em1的ip地址
1
2
[iyunv@bds01 ~]# ifconfig | grep -E '(eth0|em1)' -A 1|grep -oP '(?<=addr:)[\d.]+'
10.10.10.180



(2)取日志的时间
假如nginx备份日志的格式文件是access_20150408_nginx.log
1
2
[iyunv@bj-idc-15 ~]# echo "access_20150408_nginx.log" | grep -oP '(?<=access_).*(?=_nginx)'
20150408




1.1.2 打印菜单
echo命令

1
2
-n: 不自动换行
  -e: 打开反斜杠ESC转义。若字符串中出现以下字符,则特别加以处理,而不会将它当成一般文字输出




1
2
3
4
5
6
7
8
9
10
            \a 发出警告声;
           \b 删除前一个字符;
           \c 最后不加上换行符号;
           \f 换行但光标仍旧停留在原来的位置;
           \n 换行且光标移至行首;
           \r 光标移至行首,但不换行;
           \t 插入tab;
           \v 与\f相同;
           \\ 插入\字符;
           \nnn 插入nnn(八进制)所代表的ASCII字符



read命令

1
2
3
4
5
     -p: 交互式显示提示符信息
    -t: 超时时间
    -n: 设定字符个数
    -s: 不会显(输入密码的时候有用)               
     -d: 定界符




实现一个简单菜单
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/bin/bash
MYDATE=`date +%d/%m/%Y`
THIS_HOST=`hostname`
USER=`whoami`
while true;do  
#clear
cat <<EOF
        ____________________________________________________
                User: $USER Host: $THIS_HOST DATE: $MYDATE
        ____________________________________________________
                1: list files
                2: See memory total
                3: See who is on the systeme
                4: Help screen
                Q: Exit Menu
        ____________________________________________________
EOF
echo -e  -n "\t You Choice[1,2,3,4,Q]> "
read CHOICE
echo $CHOICE
case $CHOICE  in
1) ls ;;
2) free -m ;;  
3) who ;;
4) echo "This is help screen , nothing here yet to help you !" ;;
Q|q) exit 0 ;;
*)
echo -e "\t \007 unknown user reponse"
;;
esac
#echo -e -n "\t Hit the return key to continue "
read -p "  Hit the return key to continue " go
done



1.1.3 getopts简介

根据参数进行不同的提示或者不同的执行
先来看一个列子

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
while getopts "h:n:a" arg
do
        case $arg in
                h)
                        echo "Welcome to Beijing arg:$OPTARG";;
                n)
                        echo "Your name is $OPTARG";;
                a)
                        echo "Your age is $OPTARG" ;;
                :)
                        echo "The -h arg must have one args";;
                ?)
                        echo "Please input your select args" ;;
        esac
done



执行结果

1
2
3
4
[iyunv@bj-idc-15 learn]# sh get.sh -h "hello world" -n budongshu -a 18
Welcome to Beijing arg:hello world
Your name is budongshu
Your age is



getopts的脚本使用方法
脚本.sh  option_string  variable

第一个参数使用-h 这样的选项 第二参数是-h 后面跟的hello world 字符串
解析上面的示例
定义选项:
     可以看到while getopts "h:n:a" arg 这句,所以定义的选项"h:n:a"在这里定义.  

冒号含义
    1,字母后面跟上冒号代表这个选项必须有参数也就是-h和-n 后面必须提供一个参数,
        参数的值存储在变量$OPTARG
    2,字母后面没有冒号代表这个选项可以不用跟上参数 ,跟上也不进行赋值. 可以看到-a 没有值

    3, 在字母的最前面加上冒号代表忽略这个命令自己提供的错误信息提示. 下面演示一下

脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
while getopts "h:n:a" arg
do
        case $arg in
                h)
                        echo "Welcome to Beijing arg:$OPTARG";;
                n)
                        echo "Your name is $OPTARG";;
                a)
                        echo "Your age is $OPTARG" ;;

                ?)
                        echo "Please input your select args" ;;
        esac
done



执行结果: 命令内部自己提供的报错中间那个

1
2
3
4
[iyunv@bj-idc-15 learn]# sh get.sh -h hello -n  
Welcome to Beijing arg:hello
get.sh: option requires an argument -- n
Please input your select args



加上冒号以后再看
脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
while getopts ":h:n:a" arg
do
        case $arg in
                h)
                        echo "Welcome to Beijing arg:$OPTARG";;
                n)
                        echo "Your name is $OPTARG";;
                a)
                        echo "Your age is $OPTARG" ;;

                ?)
                        echo "Please input your select args" ;;
        esac
done



执行结果: 可以看到少了一条报错.

1
2
3
[iyunv@bj-idc-15 learn]# sh get.sh -h hello -n  
Welcome to Beijing arg:hello
Please input your select args



报错信息提示

脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
while getopts ":h:n:a" arg
do
        case $arg in
                h)
                        echo "Welcome to Beijing arg:$OPTARG";;
                n)
                        echo "Your name is $OPTARG";;
                a)
                        echo "Your age is $OPTARG" ;;
                :)
                        echo "The -h arg must have one args";;
                ?)
                        echo "Please input your select args" ;;
        esac
done



:  case中冒号,当你忽略系统命令报错的时候(就是在前面加上:号),还有参数-h后面有冒号的,代表必须跟上一个选项参数, 如果没有跟选项参数时候, 它会提示对应你自定义的错误信息,-a不是必须提供参数选项(因为字母后面没有加冒号),所以执行脚本的时候 -a 后面不跟参数(或者不跟-a 选项时候),也不会报错的

?  case中问号,当跟上的参数比如说-e 不存在的时候报错

以上所有的报错$OPTARG变量都是不可用的

1.1.4 脚本查询ip地理位置

脚本
1
2
3
4
5
6
7
8
9
10
#!/bin/bash
Getip=$(curl -s ip.cn?ip=$1)
IParea=$(echo $Getip|awk -F ":" '{print $3}'|awk '{print $1}')
IPisp=$(echo $Getip|awk -F ":" '{print $3}'|awk '{print $2}')
if [ ! $1 ];then
IP=$(echo $Getip|awk -F ":" '{print $2}'|awk '{print $1}')
echo $IP $IParea $IPisp
else
echo $1 $IParea $IPisp
fi



执行结果

1
2
3
4
5
6
[iyunv@bj-idc-15 script]# ./getip.sh (当前的外网ip地址)   
117......... 北京市 ....
[iyunv@bj-idc-15 script]# ./getip.sh 8.8.8.8
8.8.8.8 美国 Google
[iyunv@bj-idc-15 script]# ./getip.sh 114.114.114.114
114.114.114.114 江苏省南京市 信风网络



2.0 Expect 简介
安装

1
yum install expect -y




脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/usr/bin/expect -f
set ip [lindex $argv 0]   
set password "111111"
spawn ssh root@$ip
set timeout 20
expect  {
        "yes/no" { send "yes\r";exp_continue }
        "password:" { send "$password\r" }
}
expect "]#"
send "free -m\r"
interact
#expect eof
#exit



执行结果

1
2
3
4
5
6
7
8
9
10
11
12
13
[iyunv@bj-idc-15 learn]# hostname
bj-idc-15
[iyunv@bj-idc-15 learn]# ./test1.exp 10.10.10.14
spawn ssh root@10.10.10.14
root@10.10.10.14's password:
Last login: Mon Apr 11 16:24:49 2016 from 10.10.11.15
[iyunv@BS01 ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          7870       2782       5088          0        341       2219
-/+ buffers/cache:        220       7650
Swap:         3999         57       3942
[iyunv@BS01 ~]# hostname
BS01



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-202902-1-1.html 上篇帖子: shell脚本带参数实例 下篇帖子: linux shell中的各种奇怪符号 linux
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表