English 中文(简体)
在Bash和KornShell(克什)为指挥制定撤离法
原标题:Get the exit code for a command in Bash and KornShell (ksh)

我想写这样的法典:

command="some command"

safeRunCommand $command

safeRunCommand() {
   cmnd=$1

   $($cmnd)

   if [ $? != 0 ]; then
      printf "Error when executing command:  $command "
      exit $ERROR_CODE
   fi
}

但是,这部法律并没有实现我的愿望。 我在哪里犯错误?

最佳回答

固定代码如下:

#!/bin/ksh
safeRunCommand() {
  typeset cmnd="$*"
  typeset ret_code

  echo cmnd=$cmnd
  eval $cmnd
  ret_code=$?
  if [ $ret_code != 0 ]; then
    printf "Error: [%d] when executing command:  $cmnd " $ret_code
    exit $ret_code
  fi
}

command="ls -l | grep p"
safeRunCommand "$command"

现在,如果你看一下这一法典,我改变的几件事情是:

  • use of typeset is not necessary, but it is a good practice. It makes cmnd and ret_code local to safeRunCommand
  • use of ret_code is not necessary, but it is a good practice to store the return code in some variable (and store it ASAP), so that you can use it later like I did in printf "Error: [%d] when executing command: $command " $ret_code
  • pass the command with quotes surrounding the command like safeRunCommand "$command". If you don’t then cmnd will get only the value ls and not ls -l. And it is even more important if your command contains pipes.
  • you can use typeset cmnd="$*" instead of typeset cmnd="$1" if you want to keep the spaces. You can try with both depending upon how complex is your command argument.
  • eval is used to evaluate so that a command containing pipes can work fine

Note: Do remember some commands give 1 as the return code even though there isn t any error like grep. If grep found something it will return 0, else 1.

我用、KornShell Bash 。 该公司还从事罚款。 让我知道,你是否面临这一问题。

问题回答

提 出

safeRunCommand() {
   "$@"

   if [ $? != 0 ]; then
      printf "Error when executing command:  $1 "
      exit $ERROR_CODE
   fi
}

应为<代码>$cmd,而不是$($cmd)。 在我的盒子上,它做了一些细微的工作。

Your script works only for one-word commands, like ls. It will not work for "ls cpp". For this to work, replace cmd="$1"; $cmd with "$@". And, do not run your script as command="some cmd"; safeRun command. Run it as safeRun some cmd.

此外,当你必须打折你的印章时,用——x国旗执行。 [bash -x s.sh]。

你的文字有几个错误。

职能(附属机构)应在试图指定之前宣布。 你可能希望(......)返回,但不要离开(......)你,让呼吁的团体检验某一指挥的成功或失败。 除此以外,你没有抓住ERROR_ 因此,CODE一直为零(未界定)。

打破你可变的参考资料,同时用粗略的镜子,也是好的做法。 您的法典不妨考虑:

#!/bin/sh
command="/bin/date -u"          #...Example Only

safeRunCommand() {
   cmnd="$@"                    #...insure whitespace passed and preserved
   $cmnd
   ERROR_CODE=$?                #...so we have it for the command we want
   if [ ${ERROR_CODE} != 0 ]; then
      printf "Error when executing command:  ${command} 
"
      exit ${ERROR_CODE}        #...consider  return()  here
   fi
}

safeRunCommand $command
command="cp"
safeRunCommand $command




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签