1、help信息
Develop>dirname --help
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 -> "."
GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report dirname translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'dirname invocation'
2、應用場景
1)獲取當前shell程序所在路徑
在命令行狀態下單純執行
dirname $0是毫無意義的。因為他返回當前路徑的"."
在/root下新建xxx.sh腳本,在另外一個目錄/usr/bin來執行這個文件,顯示結果如下:
Develop>cat xxx.sh
#!/bin/bash
DIR=`dirname $0`
echo $DIR
Develop>/root/xxx.sh
/root
Develop>pwd
/usr/bin
Develop>/root/xxx.sh
/root
Develop>
2)進入當前shell代碼所在目錄
Develop>cat /root/xxx.sh
#!/bin/bash
DIR=`dirname $0`
cd $DIR
echo `pwd`
Develop>pwd
/usr/bin
Develop>/root/xxx.sh
/root
Develop>pwd
/usr/bin
Develop>
看上面的實操可以看出,執行腳本之后,已經從之前的/usr/bin 進入/root 了。當然也僅限於在shellcode執行時進入對應目錄,結束運行后實際的代碼目錄並未改變。
上面跳轉代碼也可以直接寫:
cd `dirname $0`
- $0:當前Shell程序的文件名
在可移植方面來說,這種寫法不用考慮腳本實際的目錄所在,比較方便
3、引申
- 【`】,學名叫“倒引號”, 如果被“倒引號”括起來, 表示里面需要執行的是命令。
- 【“”】 , 被雙引號括起來的內容, 里面 出現 $ (美元號: 表示取變量名) `(倒引號: 表示執行命令) \(轉義號: 表示轉義), 其余的才表示字符串。
- 【’‘】, 被單引號括起來的內容, 里面所有的都表示串, 包括上面所說的三個特殊字符。
