最近在弄linux内核相关东西,经常要写一些脚本文件,这里简单记录一下。
这里简单的记录一下。
1.首先获取当前路径
path=$(pwd)
echo $path
echo 就是打印对应数据。
srcPath=${path}/bin/makefile/tg/bin
curPath=${path}/cur/src/include
echo $srcPath
echo $curPath
testpath=$srcPath
echo "1"
echo ${testpath#/*bin}
echo ${testpath##/*bin}
echo "2"
echo ${testpath%/*bin}
echo ${testpath%%/bin*bin}
echo "3"
echo ${testpath/bin/BIN}
echo ${testpath//bin/BIN}
echo "4"
pe=${testpath%%bin*/bin}${testpath#/*bin/}
echo $pe
输出结果为
/home/pkqbp/work/test-small/deletT/curd
/home/pkqbp/work/test-small/deletT/curd/bin/makefile/tg/bin
/home/pkqbp/work/test-small/deletT/curd/cur/src/include
1
/makefile/tg/bin
2
/home/pkqbp/work/test-small/deletT/curd/bin/makefile/tg
/home/pkqbp/work/test-small/deletT/curd
3
/home/pkqbp/work/test-small/deletT/curd/BIN/makefile/tg/bin
/home/pkqbp/work/test-small/deletT/curd/BIN/makefile/tg/BIN
4
/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 但是只会替换当前第一个
// 就可以将全部替换掉
/home/pkqbp/work/test-small/deletT/curd/BIN/makefile/tg/bin
/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
今天就记录到这里,下次见,如果没有看到我,祝你早安、午安、晚安。