解決呼叫shell指令碼中相對路徑的問題
阿新 • • 發佈:2018-12-29
依家我有1個軟體goagent目錄(大家懂得)
放在/home/gateman/Programs/ 下
1. proxy.py
入面有1個 proxy.py 檔案 放在/home/gateman/Programs/goagent/local/ 入面
2.breakwall.sh
我在 proxy.py 的上一級目錄 /home/gateman/Programs/goagent/
建立1個指令碼來啟動這個程式.
如果進入/home/gateman/Programs/goagent/ 目錄
執行 sh ./breakwall.sh
系無問題的。 正常啟動。
但系去到其他目錄, 例如上一級的 /home/gateman/Programs/
去執行 sh ./goagent/breakwall.sh
就會報錯
因為在/home/gateman/Programs/呢個目錄下執行腳步裡的
時,./local/proxy.py 被解析為 /home/gateman/Programs/local/proxy.py
而事實上的正確路徑應該是 /home/gateman/Programs/goagent/local/proxy.py
所以就會搵唔到呢個檔案。
解決方法1:
將breakwall.sh 的相對路徑改為絕對路徑:
./local/proxy.py ==> /home/gateman/Programs/local/proxy.py`
缺陷:
但系這樣改的話, 工程遷移會好麻煩, 例如我要將/home/gateman/Programs/goagent/ 這個 goagent軟體搬到別路徑 或者別的機器下時, 就要修改breakwall.sh檔案 修正入面的絕對路徑.
解決方法2:
在指令碼breakwall.sh 中加入獲取 絕對路徑的語句。
$0 就是第0個引數
例如 我在 /home/gateman/Programs/ 執行
sh goegent/breakwall.sh
其中
pwd 就返回 /home/gateman/Programs/
$0 返回 goegent/breakwall.sh
dirname $0返回 goegent/
所以$CURDIR 就= /home/gateman/Programs/goegent/
缺陷:
貌似執行成功,其實可以發現$0這個變數 是由執行命令的路徑決定的
這個指令碼只能用相對路徑來執行
如果我在/home/gateman/Programs/ 用絕對路徑來執行
sh /home/gateman/Programs/goegent/breakwall.sh
就會報錯了..
原因都好簡單, 因為此時$0 就變成/home/gateman/Programs/goegent/breakwall.sh
導致$CURDIR=/home/gateman/Programs/goegent/breakwall.sh
解決方法3:
在breakwall.sh 對$0 進行判斷, 對絕對路徑和相對路徑加入判斷。
這次無論在什麼路徑下 用
bash /home/gateman/Programs/goegent/breakwall.sh
或
bash 相對路徑/breakwall.sh
一定要用bash, sh解析不了這個語法。
大家有更好方法的話請指教
放在/home/gateman/Programs/ 下
1. proxy.py
入面有1個 proxy.py 檔案 放在/home/gateman/Programs/goagent/local/ 入面
2.breakwall.sh
我在 proxy.py 的上一級目錄 /home/gateman/Programs/goagent/
建立1個指令碼來啟動這個程式.
#!/bin/sh
python ./local/proxy.py
如果進入/home/gateman/Programs/goagent/ 目錄
執行 sh ./breakwall.sh
系無問題的。 正常啟動。
但系去到其他目錄, 例如上一級的 /home/gateman/Programs/
去執行 sh ./goagent/breakwall.sh
就會報錯
因為在/home/gateman/Programs/呢個目錄下執行腳步裡的
python
而事實上的正確路徑應該是 /home/gateman/Programs/goagent/local/proxy.py
所以就會搵唔到呢個檔案。
解決方法1:
將breakwall.sh 的相對路徑改為絕對路徑:
./local/proxy.py ==> /home/gateman/Programs/local/proxy.py`
#!/bin/sh那麼無論在哪個路徑呼叫這個指令碼,都能正常啟動程式。
python
缺陷:
但系這樣改的話, 工程遷移會好麻煩, 例如我要將/home/gateman/Programs/goagent/
解決方法2:
在指令碼breakwall.sh 中加入獲取 絕對路徑的語句。
#!/bin/sh
CURDIR="`pwd`"/"`dirname $0`"
echo $CURDIR
python $CURDIR/local/proxy.py
$0 就是第0個引數
例如 我在 /home/gateman/Programs/ 執行
sh goegent/breakwall.sh
其中
pwd 就返回 /home/gateman/Programs/
$0 返回 goegent/breakwall.sh
dirname $0返回 goegent/
所以$CURDIR 就= /home/gateman/Programs/goegent/
缺陷:
貌似執行成功,其實可以發現$0這個變數 是由執行命令的路徑決定的
這個指令碼只能用相對路徑來執行
如果我在/home/gateman/Programs/ 用絕對路徑來執行
sh /home/gateman/Programs/goegent/breakwall.sh
就會報錯了..
原因都好簡單, 因為此時$0 就變成/home/gateman/Programs/goegent/breakwall.sh
導致$CURDIR=/home/gateman/Programs/goegent/breakwall.sh
解決方法3:
在breakwall.sh 對$0 進行判斷, 對絕對路徑和相對路徑加入判斷。
#!/bin/bash
DIRNAME=$0
if [ "${DIRNAME:0:1}" = "/" ];then
CURDIR=`dirname $DIRNAME`
else
CURDIR="`pwd`"/"`dirname $DIRNAME`"
fi
echo $CURDIR
python $CURDIR/local/proxy.py
這次無論在什麼路徑下 用
bash /home/gateman/Programs/goegent/breakwall.sh
或
bash 相對路徑/breakwall.sh
一定要用bash, sh解析不了這個語法。
${DIRNAME:0:1}都可以正常啟動了。
大家有更好方法的話請指教