更新时间:2023-11-21 20:57:58
这取决于被调用来执行命令的 shell,但通常大多数 shell 使用 ;
来分隔命令,所以像这样应该工作:
That depends on the shell being invoked to execute the commands, but in general most shells use ;
to separate commands so something like this should work:
command1; command2; command3
正如@dicroce 提到的,您可以使用 &&
而不是 ;
这将在返回非零值的第一个命令处停止执行.这可能需要也可能不需要(并且某些命令可能会在成功时返回非零值)但是如果您尝试处理可能失败的命令,您可能不应该在 system() 调用中将多个命令串在一起,因为您没有任何确定故障发生位置的方法.在这种情况下,您***的选择是一次执行一个命令,或者创建一个 shell 脚本来执行适当的错误处理并调用它.
As @dicroce mentioned, you can use &&
instead of ;
which will stop execution at the first command that returns a non-zero value. This may or may not be desired (and some commands may return non-zero on success) but if you are trying to handle commands that can fail you should probably not string multiple commands together in a system() call as you don't have any way of determining where the failure occured. In this case your best bet would either be to execute one command at a time or create a shell script that performs the appropriate error handling and call that instead.