shell dirname的使用

1、用途说明

dirname命令可以取给定路径的目录部分,如果给定的参数本身为一个目录,那就取当前目前的上一层目录。这个命令很少直接在shell命令行中使用,一般把它用在shell脚本中,用于取得脚本文件所在目录,然后将当前目录切换过去。

 

Usage: dirname NAME

  or:  dirname OPTION

Print NAME with its trailing /component removed; if NAME contains no /'s,

output `.' (meaning the current directory).

      --help     display this help and exit

      --version  output version information and exit

Examples:

  dirname /usr/bin/sort  Output "/usr/bin".

  dirname stdio.h        Output ".".

 例子:

$dirname /usr/bin/sort/

 /usr/bin

2、在脚本中使用方式 

当无法确定当前所在目录时,可以使用以下方式:

#!/bin/sh

 DIR=$(cd $(dirname $0) && pwd)

表示的意思是将当前目录赋给DIR,并显示出当前路径

猜你喜欢

转载自daizj.iteye.com/blog/2255783