1、字符串长度
expr命令引出通用求值表达式,可以实现算术操作、比较操作、字符串操作和逻辑操作等功能
# 说明:expr length ${string} 计算字符串的长度,若string中包括空格,则expr计算命令中需用双引号引起来,expr length后面只能跟一个参数,string有空格会当作多个参数处理。用单引号的话,统计的是"字符去重以及去掉空格"之后的长度
xxx@yyy:~$ str="the world perfact"
xxx@yyy:~$ expr length ${str}
expr: syntax error: unexpected argument ‘world’
xxx@yyy:~$ expr length "${str}"
17
xxx@yyy:~$ expr length '${str}'
6
xxx@yyy:~$ str="theworldperfact"
xxx@yyy:~$ expr length ${str}
15
xxx@yyy:~$ expr length '${str}'
6
# 说明:${#string} 方法对于有无空格的字符串均可使用
xxx@yyy:~$ str="theworldperfact"
xxx@yyy:~$echo ${#str}
15
xxx@yyy:~$ str="the world perfact"
xxx@yyy:~$ echo ${#str}
17
xxx@yyy:~$ echo "${#str}"
17
xxx@yyy:~$ echo '${#str}'
${#str}
2、字符串匹配(长度)
匹配字符串开头子串的长度,返回匹配到的substring字符串的长度,若string开头匹配不到则返回0,其中substring可以是字符串也可以是正则表达式。操作表达式为: expr match ${string} s u b s t r i n g 和 e x p r " substring 和 expr " substring和expr"string" : ‘$substring’
xxx@yyy:~$ str="world is good"
xxx@yyy:~$ expr length "${str}"
13
xxx@yyy:~$ expr match "${str}" "w.*"
13
xxx@yyy:~$ expr match "${str}" "world"
5
# good 尽管在string字符串中出现,但是未出现在string的开头处,因此返回0! expr match 匹配的是开头子串的长度。
xxx@yyy:~$ expr match "${str}" "good"
0
# 后面的$substring可以是正则, $substring两边是单引号或双引号无所谓。
xxx@yyy:~$ expr match "${str}" "[a-z]*"
5
xxx@yyy:~$ expr match "${str}" "[a-w]*"
5
xxx@yyy:~$ expr match "${str}" "[a-u]*"
0
xxx@yyy:~$ expr match "${str}" '[a-z]*'
5
xxx@yyy:~$ expr "${str}" : '[a-z]*'
5
xxx@yyy:~$ expr "${str}" : "[a-z]*[a-z]*"
5
3、字符串匹配(索引)
在字符串string上匹配substring中字符最早第一次出现的位置(从左到右,从1开始)匹配不到expr index返回0。简单来说,就是找出子串在字符串中最早第一次出现的单个字符的位置。操作表达式:expr index ${string} $substring
xxx@yyy:~$ str="world is good"
xxx@yyy:~$ echo ${str}
world is good
xxx@yyy:~$ expr index ${str} "is"
expr: syntax error: unexpected argument ‘good’ #注意string字符串中有空格,需要加双引号
xxx@yyy:~$ expr index "${str}" "is"
7
xxx@yyy:~$ expr index "${str}" "wor"
1
xxx@yyy:~$ expr index "${str}" "cat"
0
4、字符串抽取
# 从左到右抽取字符串(左边默认从0开始标号)
# 格式一:{string:position} 从名称为${string}的字符串的第$position个位置开始抽取子串,从0开始标号。
xxx@yyy:~$ echo ${str}
world is good
xxx@yyy:~$ echo ${str:0}
world is good
xxx@yyy:~$ echo ${str:2}
rld is good
# 格式二:{string:position:length} 增加$length变量,表示从${string}字符串的第$position个位置开始抽取长度为$length的子串。
xxx@yyy:~$ echo ${str:0:3}
wor
xxx@yyy:~$ echo ${str:2:0}
xxx@yyy:~$
# 从右到左抽取字符串(右边默认从-1开始标号)
# 格式一:{string: -position}。需要谨记:冒号与横杠间有一个空格
xxx@yyy:~$ echo ${str}
world is good
xxx@yyy:~$ echo ${str: -1}
d
xxx@yyy:~$ echo ${str: -2}
od
xxx@yyy:~$ echo ${str:-2} # #冒号与"-"之间必要要有空格,否则截取无效
world is good
# 格式二:{string:(position)}。如果加了括号,则冒号后面加不加空格是一样的效果
xxx@yyy:~$ echo ${str:(-1)}
d
xxx@yyy:~$ echo ${str:(-2)}
od
# 如果要想实现从右边第几个字符开始截取,截取长度为多少,则方法为: {string:m-n:x} 表示从右边第"m-n"个字符开始截取,截取长度为x
xxx@yyy:~$ echo ${str}
world is good
xxx@yyy:~$ echo ${str:1-7:4}
s go
xxx@yyy:~$ echo ${str:3-9:4}
s go
# expr substr也能够实现抽取子串功能,命令格式:expr substr ${string} $position $length 。expr substr命令从1开始进行标号
xxx@yyy:~$ echo ${str}
world is good
xxx@yyy:~$ echo ${str:3:5}
ld is
xxx@yyy:~$ expr substr ${str} 3 5 # 加双引号
expr: syntax error: unexpected argument ‘3’
xxx@yyy:~$ expr substr "${str}" 3 5 # 从0开始进行标号
rld i
xxx@yyy:~$ expr substr "${str}" 4 5 # 从1开始标号
ld is
# 使用正则表达式抽取子串的命令,但只能抽取string开头处或结尾处的子串。
# 抽取字符串开头处的子串:
# 格式一:expr match $string ''
# 格式二:expr $string : '' 注意:其中冒号前后都有一个空格
xxx@yyy:~$ str="20160606hello World good"
xxx@yyy:~$ echo "${str}"
20160606hello World good
xxx@yyy:~$ expr match "${str}" '\([0-9]*\)' # 有空格$str使用双引号引起来,没空格可以不用使用双引号
20160606
xxx@yyy:~$ expr "${str}" : '\([0-9]*\)'
20160606
xxx@yyy:~$ expr "${str}" :'\([0-9]*\)' # 注意:其中冒号前后都有一个空格
expr: syntax error: unexpected argument ‘:\\([0-9]*\\)’
xxx@yyy:~$ expr "${str}": '\([0-9]*\)' # 注意:其中冒号前后都有一个空格
expr: syntax error: unexpected argument ‘\\([0-9]*\\)’
# 抽取字符串结尾处的子串:
# 格式一:expr match $string '.*'
# 格式二:expr $string : '.*'。 注意:.*表示任意字符的任意重复。一个.表示一个字符。
xxx@yyy:~$ string="20181112hello WORld Good"
xxx@yyy:~$ echo ${string}
20181112hello WORld Good
xxx@yyy:~$ expr match "$string" '.*\(.\)'
d
xxx@yyy:~$ expr match "$string" '.*\(..\)'
od
xxx@yyy:~$ expr match "$string" '.*\(...\)'
ood
xxx@yyy:~$ expr match "$string" '.*\(.....\)'
Good
xxx@yyy:~$ expr "$string" : '.*\(.....\)'
Good
xxx@yyy:~$ expr "$string" : '.*\(.........\)'
ORld Good
xxx@yyy:~$ string="heLLO2018 world"
xxx@yyy:~$ expr match "$string" '\([a-z]*\)'
he
xxx@yyy:~$ expr match "$string" '\([a-Z]*\)'
heLLO
xxx@yyy:~$ expr match "$string" '\([a-Z]*[0-9]*\)'
heLLO2018
xxx@yyy:~$ expr match "$string" '\(.[a-Z]*[0-9]*\)'
heLLO2018
xxx@yyy:~$ expr "$string" : '.*\(.........\)'
018 world
5、字符串删除
# 从string开头处删除子串:
# 格式一:${string#substring} 删除开头处与substring匹配的最短子串。
# 格式二:${string##substring} 删除开头处与substring匹配的最长子串。其中substring并非是正则表达式而是通配符。
xxx@yyy:~$ string="china IS niuBIlity2018"
xxx@yyy:~$ echo ${string#c*i} #删除c开头到i的最短子串
na IS niuBIlity2018
xxx@yyy:~$ echo ${string##c*i} #删除c开头到i的最长子串
ty2018
xxx@yyy:~$ echo ${string#ch*n} #删除ch开头到n的最短子串
a IS niuBIlity2018
xxx@yyy:~$ echo ${string##ch*n} #删除ch开头到n的最长子串
iuBIlity2018
# 上面#或##后面的字符必须是${string}字符串的开头子串!否则删除子串就无效了!
xxx@yyy:~$ echo ${string#i*n} #i不是开头字符,所以删除无效
china IS niuBIlity2018
xxx@yyy:~$echo ${string##i*n} #i不是开头字符,所以删除无效
china IS niuBIlity2018
# ${string#*substring} 删除${string}字符串中第一个$substring及其之前的字符
# ${string##*substring} 删除${string}字符串中最后一个$substring及其之前的字符
xxx@yyy:~$ string="china IS niuBIlity2018"
xxx@yyy:~$ echo ${string#*i} #删除第一个i及其之前的字符
na IS niuBIlity2018
xxx@yyy:~$ echo ${string##*i} #删除最后一个i及其之前的字符
ty2018
# 格式一:${string%substring*} 删除${string}字符串中最后一个$substring及其之后的字符
# 格式二:${string%%substring*} 删除${string}字符串中第一个$substring及其之后的字符
xxx@yyy:~$ echo ${string}
china IS niuBIlity2018
xxx@yyy:~$ echo ${string%i*}
china IS niuBIl
xxx@yyy:~$ echo ${string%%i*}
ch
xxx@yyy:~$ echo ${string%c*}
xxx@yyy:~$ echo ${string%%c*}
xxx@yyy:~$ str=bo/www/kevin/data/test/ccd.log
xxx@yyy:~$ echo ${str#*/*/}
kevin/data/test/ccd.log
xxx@yyy:~$ echo ${str##*/*/}
ccd.log
xxx@yyy:~$ echo ${str%/*/*}
bo/www/kevin/data
xxx@yyy:~$ echo ${str%%/*/*}
bo
6、字符串替换
换子串命令可以在任意处、开头处、结尾处替换满足条件的子串
# 格式一:${string/substring/replacement},仅替换第一次与substring相匹配的子串。
# 格式二:${string//substring/replacement},替换所有与substring相匹配的子串。
xxx@yyy:~$ string="china IS niuBIlity2018"
xxx@yyy:~$ echo ${string/i/#}
ch#na IS niuBIlity2018
xxx@yyy:~$ echo ${string//i/#}
ch#na IS n#uBIl#ty2018
xxx@yyy:~$ echo ${string}
china IS niuBIlity2018
xxx@yyy:~$ echo ${string/ /--} #替换空格
china--IS niuBIlity2018
xxx@yyy:~$ echo ${string// /--}
china--IS--niuBIlity2018
# 在开头处替换与substring相匹配的子串,格式为:${string/#substring/replacement}。
# 在结尾除替换与substring相匹配的子串,格式为:${string/%substring/replacement}。
xxx@yyy:~$ echo ${string}
china IS niuBIlity2018
xxx@yyy:~$ echo ${string/#ch/he}
heina IS niuBIlity2018
xxx@yyy:~$ echo ${string/#china/anhui}
anhui IS niuBIlity2018
xxx@yyy:~$ echo ${string/#niu/he} #注意这里#后面的字符必须是${string}字符串中开头的字符
china IS niuBIlity2018
xxx@yyy:~$ echo ${string/%2018/2020}
china IS niuBIlity2020
xxx@yyy:~$ echo ${string/%lity2018/hehehe}
china IS niuBIhehehe
7、${!varprefix*} 和 ${!varprefix@}
变量通配符的使用
xxx@yyy:~$ test="bobo"
xxx@yyy:~$ test1="bobo1"
xxx@yyy:~$ test2="bobo2"
xxx@yyy:~$ test4="bobo4"
xxx@yyy:~$ echo ${!test*}
test test1 test2 test4
xxx@yyy:~$ echo ${!test@}
test test1 test2 test4
8、判断一个字符串是否由字母数字开头&grep
#!/bin/bash
#str="This IS a root USER, 20171aaabb"
read -p "请输入内容:" str # -p 表示指定要显示的提示如果添加-s参数,即"read -sp",表示静默输入隐藏输入的数据一般用于密码输入
if echo "${str}" | grep -q '^[A-Za-z0-9].*\+$'; then
echo -e "${str}\nok" # echo后添加-e参数为了让打印中的\n换行符生效不加-e参数,则\n就被做当普通字符打印出来了
else
echo "invaliad"
fi
# 为了简化,还可以将上面脚本中的: grep -q '^[A-Za-z0-9].*\+$' 改为 grep -q '^[A-Za-z0-9].*'
# 判断字符串是否由大小写字母或数字结尾 grep -q '^[A-Za-z0-9].*\+$' 改为 grep -q '.*[A-Za-z0-9]$'
'^[A-Za-z0-9].*\+$' # 表示以大写字母或小写字母或数字为开头。没有顺序要求
'^[A-Za-z0-9].*' # 可以直接去掉后面的"\+$"部分
'^[A-Z].*' # 表示以大写字母开头
'^[a-z].*' # 表示以小写字母开头
'^[0-9].*' # 表示以数字字母开头
".*[0-9]$"- # 表示以数字结尾
".*[a-z]$" # 表示以小写字母结尾
".*[a-Z]$" # 表示以大小写字母结尾
".*[A-Z]$" # 表示以大写字母结尾
xxx@yyy:~$ cat a.txt
Good study 2018! hahahah~
good Study 2018hehehehe
2018 Good study 1wqe
2018stuDY is heht6ttt
!@#asdf
TOOk213gg asdfasdf
anhui 2018asdfjlsadfdsaff
#$$$$$
xxx@yyy:~$ cat a.txt|grep '^[A-Z].*\+$'
Good study 2018! hahahah~
TOOk213gg asdfasdf
xxx@yyy:~$ cat a.txt|grep '^[a-z].*\+$'
good Study 2018hehehehe
anhui 2018asdfjlsadfdsaff
xxx@yyy:~$ cat a.txt|grep '^[0-9].*\+$'
2018 Good study 1wqe
2018stuDY is heht6ttt
xxx@yyy:~$ cat a.txt|grep '^[A-Za-z0-9].*\+$'
Good study 2018! hahahah~
good Study 2018hehehehe
2018 Good study 1wqe
2018stuDY is heht6ttt
TOOk213gg asdfasdf
anhui 2018asdfjlsadfdsaff
xxx@yyy:~$ cat a.txt|grep -v '^[A-Za-z0-9].*\+$'
!@#asdf
#$$$$$
xxx@yyy:~$ cat a.txt|grep -v '^[A-Z].*\+$'|grep -v '^[a-z].*\+$'|grep -v '^[0-9].*\+$'
!@#asdf
#$$$$$
xxx@yyy:~$ cat a.txt |grep -v ^[a-Z]
2018 Good study 1wqe
2018stuDY is heht6ttt
!@#asdf
#$$$$$
xxx@yyy:~$ cat a.txt |grep -v ^[a-Z]|grep -v [0-9]
!@#asdf
#$$$$$
# grep获取多个条件(grep -E "条件1|条件2|条件3")
xxx@yyy:~$ cat a.txt |grep -E "^[a-z]|^[0-9]"
good Study 2018hehehehe
2018 Good study 1wqe
2018stuDY is heht6ttt
anhui 2018asdfjlsadfdsaff
# grep过滤多个条件(grep -v "条件1\|条件2\|条件3"),注意""里面有转义符"\"
xxx@yyy:~$ cat a.txt |grep -v "^[a-z]\|^[0-9]"
Good study 2018! hahahah~
!@#asdf
TOOk213gg asdfasdf
#$$$$$
xxx@yyy:~$ cat a.txt |grep -v "[a-z]\|^[0-9]"
#$$$$$
9、tr 和 sed 的简单使用
# 删除制定字符串
echo "2018-10-08 15:19:05"|sed 's/-//g'|sed 's#:##g'
20181008 151905
str="root!@#QWE123"
echo ${str}| sed 's/\!//g' | sed 's/\@//g' | sed 's/\#//g'
ootQWE123
sed -n '/[0-9]/p' filename 将文件中匹配数字的行打印出来
sed -n '/[a-z]/d' filename 将文件中匹配小写字母的行删除
sed -i '/[A-Z]/s/root/huoqiu/g' filename 将文件中匹配大写字母的行里的root替换为huoqiu
# tr使用-d参数可以起到删除字符的效果
str="root!@#QWE123"
echo ${str}| tr -d "a-z"| tr -d "A-Z"| tr -d "0-9"
!@#
# 将输入字符由大写转换为小写
echo "ANhui@ROOT"|tr "A-Z" "a-z"
anhui@root
# 巧妙使用tr命令进行数字相加操作
echo 1 2 3 4 5 6 7 8 9 | xargs -n1 | echo $[ $(tr '\n' '+') 0 ]
45
# xargs -n 用法
echo "aa bb cc 1 2 3"|xargs -n1
aa
bb
cc
1
2
3
echo "aa bb cc 1 2 3"|xargs -n2
aa bb
cc 1
2 3
# 删除文件file中出现的换行'\n'、制表'\t'字符
cat filename | tr -d "\n\r\t"
# 删除空行
cat filename | tr -s "\n" > new_file
# 删除Windows文件"造成"的'^M'字符
cat filename | tr -s "\r" "\n" > new_file
10、read 的简单使用
#read后面的变量只有name一个,也可以有多个,多个变量使用空格隔开,这时如果输入多个数据,则第一个数据给第一个变量,第二个数据给第二个变量;如果输入数据个数过多,则最后所有的值都给最后那个变量!!如果输入数据太少,则对应不到数据的变量为空!在read命令行中也可以不指定变量,如果不指定变量,那么read命令会将接收到的数据放置在环境变量REPLY中
# read命令提供了-p参数,允许在read命令行中直接指定一个提示
read -p "Enter your name:" name
echo "hello $name, welcome to my program"
read -p "Enter your name:" name age city
echo "hello ${name},${age},${city}, welcome to my program"
read -p "Enter your name:"
echo "${REPLY}是输入的内容"
# read -t -t选项指定read命令等待输入的秒数。当计时满时,read命令返回一个非零退出状态
read -t 5 -p "please enter your name:" name
# read -n 当输入的字符数目达到预定数目时,自动退出,并将输入的数据赋值给变量
read -n1 -p "Do you want to continue [Y/N]?" answer
case $answer in
Y | y)
echo -e "\nfine ,continue";;
N | n)
echo -e "\nok,good bye";;
#echo需要加上参数"-e"才能将后面引号内的特殊字符"\n"生效!否则就当普通字符处理了
*)
echo -e "\nerror choice";;
esac
exit 0
read -n5 -p "please input name:" name
len=$(expr length "${name}")
#或者使用len=`echo ${#name}`
if [ ${len} -lt 5 ];then
echo "输入的名字为:${name}"
else
echo -e "\n输入的名字长度超过5,当前名字为${name}"
exit 0
fi
# read -s 用户输入,但不希望输入的数据显示在监视器上典型的例子就是输入密码
read -s -p "Enter your password: " pass
echo -e "\nyour password is $pass"
# read读取文件!!!!(cat filename | while read line)每次调用read命令都会读取文件中的"一行"文本!!当文件没有可读的行时,read命令将以非零状态退出。
cat test.txt
wangbo is a boy!
beijing is good!
abc 123 sahdfksfah
asf#$$!QA
# 处理文件的正确方式
#!/bin/bash
file=$(cat /root/test)
while read line
do
#输出每一行
echo line=${line}
#截取一行的第一列
column1=`echo ${line}| cut -d "," -f1`
echo ${column1}
#输出前8个字符
echo ${column1:0:8}
#截取一行的第二列
column2=`echo ${line}| cut -d "," -f2`
echo ${column2}
#输出前8个字符
echo ${column2:0:8}
done <<EOF
${file}
EOF
cat test.txt | while read line
#cat命令的输出作为read命令的输入,read读到的值放在line中。line为读取文件行内容的变量
do
echo ${line}|grep -w "beijing" >/dev/null 2>&1
if [ $? -eq 0 ];then
echo "想要的是:${line}"
else
echo "Line ${count}:${line}" #读取的内容默认为变量${line}
count=$[ ${count} + 1 ] #注意中括号中的空格。
fi
done
echo "finish"
Line 1:wangbo is a boy!
想要的是:beijing is good!
Line 2:abc 123 sahdfksfah
Line 3:asf#$$!QA
finish
11、shell 中 case 的简单使用
#!/bin/bash
while true
do
echo -e "
\033[31m A 显示主机ip \033[0m
\033[32m B 显示磁盘剩余空间 \033[0m
\033[33m C 显示系统运行时间 \033[0m
\033[34m Q 退出系统 \033[0m
"
read -p "请输入你的选择:" char
case ${char} in
a|A)
echo -e "\033[31m `ifconfig eth0 | grep "netmask" | awk '{print $2}'` \033[0m"
;;
b|B)
echo -e "\033[32m `df -h | awk 'NR==2{print "剩余空间大小为:"$4}'` \033[0m"
;;
c|C)
echo -e "\033[33m `uptime | awk '{print "系统已经运行了"$3""$4""}'` \033[0m"
;;
q|Q)
exit 0
;;
*)
echo "请输入A/B/C/Q"
;;
esac
done
#!/bin/bash
cat <<eof
*****************
**1. backup
**2. copy
**3. quit
*****************
eof
read -p "input your choose: " OP
case $OP in
1|backup)
echo "Backup..."
;;
2|copy)
echo "Copy..."
;;
3|quit)
exit
;;
*)
11、shell 中 while 的简单使用
#!/bin/bash
for NUM in $(seq 1 254) #或者直接"seq 254" 或者 "seq 10 30"用于一段ip
do
IP=172.16.60.${NUM}
# -c表示ping的次数,-i表示时间间隔(秒),
ping -c 3 -i 0.2 $IP &> /dev/null
if [ $? -eq 0 ];then
echo "$IP is up"
else
echo "$IP id down"
fi
done
#!/bin/bash
while :
do
read -p "Please input a number: " n
if [ -z "$n" ];then #空串为真
echo "you need input sth."
continue
fi
n1=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n1" ];then #非空串为真
echo "you just only input numbers."
continue
fi
break
done
echo $n
12、shell 中function 的简单使用
#调用函数时可以传递参数,函数内部中使用$1、$2......来引用传递的参数。格式如下:
函数名称 参数1 参数2 ......
$1 #调用第一个参数
$2 #调用第二个参数
...
$n #调用第n个参数
$# #返回参数个数n
$0 #当前脚本文件名
1、可以用$?变量来显示上一条命令/函数执行结束的退出状态码。当然,shell也为我们提供了return,像其他语言函数中return 一样,不过(整形)返回值必须在0~255之间.
2、在Shell中,也可以定义"库文件",然后再另一个文件中导入,也是脚本文件.sh。使用库函数的关键在于导入库文件。用source来导入.
3、使用函数的库文件时,如果每次都需要自己去导入定义的库文件会显得很麻烦!方法就是在Shell的配置文件的.bashrc中声明该函数,因为每次启动shell都会载入.bashrc文件,所以就实现了"自动导入库文件"
4、return与exit的区别:return和exit都可以返回退出码,但是不同的是,return是退出函数,而exit是退出整个脚本。
# 此时的函数就相当于一个命令,需要使用$()或``调用。
#!/bin/bash
function kevin(){
read -p "请输入内容: " str
echo ${str}
}
bo=$(kevin) #也可以是bo=`kevin`
echo "测试结果为${bo}"
# 函数外的参数,函数可以直接调用;函数内的参数,只要运行过函数,外部也可以直接调用。
#!/bin/bash
str="hello world"
function kevin(){
bo="${str} is very nice!!"
}
kevin
echo "${bo}, 666~"
hello world is very nice!!, 666~
13、删除空格的命令
删除文件中行首空格的命令
# sed 's/^[ \t]*//g' filename #不加-i参数,表示仅仅在当前终端显示命令执行效果。
# sed -i 's/^[ \t]*//g' filename #加-i参数,表示命令执行效果直接为文件生效,即直接在文件中删除行首空格
命令解释:
第一个/的左边是s表示替换,即将空格替换为空。
第一个/的右边是中括号表示"或",空格或tab中的任意一种。这是正则表达式的规范。
中括号右边是*,表示一个或多个。
第二个和第三个\中间没有东西,表示空
删除文件中行尾空格的命令
# sed 's/[ \t]*$//g' filename #仅仅在当前终端显示执行效果。
# sed -i 's/[ \t]*$//g' filename #直接删除文件中行尾空格。
和上面稍微有些不同是前面删除了^符,在后面加上了美元符。表示删除行尾空格
14、shell 中的特殊符号
* 任意个任意字符。可以是单个字符,也可以是多个字符。
? 任意一个字符。只能是单个字符
# 注释字符
\ 脱义字符,转义符号
$ 变量的前缀
$ 正则里面表示行尾
^ 正则里面表示行首
; 多条命令写到一行,用;分割
~ 用户的家目录。正则表达式里表示匹配符
& 把命令放到后台
> 正确重定向
>> 正确追加重定向
2> 错误重定向
2>> 错误追加重定向
&> 正确错误重定向。 通过在一条命令后面加上">/dev/null 2>&1"表示这条命令执行后不打印任何信息,执行正确或错误信息都不打印!
&& 当前面的命令执行成功时,才执行后面的命令
|| 用在shell中表示或者的意思,如果第一条命令执行成功,则不执行第二条命令。如果第一条命令不成功,则执行第二条命令
15、cut命令的使用
cut 截取
-d 指定分隔符
-f 指定截取那一段
# cat filename|cut -d":" -f2 打印filename文件中以:分割的第2列
# cat filename|cut -d ":" -f 1-3 打印filename文件中以:分割的第1到3列,但是会保留分隔符!!!!!
[root@ss-server ~]# cat test|cut -d"/" -f2
b
2
[root@ss-server ~]# cat test|cut -d"/" -f1-3
a/b/c
1/2/3
[root@ss-server ~]# cat test|awk -F"/" '{print $2}'
b
2
[root@ss-server ~]# cat test|awk -F"/" '{print $1$2$3}'
abc
123
[root@ss-server ~]# cat test|awk -F"/" '{print $1"/"$2"/"$3}'
a/b/c
1/2/3
16、wc 命令的使用
wc 统计
wc file 默认统计file文件的行数、单词数,以及该文件的字节数。即默认加了-l、-w、-c
-l 统计行数
-c 统计字节数
-w 统计字符串,即统计单词数 (默认以空白格或,为分隔符)
-m 统计字符数 (隐藏的换行符也算,用cat -A 查看隐藏符号)
-L 显示最长行的长度
[root@ss-server ~]# cat test
wang bo
zhang heng yuan
xiao ru
zhu ge shen hou
[root@ss-server ~]# wc test
4 11 49 test
[root@ss-server ~]# cat test|wc -l -w -c
4 11 49
[root@ss-server ~]# cat test|wc -l
4
[root@ss-server ~]# cat test|wc -w
11
[root@ss-server ~]# cat test|wc -c
49
[root@ss-server ~]# cat test|wc -m
49
[root@ss-server ~]# cat test|wc -L
16
17、splite 命令的使用
split 用于将一个文件分割成数个。该指令将大文件分割成较小的文件,在默认情况下将按照每1000行切割成一个小文件。
split -b 100M filename 以文件大小切割 (可以指定文件前缀,默认是x开头)
split -l 1000 filename 以行数切割,相当于"split -1000" filename
[root@ss-server ~]# du -sh aa
710M aa
[root@ss-server ~]# split -b 100M aa #指定按照每个小文件100M的大小来分割aa文件
[root@ss-server ~]# du -sh *
710M aa
100M xaa
100M xab
100M xac
100M xad
100M xae
100M xaf
100M xag
9.7M xah