shell: 10、獲取實際路徑

shell: 10、獲取實際路徑

# 在某些情況下會拿到錯誤結果
work_path=$(dirname $0)
work_path=$(pwd)

## 正確實現
# 通過 readlink 獲取絕對路徑,再取出目錄
work_path=$(dirname $(readlink -f $0))

# 或者曲線救國
work_path=$(dirname $0)
cd ./${work_path}  # 當前位置跳到腳本位置
work_path=$(pwd)   # 取到腳本目錄 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章