wwwjy2000 发表于 2007-08-28 21:04

10楼正解

:lol: :lol: :lol:

hansfly 发表于 2007-08-29 08:45

pwd | sed 's/\/[^\/]*$//'

apsong 发表于 2007-08-29 09:46

原帖由 leyyer 于 2007-8-28 18:12 发表 http://linux.chinaunix.net/bbs/images/common/back.gif
bash ?

a=/home/me/a.out
echo ${a%/*}
强哦……找到了相关解释(man bash),顶一下:

       ${parameter#word}
       ${parameter##word}
            Thewordisexpanded to produce a pattern just as in pathname
            expansion.If the pattern matches the beginning of the value of
            parameter,thentheresultofthe 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
            toeachpositional parameter in turn, and the expansion is the
            resultant list.If parameter is an arrayvariablesubscripted
            with@or*, the pattern removal operation is applied to each
            member of the array in turn, and the expansion is theresultant
            list.

       ${parameter%word}
       ${parameter%%word}
            Thewordisexpanded to produce a pattern just as in pathname
            expansion.If the pattern matches atrailingportionofthe
            expanded value of parameter, then the result of the expansion is
            the expanded value of parameter with the shortest matchingpat-
            tern(the``%''case)orthelongest matching pattern (the
            ``%%'' case) deleted.If parameteris@or*,thepattern
            removaloperationisappliedto each positional parameter in
            turn, and the expansion is the resultant list.If parameteris
            anarrayvariable subscripted with @ or *, the pattern removal
            operation is applied to each member of the arrayinturn,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 ofpat-
            ternagainstitsvalue is replaced with string.In the first
            form, only the first match is replaced.The second formcauses
            allmatchesof pattern to be replaced with string.If pattern
            begins with #, it must match at the beginningoftheexpanded
            valueof parameter.If pattern begins with %, it must match at
            the end of the expanded value of parameter.If string isnull,
            matchesofpattern are deleted and the / following pattern may
            be omitted.If parameter is @ or *, the substitutionoperation
            isapplied to each positional parameter in turn, and the expan-
            sion is the resultant list.If parameter is anarrayvariable
            subscriptedwith@ or *, the substitution operation is applied
            to each member of the array in turn, and theexpansionisthe
            resultant list.

guyulengyi 发表于 2007-08-29 11:14

:emn54: :emn58: :emn24: :emn25: :emn26: :em35: :em07: :em36: :em37: :emn54:

zwylinux 发表于 2007-08-29 11:26

很简单的问题嘛,而且楼上的几个兄弟已经给了几个方法了

jarhead1 发表于 2007-08-29 12:51

path="$(pwd -P)"
path="$(echo "${path%/*}")"
#弱引用是为了应付含有空格的目录的情况

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

codekiller 发表于 2007-08-29 14:02

没有dirname的情况下,2楼的答案是最佳答案。不同意的请举手。

fogsky 发表于 2007-08-29 14:34

path1=/a/b/c
path2=`echo $path1 | awk '{split($0,array,"/");print "/"array"/"array}'`
echo $path2

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

honckly 发表于 2007-08-29 15:13

回复 #17 codekiller 的帖子

还是2楼比较正 解。

coffee_45 发表于 2007-08-29 16:09

dirs可以,与pwd类似。
页: 1 [2] 3
查看完整版本: shell 编程 取文件绝对路径