Sunday, June 7, 2009

Shell script running problem on Ubuntu

當在 Ubuntu 欲執行 shell script (使用 /bin/sh), 卻發生執行錯誤(原本在其他 Linux distribution 可正常執行). 例如 shell script 中使用 for loop, 但是有以下的錯誤訊息:

Syntax error: Bad for loop variable

出錯原因是因為 Ubuntu 的 /bin/sh 設定.

因為一般 distribution 的 /bin/sh 是使用 /bin/bash, 可以用指令 ls -l 得知 /bin/sh 是用 symbolic link 指向 /bin/bash (亦即 /bin/sh -> bash).

但是在 Ubuntu, /bin/sh 卻是使用 /bin/dash (亦即 /bin/sh -> dash).

解決方式是將原來的 symbolic link 刪除, 將 /bin/sh 改為使用 bash, 用 root 身份執行以下指令:
cd /bin
rm -f sh
ln -s bash sh

參考資料:
fix #!/bin/sh in ubuntu, http://liquidweather.net/howto/index.php?id=59

No comments:

Post a Comment