免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: supeiwang
打印 上一主题 下一主题

shell 编程 取文件绝对路径 [复制链接]

论坛徽章:
0
11 [报告]
发表于 2007-08-28 21:04 |只看该作者

10楼正解

论坛徽章:
0
12 [报告]
发表于 2007-08-29 08:45 |只看该作者
pwd | sed 's/\/[^\/]*$//'

评分

参与人数 1可用积分 +9 收起 理由
MMMIX + 9 我很赞同

查看全部评分

论坛徽章:
0
13 [报告]
发表于 2007-08-29 09:46 |只看该作者
原帖由 leyyer 于 2007-8-28 18:12 发表
bash ?

a=/home/me/a.out
echo ${a%/*}

强哦……找到了相关解释(man bash),顶一下:

       ${parameter#word}
       ${parameter##word}
              The  word  is  expanded to produce a pattern just as in pathname
              expansion.  If the pattern matches the beginning of the value of
              parameter,  then  the  result  of  the expansion is the expanded
              value of parameter with the shortest matching pattern (the ``#''
              case) or the longest matching pattern (the ``##'' case) deleted.
              If parameter is @ or *, the pattern removal operation is applied
              to  each  positional parameter in turn, and the expansion is the
              resultant list.  If parameter is an array  variable  subscripted
              with  @  or  *, the pattern removal operation is applied to each
              member of the array in turn, and the expansion is the  resultant
              list.

       ${parameter%word}
       ${parameter%%word}
              The  word  is  expanded to produce a pattern just as in pathname
              expansion.  If the pattern matches a  trailing  portion  of  the
              expanded value of parameter, then the result of the expansion is
              the expanded value of parameter with the shortest matching  pat-
              tern  (the  ``%''  case)  or  the  longest matching pattern (the
              ``%%'' case) deleted.  If parameter  is  @  or  *,  the  pattern
              removal  operation  is  applied  to each positional parameter in
              turn, and the expansion is the resultant list.  If parameter  is
              an  array  variable subscripted with @ or *, the pattern removal
              operation is applied to each member of the array  in  turn,  and
              the expansion is the resultant list.

       ${parameter/pattern/string}
       ${parameter//pattern/string}
              The pattern is expanded to produce a pattern just as in pathname
              expansion.  Parameter is expanded and the longest match of  pat-
              tern  against  its  value is replaced with string.  In the first
              form, only the first match is replaced.  The second form  causes
              all  matches  of pattern to be replaced with string.  If pattern
              begins with #, it must match at the beginning  of  the  expanded
              value  of parameter.  If pattern begins with %, it must match at
              the end of the expanded value of parameter.  If string is  null,
              matches  of  pattern are deleted and the / following pattern may
              be omitted.  If parameter is @ or *, the substitution  operation
              is  applied to each positional parameter in turn, and the expan-
              sion is the resultant list.  If parameter is an  array  variable
              subscripted  with  @ or *, the substitution operation is applied
              to each member of the array in turn, and the  expansion  is  the
              resultant list.

论坛徽章:
0
14 [报告]
发表于 2007-08-29 11:14 |只看该作者

论坛徽章:
0
15 [报告]
发表于 2007-08-29 11:26 |只看该作者
很简单的问题嘛,而且楼上的几个兄弟已经给了几个方法了

论坛徽章:
0
16 [报告]
发表于 2007-08-29 12:51 |只看该作者
path="$(pwd -P)"
path="$(echo "${path%/*}")"
#弱引用是为了应付含有空格的目录的情况

[ 本帖最后由 jarhead1 于 2007-8-29 12:52 编辑 ]

论坛徽章:
0
17 [报告]
发表于 2007-08-29 14:02 |只看该作者
没有dirname的情况下,2楼的答案是最佳答案。不同意的请举手。

论坛徽章:
0
18 [报告]
发表于 2007-08-29 14:34 |只看该作者
path1=/a/b/c
path2=`echo $path1 | awk '{split($0,array,"/");print "/"array[2]"/"array[3]}'`
echo $path2

[ 本帖最后由 fogsky 于 2007-8-29 14:38 编辑 ]

论坛徽章:
0
19 [报告]
发表于 2007-08-29 15:13 |只看该作者

回复 #17 codekiller 的帖子

还是2楼比较正 解。

论坛徽章:
0
20 [报告]
发表于 2007-08-29 16:09 |只看该作者
dirs  可以,与pwd类似。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP