首页 > 系统相关 >shell 获取 目录名 当前目录名

shell 获取 目录名 当前目录名

时间:2022-11-15 12:14:06浏览次数:68  
标签:shell 当前目录 pattern rev current pwd 目录名 directory Using

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 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,当前目录,pattern,rev,current,pwd,目录名,directory,Using
From: https://www.cnblogs.com/exmyth/p/16891989.html

相关文章

  • 初识Linux(九)------ 学习Shell Scripts
    基本上,shellscript有点像是早期的批处理文件,亦即是将一些指令汇整起来一次执行,但是Shellscript拥有更强大的功能,那就是他可以进行类似程序(program)的编写,并且不......
  • Windows编写Shell脚本在Linux上运行报错的问题记录
    问题:bin/sh^M:badinterpreter:Nosuchfileordirectory原因:.sh脚本在windows系统下用记事本文件编写的。不同系统的编码格式引起的。解决方法:修改.sh文件格式(1)使用......
  • Linux——虚拟机如何连接XShell
    目录一、检查虚拟机设置的网络连接是否为NAT模式二、虚拟网络编辑器中查看IP地址三、设置虚拟机的IP地址,子网掩码,网关,DNS四、XSell中连接虚拟机五、总结一、检查虚拟......
  • Linux shell脚本全面学习
    1.Linux脚本编写基础1.1语法基本介绍1.1.1开头程序必须以下面的行开始(必须方在文件的第一行):#!/bin/sh符号#!用来告诉系统它后面的参数是用来执行该文件的程序。在......
  • 24 道必知必会的 Shell 脚本面试题
    虽然现在Python在运维工作中已经使用很普遍,但是很多企业在找Linux云计算工程师的时候还是会问到shell脚本的问题,它有助于你在工作环境中自动完成很多任务。如下是一些面试......
  • 第一章 linux-shell命令入门
      本人目前正在学习linux驱动开发,利用我想记录我Linux的学习过程,并分享出来,为大家提供一些经验,同时加强我对知识的总结能力,希望我的博客能为大家提供帮助。废话不多说,我......
  • Linux shell脚本执行后出现语法错误: 未预期的文件结尾
    Windows环境下编写了一个shell脚本,上传到Linux环境中执行,Linuxshell脚本执行后出现语法错误:未预期的文件结尾。出现了此错误提示,进行了如下的检查:1、检查Shell脚本的......
  • shell 程序中 2> /dev/null(转)
    转自:shell程序中2>/dev/null2>/dev/null的意思就是将标准错误stderr删掉手抄一段《linuxshell脚本攻略》描述:/dev/null是一个特殊的设备文件,这个文件接收到的任何......
  • shell脚本测试配置寄存器数值·
    shell脚本测试同一寄存器下配置不同数值配置的测试##!/bin/shreg_value=0check_reg=`devmem0x10081000`while[$reg_value-lt252]do echo"CurrentTx_Reg_Valu......
  • Shell外壳的简易模拟
    写在前面我们来谈目前进程控制的最后的一个内容,这个博客内容主要涉及到几个进程替换的相关函数,我们需要学习它们的用法.最后我们需要模拟实现一个简易的shell作为进程控制......