LinuxGem
此处为老的 LinuxGem,新版 LinuxGem 请移步 www.linuxgem.org

获得当前目录名称的 4 种方法

galeki posted @ 2007年11月08日 22:52 in Bash 和它的朋友们 with tags shell 目录 , 3357 阅读

翻译自: http://www.linux.com/feature/120291

在写 shell 脚本的时候,有时我们需要得到当前目录的名称,pwd 命令可以返回当前目录的完整路径,而不是目录名,如果要得到目录名,有下面 4 种方法:

basename

使用 basename 是最简单的方法:

basename /usr/local/bin
bin

链接 basename 和pwd,就可获得当前目录:

cd /usr/local/bin
basename `pwd`
bin

使用 ## 拆分变量

${var##pattern} 可以从 $var 变量中,去掉可以匹配 pattern 最大长度的字符串,所以,要获得目录名,可以:

cd /var/log/squid
echo ${PWD##*/}
squid

$PWD 是保存目录完整路径的环境变量,pattern 就是 */,也就是去掉了前面所有的上级目录。

使用 awk 和 rev

cd /usr/share/cups/data
pwd | rev | awk –F \/ '{print $1}' | rev
data

把结果分步显示,你就知道原理了:

pwd
/usr/share/cups/data
pwd | rev
atad/supc/erahs/rsu/
pwd | rev | awk –F \/ '{print $1}'
atad
pwd | rev | awk –F \/ '{print $1}' | rev
data

-F 选项是指定分隔符,目录之间分隔符为 /。

使用 sed

cd /home/smith/music
pwd | sed 's,^\(.*/\)\?\([^/]*\),\2,'
music

第一个 pattern,^\(.*/\)?,匹配 / 符号前所有的东西,第二个 [^/]* ,匹配剩余部分不包含 / 的部分。

 

anonymous 说:
2023年11月02日 20:30

I also wrote an article on a similar subject will find it at       write what you think. Pest Control Kissimmee

anonymous 说:
2023年11月06日 01:16

There you can download for free, see the first of these data.  Pest Control Orlando

anonymous 说:
2023年11月06日 01:18

it's really nice and meanful. it's really cool blog. Linking is very useful thing.you have really helped lots of people who visit blog and provide them usefull information. Pest Control Oviedo

anonymous 说:
2023年11月18日 22:22

Might you be able to please disclose to me how you compose such awesome articles? I am attempting a similar thing with my  work, but I can't be as good as you. I need to know the secret. Pest Control Tampa

anonymous 说:
2024年1月11日 13:49

On this page, you'll see my profile, please read this information.  cpa online

anonymous 说:
2024年1月16日 18:56

Interestingly you write, I will address        you'll find exciting and interesting things on similar topics. تصليح ثلاجات

anonymous 说:
2024年1月16日 19:01

I read this article. I think You put a lot of effort to create this article. I appreciate your work. تركيب كاميرات مراقبة الكويت

anonymous 说:
2024年1月16日 19:08

This is very interesting, but it is necessary to click on this link:  تصليح طباخات

anonymous 说:
2024年1月16日 19:17

It's superior, however , check out material at the street address. بنشر متنقل

anonymous 说:
2024年1月16日 19:26

I have a similar interest this is my page    read everything carefully and let me know what you think. تصليح طبخات المنطقة العاشره

anonymous 说:
2024年1月25日 20:03

I can recommend primarily decent and even responsible tips, as a result view it: Lentoria

anonymous 说:
2024年1月25日 20:13

It is quite beneficial, although think about the facts when it reaches this target. Lentor Mansion

anonymous 说:
2024年1月25日 20:24

Listed here you'll learn it is important, them offers the link in an helpful webpage: The Giverny Residences

Victoria Willis 说:
2024年1月30日 18:41

I just thought it may be an idea to post incase anyone else was having problems researching but I am a little unsure if I am allowed to put names and addresses on here. unique tiles

Clay Lowe 说:
2024年2月08日 22:29

Wherever people use technology, there will be people who need Managed IT Services. White River Networks offers plans that fit your budget and your business’ IT needs. We are Managed Service Provider from user applications to networks and cloud services.

Clay Lowe 说:
2024年2月13日 22:26

Can nicely write on similar topics! Welcome to      here you'll find out how it should look. weed dispensary Maryland

Clay Lowe 说:
2024年2月26日 15:45

5 stars for the c8pcg screens from screens-people.com! Incredible picture quality and easy setup. Highly recommend for a cinematic experience at home!

Clay Lowe 说:
2024年3月06日 15:34

Apprenticeships Are Amazing and Complicated. Learn more about the GoSprout Apprenticeship platform with Apprenticeship Management Software today and join the community solving the US skills gap.

Clay Lowe 说:
2024年3月26日 00:03

I might suggest solely beneficial in addition to trusted facts, and so find it: Door Entry Systems


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter