OS X Bash脚本:语法错误:意外的文件结尾
我试着学习如何编写bash脚本,以拥有自己的脚本来启动和停止Tomcat服务器,但我似乎找不到该脚本中出现错误的原因。我已经仔细检查了我的OS X Bash脚本:语法错误:意外的文件结尾,bash,Bash,我试着学习如何编写bash脚本,以拥有自己的脚本来启动和停止Tomcat服务器,但我似乎找不到该脚本中出现错误的原因。我已经仔细检查了我的if和fi语句,以确保它们匹配,但仍然不知道出了什么问题 编辑: 下面是确切的错误消息 第87行:语法错误:意外的文件结尾 以下是脚本: #!/bin/bash #Returns the process id of the Tomcat server if currently running function tomcat_pid { loc
if
和fi
语句,以确保它们匹配,但仍然不知道出了什么问题
编辑:
下面是确切的错误消息
第87行:语法错误:意外的文件结尾
以下是脚本:
#!/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
请参见
manbash
,换句话说bash(1)
,SHELL GAMMAR一节
{ list; }
list is simply executed in the current shell environment. list must be terminated with a newline or semi‐
colon. This is known as a group command. The return status is the exit status of list. Note that unlike
the metacharacters ( and ), { and } are reserved words and must occur where a reserved word is permitted to
be recognized. Since they do not cause a word break, they must be separated from list by whitespace or
another shell metacharacter.
最后一句话指出了你的问题。你需要在
}
和#
之间留出一个空格来添加注释。你已经得到了相关的答案(作为注释)。观察:“请使用正确的选项”在您6个月后使用该命令时毫无帮助,并且记不起正确的选项是什么。您应该报告一份正确使用的摘要,以点动用户的内存。例如,echo“用法:$0{restart | start | shutdown | status}”>&2;退出1
报告标准错误的使用情况,并确保脚本报告错误状态(退出0
表示成功)。启动和停止服务的脚本对于初学者来说不是一个好的练习,当你不是初学者的时候,它们是毫无意义的。@tripleee为什么它们对非初学者毫无意义?@123也许我被修辞迷住了。让这句话有点细微差别:服务脚本是一个已解决的问题;一个有经验的开发人员不会从零开始重新设计它们。这只不过是对pidof
的(糟糕的)重新实现,尽管部分逻辑可能是有用的,甚至可能是必要的;虽然大多数现代服务都已经提供了一个简单的启动/停止脚本(如果需要)。不,最后一句解释了为什么在结束}
之前有一个空格。基本上是的,但是shell脚本是一个大的列表代码>在其主级别。