#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/bashfor i in `ls *.$1`doname=${i%.*}mv $i $name.$2done++++++++++++++++++++++++++++变量赋初始值#echo ${TT:-abc}查看TT变量是否有值,有则显示TT的值否则则显示abc#QQ=12#echo ${QQ:-abc}#echo ${TT:=abc}查看TT变量是否有值,有则显示TT的值否则则显示abc,并且给TT赋值abc#!/bin/bashread -p "确定要删除吗y/n?" suresure=${sure:-n}if [ $sure = "y" ];thenrm -rf $1fi++++++++++++++++++++++++++++#!/bin/bashread -p 请输入一个数字 numnum=${num:-1}sum=0for i in `seq $num`do sum=$((sum+i))doneecho "合计为$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/bashi=0while :doread -p "输入IP:" IP[ $IP = "EOF" ]&& breaka[$i]=$IPlet i++doneecho "有${#a[*]}个IP,第一个是${a[0]}"+++++++++++++++++++++++++++++linux交互式命令#yum -y install expect#vim test#!/usr/bin/expectspawn ssh 192.168.4.5expect "passWord:" {send "redhat/r"}expect "#" {send "mkdir /qq7/r"}interact练习: fdisk /dev/vdapw+++++++++++++++++++++++++++++++++将expect内置到shell脚本中:#vim test.sh#!/bin/bashexpect << EOFspawn ssh 192.168.4.5expect { "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.txtwelcome vs welcome us welcome as welcome /(welcome/) vs/1 us /1 as/1alias grep="grep --color"#grep --color "/(welcome/)vs/1us/1as/1" a.txt2.扩展正则[优化,加强]{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 5400C5A026C5192.168.4.5 5450B5C026C5192.168.4.5 54c0C59026C5192.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-C5192.168.4.5 54-50-B5-C0-26-C5192.168.4.5 54-c0-C5-90-26-C5192.168.4.5 54-00-C5-02-62-C5#######################################################################新闻热点
疑难解答