linux shell中删除与替换

最近在弄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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值