bash RETVAL 是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15411429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:26:37  来源:igfitidea点击:

What RETVAL means?

bashshell

提问by Valter Silva

I'm trying to create a template for scripts of startand stopservices. I was checking the template for tomcatstart and stop and see the command RETVAL=$?.

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

What this does ? Should I keep it ? By the way, my script it's below in case you guys wanna see it.

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

#!/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, valter.henrique@<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  }'`;

  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 "" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status $service
        ;;
  restart|reload|condrestart)
        stop
        start
        ;;
  *)
        echo $"Usage: ##代码## {start|stop|restart|reload|status}"
        exit 1
esac 
exit 0

回答by Guru

$?gives the status of the last command executed. In your case, the last command executed was action..... The exit status of this command will be present in $?which is later captured in the RETVALvariable. If the command was successful, $?will contain 0, else a non-zero value.

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

回答by rbedger

RETVAL is a variable. $? is assigning the status of the last executed command to the RETVAL variable.

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

回答by Karoly Horvath

As others stated, $?is the exit status of the last command.

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

Now, regarding your question...

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

RETVALis not used anywhere else in a script, but remember, in bash, regular variables are global so they can be used by other functions. As you can see, there's a successcall that might use it. In the distribution I checked/etc/init.d/functionsdoesn't use this variable, so the line is just noise and can be removed.. check yourdistribution to see what it does.

RETVAL不在脚本的其他任何地方使用,但请记住,在 bash 中,常规变量是全局的,因此它们可以被其他函数使用。如您所见,有一个success调用可能会使用它。在我检查的发行版/etc/init.d/functions中没有使用这个变量,所以这条线只是噪音,可以删除..检查你的发行版看看它做了什么。