shell-04

2/13/2017来源:经验技巧人气:1703

字串的截取 #A=123456

#echo ${A:2:2}   #第一位从0开始

34

#exPR substr $A 1 3   #第一位从1开始

123

#echo $A |cut -b 2-4       #第一位从1开始 #echo $A |cut -b 2,5,6

#dirname "/a/b/c/d.txt"

/a/b/c/

#basename "/a/b/c/d.txt" d.txt #echo ${变量/旧/新}     最短匹配 #echo ${变量//旧/新}    最长匹配 ******************************************************* #A=root:x:0:0:root:/root:/bin/bash #echo ${A#*:} 掐头最短匹配 #echo ${A##*:} 掐头最长匹配 #echo ${A#*r} #echo ${A##*r} #echo ${A%:*} 去尾最短匹配 #echo ${A%%:*} 去尾最长匹配 ++++++++++++++++++++++++++++++ 批量修改扩展名的脚本 #touch {a,b,c,d,e}.doc  #!/bin/bash for i in `ls *.$1` do name=${i%.*} mv $i $name.$2 done ++++++++++++++++++++++++++++ 变量赋初始值 #echo ${TT:-abc} 查看TT变量是否有值,有则显示TT的值 否则则显示abc #QQ=12 #echo ${QQ:-abc} #echo ${TT:=abc} 查看TT变量是否有值,有则显示TT的值 否则则显示abc,并且给TT赋值abc #!/bin/bash read -p "确定要删除吗y/n?" sure sure=${sure:-n} if [ $sure = "y" ];then rm -rf $1 fi ++++++++++++++++++++++++++++ #!/bin/bash read -p 请输入一个数字 num num=${num:-1} sum=0 for i in `seq $num` do      sum=$((sum+i)) done echo "合计为$sum" ++++++++++++++++++++++++++++++ 1.数组[变量] #A=12345 #echo ${#A}

#a=(1 2 3 4) 定义数组

#a[0]=1 定义数组 #a[1]=ab

#a[2]=t12

#echo ${a[0]} 返回数组的一个值 #echo ${a[1]} #echo ${a[2]} #echo ${a[*]} 返回数组所有的值 #echo ${#a[*]}  返回数组有多少个值 使用数组保存ip信息 +++++++++++++++++++++++++ #!/bin/bash i=0 while : do read -p "输入IP:"  IP [ $IP = "EOF" ]&& break a[$i]=$IP let i++ done echo "有${#a[*]}个IP,第一个是${a[0]}"

+++++++++++++++++++++++++++++ linux交互式命令 #yum -y install expect #vim test #!/usr/bin/expect spawn ssh 192.168.4.5 expect "passWord:" {send "redhat\r"} expect "#" {send "mkdir /qq7\r"} interact 练习: fdisk /dev/vda p w +++++++++++++++++++++++++++++++++ 将expect内置到shell脚本中: #vim test.sh #!/bin/bash expect << EOF spawn ssh 192.168.4.5 expect {  "yes/no" { send "yes\r" ; exp_continue }    "password:" { send "123\r" } EOF ++++++++++++++++++++++++++++++++++++   1 #!/bin/bash   2 for i in 5 205   3 do   4 expect <<EOF   5 spawn ssh 192.168.4.$i   6 expect "yes" {send "yes\r"}   7 expect "password:" {send "123456\r"}   8 expect "#" {send "touch /data/ttt.txt\r"}   9 expect "#" {send "exit\r"}  10 expect "#" {send "\r"}  11 EOF  12 done ++++++++++++++++++++++++++++++++++++ 正则表达式 1.基本正则 dog  //dog [a-z]    //小写字母[集合] ^   //开始

$                     //结尾

 ^$            // 表示为空

.//任意单个字符

*//前一个字符出现了任意次

 .*             //表示任意字符

\{n,m\}      //前一个字符出现了n到m次 \{n,\}        //前一个字符出现了n次以上 \{n\}                  //前一个字符出现了n次 [^ab]//取反,不要a或b \(\)保留 #cat a.txt welcome vs welcome us welcome as welcome       \(welcome\) vs\1 us \1 as\1 alias grep="grep --color" #grep --color "\(welcome\)vs\1us\1as\1" a.txt 2.扩展正则[优化,加强] {n,m} 、{n,} 、 {n}     //匹配前面的符号出现了n到m次、n次以上、n次 ?//匹配前面的符号出现了0或1次 +//匹配前面的符号出现了1次以上 ()//保留 (ab)   //合并为整体 (ab|ba)//或者  |              //或者  \b            //单词边界 \broot\b ---> root  \<            //单词的开头 \<th  以th开头的单词  \>            //单词街上  \<root\> 等于 \broot\b  基本正则兼容性更强[有些软件不支持扩展正则],扩展正则功能更强 ##################################### 16进制 XX:XX:XX:XX:XX:XX [0-9a-f]:[0-9a-f]:[0-9a-f]:[0-9a-f]:[0-9a-f]:[0-9a-f]

查找网卡的MAC地址:

#ifconfig eth0  |grep --color -iE "([0-9a-f][0-9a-f]:){5}[0-9a-f][0-9a-f]"

#ifconfig eth0  |grep --color -iE "([0-9a-f]{2}:){5}[0-9a-f]{2}]"

#ifconfig  |awk '{print $5}' |grep --color -iE "([0-9a-f][0-9a-f]:){5}[0-9a-f][0-9a-f]" # ifconfig |awk '{print $5}' |egrep --color -i "([0-9a-f][0-9a-f]:){5}[0-9a-f][0-9a-f]" ##################################################################################################################### 例:MAC地址表示 192.168.4.5      5400C5A026C5 192.168.4.5      5450B5C026C5 192.168.4.5      54c0C59026C5 192.168.4.5      5400C50262C5 末行模式:%s/旧/新/ :%s /\([0-9a-fA-F][0-9a-fA-F]\)\([0-9a-fA-F][0-9a-fA-F]\)\([0-9a-fA-F][0-9a-fA-F] \)\([0-9a-fA-F][0-9a-fA-F]\)\([0-9a-fA-F][0-9a-fA-F]\)\([0-9a-fA-F][0-9a-fA-F]\)$/\1-\2-\3-\4-\5-\6/ 192.168.4.5      54-00-C5-A0-26-C5 192.168.4.5      54-50-B5-C0-26-C5 192.168.4.5      54-c0-C5-90-26-C5 192.168.4.5      54-00-C5-02-62-C5 #######################################################################