当前位置:网站首页>5. read the specified pathname -dirname
5. read the specified pathname -dirname
2022-06-11 10:38:00 【Qimu king · Prince】
1. brief introduction
dirname The command reads the specified pathname and deletes the last “/”( Slash ) And the characters after it , Keep the rest , And write the results to the standard output . If the last one “/” No characters after ,dirname The command uses the penultimate “/”, All subsequent characters are ignored .
Example :
1) dirname /etc/init.d/acpid -> /etc/init.d
2) dirname /etc/init.d/ -> /etc
3) dirname /etc/init.d -> /etc
4) dirname stdio.h -> .
5) dirname dir1/str dir2/str -> "dir1" followed by "dir2"
2. example
In many shell Script , You can often see the following statements
rootDir=$(cd $(dirname $0); pwd)
The purpose of this statement is to obtain shell The absolute path of the directory where the script is located , How to understand this sentence ? Why not use it directly pwd To get the current path ?
"." Represents the current path
$0, This is a bash shell Positional parameters in the script , Used to indicate the command itself entered into the command line .
The rest are $1,$2 wait , Represents the first parameter and the second parameter after the command entered into the command line respectively
bash test.sh 10 9
Among them $0 Namely test.sh,10 and 9 Namely $1 and $2.
pwd, This command prints the current absolute path .
Why not pwd?
Please pay attention to the basic facts
call shell Script , It is under the current directory of the calling script , Execute every command in the script line by line .
Modify the above script as follows
#/bin/bash
rootDir=$(cd $(dirname $0); pwd)
echo "rootDir $rootDir"
echo "pwd $pwd"
And in ~/lyq Enter the command code/test.sh To run this script , The output is as follows
[email protected]:~/lyq$ code/test.sh
rootDir /home/usr/lyq/code
pwd /home/usr/lyq
The first 2 individual pwd Not the directory where the script is located , It's the directory where we type the command . As that basic fact shows , Running the script in this directory is equivalent to typing in each sentence of the script line by line and executing it , So in ~/lyq Call... In the script pwd, It is equivalent to typing in the directory pwd, So the result is the current path .
The reason why it can't be used directly pwd Get the directory where the script is located , Because if the script is called outside the script Directory , It returns the directory where the command is called, not the directory where the script is located .
3. expand
#/bin/bash
topDir = $(cd $(dirname $0); cd ../; pwd) # Get the current path , Switching path , And print
echo topDir
$(topDir)/Build/build_app.bat
边栏推荐
- Fix the problem that uicollectionview does not reach the bottom security zone
- 【K-Means】K-Means学习实例
- 校园失物招领小程序源码可作毕业设计
- NFT products are alive
- Gamefi: everything you need to know about the "play and earn" game economy
- Campus lost and found applet source code can be used for graduation design
- Practical process of selecting DC-DC switching power supply controller
- 数字藏品app系统源码
- Xilinx pin constraint file xdc
- Data consistency
猜你喜欢
使用 Feign 实现声明式 REST 调用

Tiktok encounters cultural conflict in the UK, and many employees leave in a short time

为什么DDRx的电源设计时需要VTT电源

Leetcode 1995. 统计特殊四元组(暴力枚举)

International multilingual sea mall rebate product automatic matching order source code

基于C语言实现比赛评分系统

Probability theory: calculating confidence intervals

Leetcode 1961. Check whether the string is an array prefix

Cloud image quality assistant IAPP source code

详解2.5G/5G/10G Base-T以太网接口物理层一致性测试!
随机推荐
Arbitrum 基础架构:快速入门
Pyramidtnt: TNT with characteristic pyramid structure
Safety related website recommendations
Ngui, backpack drag and drop, and random cloning of picture knowledge points
Cadence OrCAD capture design method to avoid misoperation graphic tutorial
Tiktok encounters cultural conflict in the UK, and many employees leave in a short time
详解2.5G/5G/10G Base-T以太网接口物理层一致性测试!
Ngui, floating blood
Browser class for getting started with puppeter
Use of kingbasees UDP monitoring tool for gold warehouse database
NGUI,地图放大缩小
[Objective-C] dynamically create controls
金仓数据库KingbaseES中的sys_checksums坏块检测功能
Ngui, cooling effect
Unity字体间距
Some code fragments of a universal and confession wall system developed by PHP
C#入门系列(十一) -- 多维数组
国际多语言出海商城返佣产品自动匹配订单源码
Install MySQL version 5.7 or above on windows (install in compressed package)
Development and source code construction of digital collection system