shell 获取 目录名 当前目录名
2015-09-10 11:19
585 查看
Four ways to extract the current directory name
By Sergio Gonzalez Duran on November 06, 2007 (9:00:00 AM)When you're programming a shell script, you often only need the current directory name, not the whole path that the
pwdcommand
returns. Here are four ways you can extract only the current directory.
Using basename
Using thebasenamecommand is the easiest and simplest way to extract the current directory:
basename /usr/local/bin bin
However, it isn't useful in a shell script with changing directory variables. You can combine it with
pwdinside
backticks to make it more dynamic:
cd /usr/local/bin basename `pwd` bin
Using parameter substitution with echo
The bash scripting language is full of nice tricks, including parameter substitution, which allows you to manipulate or expand variables. You can use parameter substitution with the${var##pattern}syntax,
which removes from
$varthe longest part of
$Patternthat
matches the front end of
$var. Take a look at an example:
cd /var/log/squid echo ${PWD##*/} squid
PWDis the environment variable that holds the current path, and ## is the instruction that tells the script
to remove everything it finds up to */. In other words, it removes everything until the last /, leaving only the last string, which here is the current directory,
squid.
You can learn more about parameter substitution and other ways to manipulate variables in the Advanced Bash-Scripting
Guide.
Using awk and rev
A more elaborate solution uses a combination ofawk(a pattern-scanning utility) and
rev(a
utility that reverses lines from a file or from stdin):
cd /usr/share/cups/data pwd | rev | awk –F \/ '{print $1}' | rev data
It's a lot easier to understand this kind of script step by step:
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
The
-Foption indicates that you should separate by fields, where the field delimiter is /, and that you should
print field 1.
Using sed
Finally, you can parsepwdoutput in the stream editor
sedusing
an elaborate regular expression. This approach may be educational, but it's not practical:
cd /home/smith/music pwd | sed 's,^\(.*/\)\?\([^/]*\),\2,' music
For a better understanding of how this works, remove the escape character (\), which is required for special characters such as "(":
sed 's,^(.*/)?([^/]*),\2,'
ssubstitutes one string for another. It looks for two patterns, which are indicated between the first comma
and the second comma. The first pattern (
^(.*/)?) searches from the beginning of the line (^) until the last
occurrence that it finds of / (in the example, it matches
/home/smith/). The second pattern (
([^/]*))
searches everything from the last pattern except the / character , which is indicated by
[^/]*, where ^ at the
beginning of the square brackets means not. This results in both
/home/smith/and
music.
The second part of this regular expression is the substitution, indicated by \2. In sed, this is called a back reference. As its name implies, it goes back and recalls a previously used reference. There may be nine
of these, named \1, \2, \3, and so on. In the example, \2 refers to the second pattern found, which is
music--
the result expected.
As you can see, Linux gives you many ways to find a directory name. Having many choices for the same chore is one of its strengths.
Sergio Gonzalez Duran is a Linux administrator, systems developer, and network security counselor who also teaches Linux courses and publishes the Spanish-oriented Linux and open source Web site linuxtotal.com.mx.
相关文章推荐
- bash内建命令type简介
- Bash shell常用技巧
- bash
- Unity3D 之 Shell更新svn出现冲突
- zabbix中通过shell脚本进行微信监控告警
- shell 转义符号
- shell 99 乘法表
- bash编程实现冒泡排序
- shell字符串操作
- 吓尿了,mac下bash出了问题
- Linux SHELL之source与.的区别
- How To Make Cscope Database by Shell
- bash小总结
- oh-my-zsh配置你的zsh提高shell逼格终极选择
- Tomcat、Weblogic、JBoss、GlassFish、Resin、Websphere弱口令及拿webshell方法总结 [复制链接]
- shell 守护进程
- 虚拟机上安装完成redhat后无法上网和Xshell无法连接虚拟机的问题
- mac常用shell命令
- Shell笔记
- shell脚本操作mysql数据库 (部份参考)