• linux shall中删除与替换


    最近在弄linux内核相关东西,经常要写一些脚本文件,这里简单记录一下。

    这里简单的记录一下。

    1.首先获取当前路径

    1. path=$(pwd)
    2. echo $path

    echo 就是打印对应数据。

    1. srcPath=${path}/bin/makefile/tg/bin
    2. curPath=${path}/cur/src/include
    3. echo $srcPath
    4. echo $curPath
    5. testpath=$srcPath
    6. echo "1"
    7. echo ${testpath#/*bin}
    8. echo ${testpath##/*bin}
    9. echo "2"
    10. echo ${testpath%/*bin}
    11. echo ${testpath%%/bin*bin}
    12. echo "3"
    13. echo ${testpath/bin/BIN}
    14. echo ${testpath//bin/BIN}
    15. echo "4"
    16. pe=${testpath%%bin*/bin}${testpath#/*bin/}
    17. echo $pe

    输出结果为

    1. /home/pkqbp/work/test-small/deletT/curd
    2. /home/pkqbp/work/test-small/deletT/curd/bin/makefile/tg/bin
    3. /home/pkqbp/work/test-small/deletT/curd/cur/src/include
    4. 1
    5. /makefile/tg/bin
    6. 2
    7. /home/pkqbp/work/test-small/deletT/curd/bin/makefile/tg
    8. /home/pkqbp/work/test-small/deletT/curd
    9. 3
    10. /home/pkqbp/work/test-small/deletT/curd/BIN/makefile/tg/bin
    11. /home/pkqbp/work/test-small/deletT/curd/BIN/makefile/tg/BIN
    12. 4
    13. /home/pkqbp/work/test-small/deletT/curd/makefile/tg/bin

    2.${testpath#/*bin}  其中# 为删除

    这个代表从头到尾开始查找第一个为bin的数据,删除前面数据

    如:

    /home/pkqbp/work/test-small/deletT/curd/bin/makefile/tg/bin

    通过代码,第一个头为/,然后找到第一个为bin的,就变成了

    /makefile/tg/bin

    2.1${testpath##/*bin} ## 也为删除

    代表头到尾开始查找最后一个为bin,然后将其删除,与是可以看到数据被清空了

     

    3${testpath%/*bin}  %为删除

    代表是从后往前找,找到最近的bin,然后将其删除

    /home/pkqbp/work/test-small/deletT/curd/bin/makefile/tg/bin

    变成了:

    /home/pkqbp/work/test-small/deletT/curd/bin/makefile/tg

    3.1${testpath%%/bin*bin}  %%也为删除

    代表是从后往前找,找到最远的bin,然后将其删除

    /home/pkqbp/work/test-small/deletT/curd/bin/makefile/tg/bin

    变成了:

    /home/pkqbp/work/test-small/deletT/curd

    4. 有关/ 与 // 这里统一讲解下

    ${testpath/bin/BIN} 将bin转换成BIN  但是只会替换当前第一个

    // 就可以将全部替换掉

    1. /home/pkqbp/work/test-small/deletT/curd/BIN/makefile/tg/bin
    2. /home/pkqbp/work/test-small/deletT/curd/BIN/makefile/tg/BIN

    下面就是一个组合运用,将中间的bin去掉,别的保留

    ${testpath%%bin*/bin}${testpath#/*bin/}

    原来路径:

    /home/pkqbp/work/test-small/deletT/curd/bin/makefile/tg/bin

    变化的路径:

    /home/pkqbp/work/test-small/deletT/curd/makefile/tg/bin

    今天就记录到这里,下次见,如果没有看到我,祝你早安、午安、晚安。

     

  • 相关阅读:
    Dispose CLose 析构函数 Finalize()
    想知道对象有没有在外面搞暧昧?简单几步就能查清楚
    ChatGPT4实现前一天
    【芯片前端】四年经验|芯片前端|IP设计岗|面试问题|总结分享
    PythonStudy2
    【Javaweb 1】带你搞懂request,respond,servlet
    链表有环,快慢指针走3步可以吗
    仅11w粉涨800w播放,UP主在B站新分区带货变现!
    kubeadm升级k8s
    tsconfig.json 设置exclude 路径后,编译依然会产生排除后的文件
  • 原文地址:https://blog.csdn.net/weixin_42126427/article/details/126783329