Get exit code for command in bash/ksh

前端 未结 5 988
死守一世寂寞
死守一世寂寞 2021-02-01 01:08

I want to write code like this:

command=\"some command\"

safeRunCommand $command

safeRunCommand() {
   cmnd=$1

   $($cmnd)

   if [ $? != 0 ]         


        
5条回答
  •  情深已故
    2021-02-01 01:32

    It should be $cmd instead of $($cmd). Works fine with that on my box.

    Edit: 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.

    Also, when you have to debug your bash scripts, execute with '-x' flag. [bash -x s.sh].

提交回复
热议问题