本文转载编辑自:<wbr style="line-height:25px"><a rel="nofollow" href="http://www.linuxdiyf.com/viewarticle.php?id=66575" style="color:rgb(207,121,28); line-height:25px; text-decoration:none">http://www.linuxdiyf.com/viewarticle.php?id=66575</a> <div style="line-height:25px"> <div style="line-height:25px"><span style="color:#003366; line-height:25px">在写 shell 脚本的时候,有时我们需要得到当前目录的名称,pwd 命令可以返回当前目录的完整路径,而不是目录名,如果要得到目录名,有下面 4 种方法:</span></div> <div style="line-height:25px"><span style="line-height:25px"><span style="font-size:16px; line-height:28px">方法一,</span>使用basename命令</span></div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">使用 basename 是最简单的方法:</span></div> <div style="line-height:25px"><span style="line-height:25px">示例1.0</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">basename /usr/local/bin</span></div> <div style="line-height:25px">结果:<span style="color:#3366ff; line-height:25px">bin</span> </div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">把 basename 和pwd,就可获得当前目录。</span></div> <div style="line-height:25px"> <span style="line-height:25px">示例1.1</span>:</div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">cd /usr/local/bin</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">basename `pwd`</span></div> <div style="line-height:25px">结果:<span style="color:#3366ff; line-height:25px">bin</span> </div> <div style="line-height:25px"><span style="line-height:25px"><span style="font-size:16px; line-height:28px">方法二</span>,使用 ## 拆分PWD变量</span></div> <div style="line-height:25px"> <span style="color:#0000ff; line-height:25px">${var##pattern}</span><span style="color:#000080; line-height:25px">可以从 $var 变量中,去掉可以匹配 pattern 最大长度的字符串,所以,要获得目录名,可以:</span> </div> <div style="line-height:25px"><span style="line-height:25px">示例2.0:</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">cd /var/log/squid</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">echo ${PWD##*/}</span></div> <div style="line-height:25px">结果:<span style="color:#3366ff; line-height:25px">squid</span> </div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">$PWD 是保存目录完整路径的环境变量,pattern 就是 */,也就是去掉了前面所有的上级目录。</span></div> <div style="line-height:25px"><span style="line-height:25px"><span style="font-size:16px; line-height:28px">方法三,</span>使用 awk 和 rev</span></div> <div style="line-height:25px"><span style="line-height:25px">示例3.0:</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">cd /usr/share/cups/data</span></div> <div style="line-height:25px"><span style="color:#ff6600; line-height:25px">pwd | rev | awk –F \/ '{print $1}' | rev</span></div> <div style="line-height:25px">结果:<span style="color:#3366ff; line-height:25px">data</span> </div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">把结果分步显示,你就知道原理了:</span></div> <div style="line-height:25px"><span style="color:#0000ff; line-height:25px">pwd</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">/usr/share/cups/data</span></div> <div style="line-height:25px"><span style="color:#0000ff; line-height:25px">pwd | rev</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">atad/supc/erahs/rsu/</span></div> <div style="line-height:25px"><span style="color:#0000ff; line-height:25px">pwd | rev | awk –F \/ '{print $1}'</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">atad</span></div> <div style="line-height:25px"><span style="color:#0000ff; line-height:25px">pwd | rev | awk –F \/ '{print $1}' | rev</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">data</span></div> <div style="line-height:25px"> <span style="color:#ff6600; line-height:25px">-F</span><span style="color:#0000ff; line-height:25px">选项是指定分隔符,目录之间分隔符为</span><span style="color:#ff6600; line-height:25px">/</span><span style="color:#0000ff; line-height:25px">。</span> </div> <div style="line-height:25px"><span style="line-height:25px"><span style="font-size:16px; line-height:28px">方法四</span>,使用 sed</span></div> <div style="line-height:25px"><span style="line-height:25px">示例4.0</span></div> <div style="line-height:25px"><span style="color:#3366ff; line-height:25px">cd /home/smith/music</span></div> <div style="line-height:25px"><span style="color:#0000ff; line-height:25px">pwd | sed 's,^\(.*/\)\?\([^/]*\),\2,'</span></div> <div style="line-height:25px">结果:<span style="color:#3366ff; line-height:25px">music</span> </div> <div style="line-height:25px"><span style="color:#000080; line-height:25px">第一个 pattern,^\(.*/\)?,匹配 / 符号前所有的东西,第二个 [^/]* ,匹配剩余部分不包含 / 的部分。</span></div> </div> </wbr>