私は自分自身のスクリプトを起動してTomcatサーバーを停止するためにbashスクリプトを書く方法を学びましたが、エラーの原因を見つけることができません。このスクリプトでは私は自分のif
とfi
のステートメントを二重にチェックして、それらが一致することを確認しましたが、何が間違っているかはまだ分かりません。 EDITOS X Bashスクリプト:構文エラー:予期しないファイルの末尾
:
#!/bin/bash
#Returns the process id of the Tomcat server if currently running
function tomcat_pid {
local pid=0
local temp=$(ps x | grep "$CATALINA_HOME" | grep -v grep | cut -d ' ' -f 1)
if [ -n "$temp" ]; then
pid=$temp
fi
echo "$pid"
}#tomcat_pid
#Checks the status of the Tomcat Server
function tomcat_status {
local retval="Tomcat Server is not currently running"
local pid
pid=$(tomcat_pid)
if [ "$pid" -gt 0 ]; then
retval="Tomcat Server is running at pid: $pid"
fi
echo "$retval"
}#tomcat_status
#Starts the Tomcat Server
function tomcat_start {
local pid
pid=$(tomcat_pid)
if [ "$pid" -gt 0 ]; then
echo "Tomcat Server already running at pid: $pid"
else
echo "Starting Tomcat Server"
"$CATALINA_HOME/bin/startup.sh"
fi
}#tomcat_start
#Stops the Tomcat Server
function tomcat_stop {
local pid
pid=$(tomcat_pid)
if [ "$pid" -gt 0 ]; then
echo "Shutting down Tomcat Server"
"$CATALINA_HOME/bin/shutdown.sh"
else
echo "Tomcat Server is not currently running"
fi
}#tomcat_stop
#Restarts the Tomcat Server
function tomcat_restart {
local pid
pid=$(tomcat_pid)
if [ "$pid" -gt 0 ]; then
echo "Restarting the Tomcat Server"
"$CATALINA_HOME/bin/shutdown.sh"
sleep 5s
"$CATALINA_HOME/bin/start.sh"
else
echo "Starting the Tomcat Server"
"$CATALINA_HOME/bin/startup.sh"
fi
}#tomcat_restart
if [ -n "$1" ]; then
if [ "$1" = 'restart' ]; then
tomcat_restart
#tomcat start - Starts the Tomcat Server
elif [ "$1" = 'start' ]; then
tomcat_start
#tomcat shutdown - Shuts down the Tomcat Server
elif [ "$1" = 'shutdown' ]; then
tomcat_stop
#tomcat status - Checks the status of the tomcat server
elif [ "$1" = 'status' ]; then
tomcat_status
else
echo "Please use correct options"
fi
else
echo "Please use correct options"
fi
コメントのために '}'と '#'の間にスペースが必要です。 – 123
あなたは関連する回答を(コメントとして)得ています。観察:「正しいオプションを使用してください」は、6ヵ月後にコマンドを使用し、正しいオプションが何であるかを覚えていないと大いに役に立たない。正しい使用法の概要を報告して、ユーザーのメモリをジョグする必要があります。たとえば、 'echo"使用法:$ 0 {restart | start | shutdown | status} ">&2; exit 1'は標準エラーの使用状況を報告し、スクリプトがエラー状態を報告することを保証する( 'exit 0'は成功を示す)。 –
サービスを開始したり停止したりするスクリプトは初心者にとっては本当に良いエクササイズではなく、初心者でないときには無意味です。 – tripleee