2013-03-14 101 views
6

我正在尝试为startstop服务的脚本创建模板。 我正在检查tomcat启动和停止模板,并看到命令RETVAL=$?RETVAL是什么意思?

这是干什么的?我应该保留它吗? 顺便说一下,我的脚本它的下面,以防万一你们想看到它。

#!/bin/bash 
#=================================================================================== 
# 
# 
FILE: <script-file-name>.sh 
# 
# 
USAGE: <script-file-name>.sh [-d] [-l] [-oD logfile] [-h] [starting directories] 
# 
# DESCRIPTION: List and/or delete all stale links in directory trees. 
# 
The default starting directory is the current directory. 
# 
Don’t descend directories on other filesystems. 
# 
# 
OPTIONS: see function ’usage’ below 
# REQUIREMENTS: --- 
# 
BUGS: --- 
# 
NOTES: --- 
# 
AUTHOR: Valter Henrique, [email protected]<company>.com 
# 
COMPANY: <company> 
# 
VERSION: 1.0 
# 
CREATED: 03.14.13 
# 
REVISION: 03.14.13 
#=================================================================================== 
# 
# chkconfig: 345 90 12 
# description: <service-name> start, stop, restart service 
# 
# Get function from functions library 
. /etc/init.d/functions 

folder=/<company>/<service-folder> #folder to the application 
service="<service-name>" #name of the service 

startup=$folder/start.sh 
shutdown=$folder/stop.sh 


#=== FUNCTION ================================================================ 
# 
NAME: start 
# DESCRIPTION: Start the service <service-name> 
# PARAMETER 1: --- 
#=============================================================================== 
start() { 

    #---------------------------------------------------------------------- 
    # logging the start 
    #---------------------------------------------------------------------- 
    initlog -c "echo -n Starting $service:" 

    #---------------------------------------------------------------------- 
    # getting the process PID 
    #----------------------------------------------------------------------  
    pid_process=`ps -ef | grep "$folder/<jar>.jar" | grep -v grep |awk -F' ' '{ print $2 }'`; 

    if [ $pid_process ]; then 
    echo "<service-name> is running!" 
    echo "Stop then first!" 
    else 
    action $"Starting <service-name> service: " su - <user_deployer> -c $startup 
    RETVAL=$? 
    fi 
    #---------------------------------------------------------------------- 
    # create the lock file 
    #---------------------------------------------------------------------- 
    touch /var/lock/subsys/$service 
    success $"Sucess $service startup" 
    echo 
} 

#=== FUNCTION ================================================================ 
# 
NAME: stop 
# DESCRIPTION: Stop the service <service-name> 
# PARAMETER 1: --- 
#=============================================================================== 
stop() { 
    #---------------------------------------------------------------------- 
    # logging the stop 
    #---------------------------------------------------------------------- 
    initlog -c "echo -n Stopping $service: " 
    killproc $service 
    #---------------------------------------------------------------------- 
    # now, delete the lock file 
    #---------------------------------------------------------------------- 
    rm -f /var/lock/subsys/$service 
    echo 
} 

#---------------------------------------------------------------------- 
# Main Logic 
#---------------------------------------------------------------------- 
case "$1" in 
    start) 
     start 
     ;; 
    stop) 
     stop 
     ;; 
    status) 
     status $service 
     ;; 
    restart|reload|condrestart) 
     stop 
     start 
     ;; 
    *) 
     echo $"Usage: $0 {start|stop|restart|reload|status}" 
     exit 1 
esac 
exit 0 
+1

由于没有其他人提到过,RETVAL是RETURN VALUE的缩写。了解* nix关键词的词源可以帮助我记住它们。 – NobleUplift 2015-07-14 17:19:47

回答

19

$?给出了最后执行的命令的状态。在你的情况下,最后执行的命令是action....。该命令的退出状态将出现在$?中,稍后在RETVAL变量中捕获该命令。 如果命令成功,$?将包含0,否则为非零值。

+2

需要强调的一点是:在每条命令后面会覆盖'$?',所以如果您想记住特定命令的状态,则需要在命令完成后立即将其值分配给另一个变量(如'RETVAL')。 – chepner 2013-03-14 14:34:54

3

RETVAL是一个变量。 $?将最后执行的命令的状态分配给RETVAL变量。

2

正如其他人所说,$?是最后一个命令的退出状态。

现在,关于你的问题......

RETVAL,不使用其他任何地方的脚本,但要记住,在bash,常规变量是全局,使他们可以通过其他功能一起使用。正如你所看到的,有一个可能会使用它的success调用。在分配我检查/etc/init.d/functions不使用这个变量,所以行只是噪音,可以删除..检查你的分配,看看它是什么。