zoukankan      html  css  js  c++  java
  • shell脚本知识点汇总

    sed中在对内容进行修改时,有时候需要引用外部变量的值或者获取一个shell命令执行的结果,以便达到更加可观的输出结果

    1、sed中使用变量替换
    1)sed命令使用双引号的情况下,使用$var直接引用
    [rooot@192 ~]$ cat test.txt
    192.168.53.128/contiv/name
    [rooot@192 ~]$ ip=192.168.53.77
    [rooot@192 ~]$ sed -i "s/192.168.53.128/$ip/g" test.txt
    [rooot@192 ~]$ cat test.txt
    192.168.53.77/contiv/name
    [rooot@192 ~]$
    如果替换的变量内容中含有/符号则会提示错误,原因是从语法上看,没有任何问题;但由于变量中包含有“/”作为分隔符,这会和sed的替换操作的分隔符“/”引起混淆;所以,只要不使用“/”做分隔符就可以解决这个问题,如果使用“%”而不是“/”来作为sed的替换操作的分隔符,就不会出错。其实使用#或%或;作为分隔符也是可以的,只要不会与替换中有相同的而且不是元字符的特殊符号都是可以的
    [rooot@192 chenwei]$ path=/home/root
    [rooot@192 chenwei]$ cat test.txt
    192.168.53.77/contiv/name
    [rooot@192 chenwei]$ sed -i "s%192.168.53.77%$path%g" test.txt
    [rooot@192 chenwei]$ cat test.txt
    /home/root/contiv/name
    [rooot@192 chenwei]$ sed -i "s#/home/root#192.168.53.77#g" test.txt
    [rooot@192 chenwei]$ cat test.txt
    192.168.53.77/contiv/name
    [rooot@192 chenwei]$
    2)sed命令使用单引号的情况下,使用'"$var"'引用,即变量用双引号括起来,外面再加上单引号
    [rooot@192 chenwei]$ ip=192.168.0.34
    [rooot@192 chenwei]$ cat test.txt
    192.168.53.77/contiv/name
    [rooot@192 chenwei]$ sed -i 's/192.168.53.77/'"$ip"'/g' test.txt
    [rooot@192 chenwei]$ cat test.txt
    192.168.0.34/contiv/name
    [rooot@192 chenwei]$

    2、sed中执行外部命令
    1)sed命令使用单引号的情况下使用'`shell command`'或者'$(shell command)'引用命令执行的结果
    [rooot@192 chenwei]$ cat test.txt
    192.168.0.34/contiv/name
    [rooot@192 chenwei]$ ip=192.168.0.56
    [rooot@192 chenwei]$ sed -i 's/192.168.0.34/'`echo $ip`'/g' test.txt
    [rooot@192 chenwei]$ cat test.txt
    192.168.0.56/contiv/name
    [rooot@192 chenwei]$
    或者使用新式的命令
    [rooot@192 chenwei]$ cat test.txt
    192.168.0.56/contiv/name
    [rooot@192 chenwei]$ ip=192.168.0.68
    [rooot@192 chenwei]$ sed -i 's/192.168.0.56/'$(echo $ip)'/g' test.txt
    [rooot@192 chenwei]$ cat test.txt
    192.168.0.68/contiv/name
    [rooot@192 chenwei]$
    2.sed命令使用双引号的情况下直接`shell command`或者$(shell command)引用命令执行的结果
    [rooot@192 chenwei]$ cat test.txt
    192.168.0.68/contiv/name
    [rooot@192 chenwei]$ ip=192.168.0.56
    [rooot@192 chenwei]$ sed -i "s/192.168.0.68/$(echo $ip)/g" test.txt
    [rooot@192 chenwei]$ cat test.txt
    192.168.0.56/contiv/name

    在sed语句里面,变量替换或者执行shell命令,双引号比单引号少绕一些弯子

    3、一些小技巧

    在每行的头添加字符,比如"HEAD",命令如下:
    sed 's/^/HEAD&/g' test.file
    在每行的行尾添加字符,比如“TAIL”,命令如下:
    sed 's/$/&TAIL/g' test.file
    1)"^"代表行首,"$"代表行尾
    2)'s/$/&TAIL/g'中的字符g代表每行出现的字符全部替换,否则只会替换每行第一个,而不继续往后找了

    4、直接修改文件的内容

    直接编辑文件选项-i,会匹配test.txt文件中每一行的第一个This替换为this:
    sed -i 's/This/this/' test.txt

    5、shell变量的写法

    ${var} 变量var的值, 与$var相同

    echo ${s1}${s2}   # 当然这样写 $s1$s2 也行,但最好加上大括号  

    6、shell支持逻辑与或的写法

    [[]] 表达式
    [root@localhost ~]# [ 1 -eq 1 ] && echo 'ok'
    ok
    [root@localhost ~]$ [[ 2 < 3 ]] && echo 'ok'
    ok

    [root@localhost ~]$ [[ 2 < 3 && 4 > 5 ]] && echo 'ok'
    ok
    注意:[[]] 运算符只是[]运算符的扩充。能够支持<,>符号运算不需要转义符,它还是以字符串比较大小。里面支持逻辑运算符:|| &&

  • 相关阅读:
    「模拟赛20180306」回忆树 memory LCA+KMP+AC自动机+树状数组
    python写一个通讯录
    Git学习笔记
    交换排序
    用Windows自带的方法创建WiFi
    MySQL之触发器
    插入排序
    range和arange的区别
    Spring前后端跨域请求设置
    三、图的定义及遍历
  • 原文地址:https://www.cnblogs.com/potato-chip/p/9824993.html
Copyright © 2011-2022 走看看