使用场景
为了方便脚本的移植,在脚本可以使用
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 -> "."
脚本示例
- 新建脚本
vi /home/test/test.sh
- 脚本内容
cd `dirname $0`
echo `pwd`
- 保存脚本并退出,运行脚本,结果如下:
/home/test
关键知识点
`command`
在脚本中代表要执行命令;- 而
'command'
则是将command所代表的内容设置为字符串; - 而
"command"
里面的$param
代表变量,`command`
代表执行命令,\
代表转义字符。 $0
在脚本代表当前脚本的文件名