Redhat(3)-Bash-Shell-正则表达式

1.bash脚本
2.bash变量、别名、算术扩展
3.控制语句
4.正则表达式


1.bash脚本  

#!/bin/bash

#this is basic bash script

<< BLOCK
This is the basic bash script
BLOKC

:'
This is the basic bash script
'

echo "hello world!"

 双引号、单引号只有在变量时才有区别
 双引号: 变量值
  单引号:字符

#!/bin/bash
comment="hello world"

echo"$comment"
echo'$comment'


输出:hello world
     $comment
#1.touch创建脚本
touch file_name

#2.vim 编写内容
#!/bin/bash
echo -n "hostname is :";hostname

#3.执行程序
chmod +x ./file_name.sh
./file_name.sh
/bin/sh file_name.sh

2.shell变量、别名、算术扩展
#!/bin/bash
 

declare -u first_name="Ji"; #定义大写的变量
echo  $first_name           #单独输出:JI

declare -l last_name="Ma";  #定义小写的变量
echo &last_name             #ma
 
echo $first_name_&last_name     #ma
echo &{first_name}_&{last_name} #字符拼接:JI_ma


echo file-{1..3}  #file-1 file-2 file-3
echo &[ &num+5 ]
# 更改EDITOR
EDITOR=vim;export EDITOR
export EDITOR=vim

#更改LANG
export LANG=zh_CN.UTF-8

#更改PATH
export PATH=&{PATH}:/home/student/sbin


3.控制语句
 
3.1for语句
 

#1.基本语法  两个小括号
for((i=n;i<=m;i++))
do
  command1;
done

sum=0;
for((i=n;i<=m;i++))
do
  let sum=$[sum+i];
done


for num in{1..10};
do
   echo $num;
done


#2.$*和$@区别

#!/bin/bash
for args in $*
do
  echo $args
done

for args in "$@"
do 
  echo $args
done
    


  
4.正则表达式
    
正则表达式提供一种便于查找特定内容的模式匹配机制。  

cp /etc/fstab .
vim fstab

影响多少行:% ,s替换,开头:/^ 以UUID开头;中间:.* 任意;结尾:defaults/;/:替换成 
:%s/^UUID.*defaults//
:1,3  #只控制1-3行
#1 查找test文件中的字符串 cat
#2.需要加单引号,特殊字符
cat test|grep 'cat'


匹配规则:


 

 4.1扩展
   grep -E 'a|b'

#在test里找 a 或者 b
cat test|grep -E 'a|b'
cat test| egrep 'ab|bc'

4.2非打印字符

 4.3 限定次数,指定正则表达式出现次数

 比如:在文件test中查找 abc 出现2次的字符:
cat test| egrep '(abc){2}'
在文件test中查找 abc 出现2次以上的字符:
cat test| egrep '(abc){2,}'
在文件test中查找 abc 出现2次以下的字符:
cat test| egrep '(abc){,2}'
在文件test中查找 abc 出现1-3次的字符:
cat test| egrep '(abc){1,3}'


在文件test中查找 a 出现1次以上:
cat test| egrep 'a+' 
在文件test中查找 a 出现任意次数:
cat test| egrep 'a{0,}'
cat test| egrep 'a*'

4.4 定位符
  行首、行尾;单词开头、单词结尾。

 catssssss
catsssscatsssscat
查找以cat开头的:
cat test |grep ‘^cat’
查找以cat开头,后面多一个字符
cat test|grep  ‘^cat.’
查找以cat开头,后面一整行
cat test|grep  ‘^cat.*’

查找以cat结尾
cat test |grep ‘cat$’
查找以cat结尾的一整行
cat test |grep ‘.*cat$’

左边界:左边为空
cat test | egrep '\bcat'
右边界:右边为空
cat test | egrep 'cat\b'
左右边界:
cat test | egrep '\bcat\b'

4.5 反向引用
   提供查找文本中两个相同的相邻单词的匹配项的能力。

cat test | egrep '(laoma).*(laoma)'
cat test|grep '(laoma).\1'  #前面小括号第一次出现的值,*表示任意字符。
返回:laoma laoniu laohu laoma
cat test|grep '(laoma).*(laoniu).*\2'  laoma  laoniu 出现两次
返回:laoma laoniu laohu laoma laoniu 

 
  \b:左边边界;[a-z]+:小写字符每个单词;空格;\1:一模一样的 ;\b右边界

4.6匹配


 cat test | egrep 'ca|bo'
 cat test |grep -e ca -e bo
以上两者等价。

cat test |grep -i cat     忽略大小写
cat test |grep -i -v cat 不包含 cat

4.7输出

 在 目录/etc 中找包含 servera的文件。
grep -R 'servera' /etc
grep -R &(hostname) /etc 查找主机名
grep -R  &(hostname)  /etc 2>/dev/null  报错的丢掉
grep -R  &(hostname)  /etc 2>/dev/null  -l 文件名



  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值