本文介绍了在bash脚本中的`exec`之后没有代码将运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在命令名称中使用变量扩展进行测试的bash脚本示例:

Sample bash script where I'm testing using variable expansion in command names:

test_command_w_variable_expansion_in_name.sh :

#!/bin/bash

# Gabriel Staples
# 21 Mar. 2020

echo "PATH = \"$PATH\""
# PATH="$HOME/bin:$PATH"
# echo "PATH = \"$PATH\""

# 1st, create a command in ~/bin to test here
mkdir -p ~/bin
echo -e "#!/bin/bash\necho \"This is a test script found in ~/bin.\"" > ~/bin/gs_test_script
chmod +x ~/bin/gs_test_script

# 1)
# command: `echo`
CMD_PREFIX="ec"

${CMD_PREFIX}ho "hey" # works
# exec "${CMD_PREFIX}ho" "hey" # works, but then prevents the code below from running!
# eval "${CMD_PREFIX}ho" "hey" # does NOT work, but also throws no error

# 2)
# command: `gs_test_script` from ~/bin
CMD_PREFIX="gs_test"

~/bin/gs_test_script # works!
gs_test_script # works!
${CMD_PREFIX}_script # works!

输出:

$ ./test_command_w_variable_expansion_in_name.sh
PATH = "/home/gabriel/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
hey
This is a test script found in ~/bin.
This is a test script found in ~/bin.
This is a test script found in ~/bin.

问题:

  1. 现在,如果我取消注释此行:# exec "${CMD_PREFIX}ho" "hey" # works, but then prevents the code below from running!,它下面的代码将不再运行,而是得到此输出!请注意,我不再获得This is a test script found in ~/bin. 为什么?

  1. Now, if I uncomment this line: # exec "${CMD_PREFIX}ho" "hey" # works, but then prevents the code below from running!, the code below it no longer runs, and I get this output instead! Notice I no longer get the 3 printouts of This is a test script found in ~/bin. Why?

$ ./test_command_w_variable_expansion_in_name.sh
PATH = "/home/gabriel/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"
hey
hey

  • 此外,它下面的eval命令也不起作用.如果我取消注释该行,则会得到与上面刚刚发布的错误输出完全相同的错误输出,该输出仍然不会像应有的那样执行我对gs_test_script的调用.为什么?

  • Also, the eval command just below it doesn't work. If I uncomment out that line I get the exact same erroneous output as posted just above, which still doesn't execute my call to gs_test_script three times like it should. Why?

    推荐答案

    因为exec命令将用要执行的新命令替换当前的bash进程.它不会返回到调用过程.因此,您不应在脚本中使用exec.

    Because the exec command will replace the current bash process with the new command to be executed. It does not return to the calling process. So you should not use exec in your script.

      exec [-cl] [-a name] [command [arguments]]
    
      If command is specified, it replaces the shell.  No new  process
      is  created.  The arguments become the arguments to command.  If
      the -l option is supplied,  the  shell  places  a  dash  at  the
      beginning  of  the  zeroth  argument passed to command.  This is
      what login(1) does.  The -c option causes command to be executed
      with  an empty environment.  If -a is supplied, the shell passes
      name as the zeroth argument to the executed command.  If command
      cannot  be  executed  for  some  reason, a non-interactive shell
      exits, unless the execfail shell option  is  enabled.   In  that
      case,  it returns failure.  An interactive shell returns failure
      if the file cannot be executed.  If command  is  not  specified,
      any  redirections  take  effect  in  the  current shell, and the
      return status is 0.  If there is a redirection error, the return
      status is 1.
    

    这篇关于在bash脚本中的`exec`之后没有代码将运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

  • 09-05 07:06