shell 获取 目录名 当前目录名
Four ways to extract the current directory name
When you're programming a shell script, you often only need the current directory name, not the whole path that the pwd command returns. Here are four ways you can extract only the current directory.
Using basename
Using the basename command 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 pwd inside 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 $var the longest part of $Pattern that matches the front end of $var. Take a look at an example:
cd /var/log/squid
echo ${PWD##*/}
squid
PWD is 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 of awk (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 -F option indicates that you should separate by fields, where the field delimiter is /, and that you should print field 1.
Using sed
Finally, you can parse pwd output in the stream editor sed using 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,'
s substitutes 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.
shell 获取 目录名 当前目录名的更多相关文章
- shell 提取文件名和目录名
转自http://blog.csdn.net/universe_hao/article/details/52640321 shell 提取文件名和目录名 在写shell脚本中,经常会有需要对路径和文件 ...
- Bash Shell 获取进程 PID
转载地址:http://weyo.me/pages/techs/linux-get-pid/ 导读 Linux 的交互式 Shell 与 Shell 脚本存在一定的差异,主要是由于后者存在一个独立的运 ...
- 用C++和shell获取本机CPU、网卡IO、内存、磁盘等的基本信息
用C++和shell获取本机CPU.网卡.内存.磁盘等的基本信息: 由于对C++相关的函数没多少了解,但是觉得用shell反而相对简单一些: 一.shell脚本,用来辅助C++获取主机的资源使用信息 ...
- linux中用shell获取昨天、明天或多天前的日期
linux中用shell获取昨天.明天或多天前的日期 时间 -- :: BlogJava-专家区 原文 http://www.blogjava.net/xzclog/archive/2015/12/0 ...
- linux中用shell获取时间,日期
linux中用shell获取昨天.明天或多天前的日期:在Linux中对man date -d 参数说的比较模糊,以下举例进一步说明:# -d, --date=STRING display time d ...
- shell 获取网关 以及修改ip 启用网卡
shell 获取网关 以及修改ip 启用网卡 #!/bin/bash #autho freefei #script is a init computer eth #data 2014 10 09 19 ...
- python执行shell获取硬件参数写入mysql
最近要获取服务器各种参数,包括cpu.内存.磁盘.型号等信息.试用了Hyperic HQ.Nagios和Snmp,它们功能都挺强大的,但是于需求不是太符,亦或者太heavy. 于是乎想到用python ...
- Shell获取格式化日期
Shell获取格式化日期 shell date 获取昨天日期 使用date -d 选项: date +"%Y%m%d" -d "+n days" 今天的后n天日 ...
- Shell获取文件的文件名和扩展名的例子
这篇文章主要介绍了Shell获取文件的文件名和扩展名的例子,简明版的代码实例,看了就懂,需要的朋友可以参考下 basename example.tar.gz .tar.gz # => examp ...
- linux命令(26):Bash Shell 获取进程 PID
转载地址:http://weyo.me/pages/techs/linux-get-pid/ 根据pid,kill该进程:http://www.cnblogs.com/lovychen/p/54113 ...
随机推荐
- find_sys_call_table和kallsysms_lookup_name的区别
find_sys_call_table 和 kallsyms_lookup_name 都可以用于查找内核符号,但它们的具体作用和使用场景有所不同.以下是两者的详细对比: 1. find_sys_cal ...
- 推荐一款轻量级 eBPF 前端工具 ply
1 Overview ply 是 eBPF 的 front-end 前端工具之一,专为 embedded Linux systems 开发,采用 C 语言编写,只需 libc 和内核支持 BPF 就可 ...
- 云原生周刊:Docker大涨价|2024.10.8
开源项目推荐 Kubeshark 如果把 K8s 比作操作系统,那它就是 K8s 上的 tcpdump,使用起来就像 Chrome 开发者工具一样简单直接,能够让 K8s 上微服务之间的网络通信一览无 ...
- KubeSphere Cloud 月刊|灾备支持 K8s 1.22+,轻量集群支持安装灾备和巡检组件
功能升级 备份容灾服务支持 K8s v1.22+ 版本集群 随着 Kubernetes 近一年频繁的发版.升级,越来越多的用户开始部署并使用高版本的 Kubernetes 集群.备份容灾服务支持 Ku ...
- 基于云原生的私有化 PaaS 平台交付实践
作者:牛玉富,某知名互联网公司专家工程师.喜欢开源 / 热衷分享,对 K8s 及 golang 网关有较深入研究. 本文将解读如何利用云原生解决私有化交付中的问题,进而打造一个 PaaS 平台,提升业 ...
- 云原生爱好者周刊:非容器化应用也需要 Docker Compose
开源项目推荐 Process Compose Process Compose 是一个调度编排工具,不过不是用来调度容器的,而是用来调度非容器化的应用,可以定义进程的依赖性和启动顺序,也可以定义重启策略 ...
- Machine Learning Week_1 Welcome
目录 0 Welcome 0.1 Video: Welcome to Machine Learning! Transcript unfamiliar words 0.2 Reading: Machin ...
- CSP2024 游记 - 未完
CSP2024 游记 \[written\ by:\mathbb{CMRHHH} \] 此时 :2024/10/25 ;18:30: 路途颠簸,作业先不写了吧--有些晕了,正在听杰伦的仙乐: CCF真 ...
- php如何快速入门
PHP交流群 656679284 为PHP广大爱好者提供技术交流,有问必答,相互学习相互进步! 学习教程 学习前期,首要的WEB前端基础知识,比如html5/css3/java/jquery有个简 ...
- NATAPP实现内网穿透简易教程
NATAPP是什么 NATAPP是一个十分容易上手的内网穿透工具,可以把本机的ip和端口映射到公网,将本机暴露在公网中供他人访问. 这在进行一些回调接口的本地测试(如支付宝微信支付的回调接口)时十分好 ...