我有一個文件裏面這個shell腳本調用restart_server.sh:與shell腳本奇怪的問題文件
echo "KILLING SERVER SESSION"
kill $(cat serverlastpid.txt)
sleep 20
echo "STARTING SERVER..."
java -jar eu.greensmartcampus-0.0.1-SNAPSHOT-jar-with-dependencies.jar </dev/null>/dev/null 2>&1 &
echo $! > serverlastpid.txt
echo "SERVER STARTED! PID:" $(cat serverlastpid.txt)
如果我單獨執行的每一行,我得到這樣的:
[email protected]:/opt/AppServer# echo "KILLING SERVER SESSION"
KILLING SERVER SESSION
[email protected]:/opt/AppServer# kill $(cat serverlastpid.txt)
[email protected]:/opt/AppServer# sleep 20
[email protected]:/opt/AppServer# echo "STARTING SERVER..."
STARTING SERVER...
[email protected]:/opt/AppServer# java -jar eu.greensmartcampus-0.0.1-SNAPSHOT-jar-with-dependencies.jar </dev/null>/dev/null 2>&1 &
[1] 14620
[email protected]:/opt/AppServer# echo $! > serverlastpid.txt
[email protected]:/opt/AppServer# echo "SERVER STARTED! PID:" $(cat serverlastpid.txt)
SERVER STARTED! PID: 14620
[email protected]:/opt/AppServer#
現在,如果我只是在shell腳本文件中運行所有這些行,我會得到這個輸出:
[email protected]:/opt/AppServer# sh restart_server.sh
restart_server.sh: 1: restart_server.sh: echo: not found
STARTING SERVER...
SERVER STARTED! PID: 14777
[1]+ Exit 143 java -jar eu.greensmartcampus-0.0.1-SNAPSHOT-jar-with-dependencies.jar </dev/null> /dev/null 2>&1
[email protected]:/opt/AppServer#
部分回聲消息丟失,屏幕上出現一些錯誤。
爲什麼會發生這種情況?與單獨運行每個行或運行在.sh文件中有什麼不同?
編輯:添加#/斌/ bash作爲第一行輸出:
[email protected]:/opt/AppServer# sh restart_server.sh
restart_server.sh: 1: restart_server.sh: #!/bin/bash: not found
KILLING SERVER SESSION
STARTING SERVER...
SERVER STARTED! PID: 15366
[email protected]:/opt/AppServer#
腳本的第一行是什麼?你在用什麼外殼? – tink 2015-02-07 18:05:58
bash我想。第一行是* echo「KILLING SERVER SESSION」* – PedroD 2015-02-07 18:07:37
考慮製作第一行**#!/ bin/bash ** ... – tink 2015-02-07 18:09:34