码迷,mamicode.com
首页 > 系统相关 > 详细

Shell中#*/和%/*是什么意思?

时间:2014-11-05 17:20:00      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   for   sp   div   

shell中有许多奇特的语法:

比如有以下一段脚本;

#!/bin/sh
files=`find -name *.conifg`
for i in $files
do
  name=${i#*/}
  dir=${name%/*}
done


name和dir都代表什么呢?

假如

i=this/is/a/path.config

那么

name=is/a/path.config

dir=this/is/a


也就是说%/*代表取从头到最后一个slash之前的所有内容

#*/代表去取从第一个slash之后的所有内容

为了更好的理解,我们看一下具体的文档,可参考Advanced Bash-Scripting Guide一书:

${string#substring}Strip shortest match of $substring from front of $string

${string%substring}Strip shortest match of $substring from back of $string


也就是说

#代表删除从前往后最小匹配的内容

%代表删除从后往前最小匹配的内容


这样明白了吧。

原文:http://blog.csdn.net/hongchangfirst/article/details/28436947

作者:hongchangfirst

hongchangfirst的主页:http://blog.csdn.net/hongchangfirst



Shell中#*/和%/*是什么意思?

标签:style   blog   http   io   color   ar   for   sp   div   

原文地址:http://blog.csdn.net/hongchangfirst/article/details/28436947

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!