1. 程式人生 > >shell指令碼-路徑處理$(cd `dirname $0`; pwd)

shell指令碼-路徑處理$(cd `dirname $0`; pwd)

#!/bin/bash

#得到當前輸入的引數
PARAMETER=${0}
echo ${PARAMETER}
#得到當前輸入引數的父級路徑
PARAMETER_PATH=`dirname $0`
echo ${PARAMETER_PATH}
#得到當前執行命令的路徑
CURRENT_PATH=`pwd`
echo ${CURRENT_PATH}
#得到當前執行命令+引數的父級路徑組合的指令碼的絕對路徑
ROOT_PATH=$(cd `dirname $0`; pwd)
echo ${ROOT_PATH}
#切換到指令碼的路徑下
cd ${ROOT_PATH}
#
#TODO
#
exit 0

例子:

資料路徑:
/Users/peeno/work/project/script/untitled.sh

終端執行:

cd /Users/peeno/work/project
sh script/untitled.sh

輸出結果:

script/untitled.sh
script
/Users/peeno/work/project
/Users/peeno/work/project/script