我有一個draft.sh具有以下格式的腳本:
#!/bin/bash
set -ex
some_var=''
while getopts 'k' flag; do
case "${flag}" in
k) some_var='true' ;;
*) ;;
esac
done
if [ -n $some_var ]; then
exec sh -c "some_command"
fi
( echo "some other command" )| tee output.txt
我注意到如果我運行./draft.sh -k,它會運行some_command,并且不會繼續echo "some other command" ) | tee output.txt 。我想知道exec sh -c在這種情況下是什么意思。
uj5u.com熱心網友回復:
這意味著將您正在運行 shell 腳本的當前行程更改為執行新 shell 的行程,sh并且這個新 shell 將執行“some_command”。隨著流程的變化,您正在運行的 shell 腳本將不會繼續。
在 bash 中, exec 是一個內置函式。檢查man builtins:
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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/393517.html
上一篇:如何在檔案夾中沒有特定擴展名的情況下搜索bash腳本檔案?
下一篇:成對比較檔案夾的所有檔案
