dirname命令学习

使用场景

为了方便脚本的移植,在脚本可以使用
dirname
命令来进行获取相对路径,这样脚本的可用性将会获得提高。

命令详解

Usage: dirname [OPTION] NAME...
Output each NAME with its last non-slash component and trailing slashes
removed; if NAME contains no /'s, output '.' (meaning the current directory).

  -z, --zero     separate output with NUL rather than newline
      --help     display this help and exit
      --version  output version information and exit

Examples:
  dirname /usr/bin/          -> "/usr"
  dirname dir1/str dir2/str  -> "dir1" followed by "dir2"
  dirname stdio.h            -> "."

脚本示例

  1. 新建脚本
vi /home/test/test.sh
  1. 脚本内容
cd `dirname $0`
echo `pwd`
  1. 保存脚本并退出,运行脚本,结果如下:
/home/test

关键知识点

  1. `command` 在脚本中代表要执行命令;
  2. 'command'则是将command所代表的内容设置为字符串;
  3. "command" 里面的$param代表变量,`command`代表执行命令,\代表转义字符。
  4. $0 在脚本代表当前脚本的文件名
发布了43 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/u013084266/article/details/79899745
今日推荐