开发者

C - System(""); Execute one at a time

I've got a system("sudo apt-get update | sudo apt-get -y install apache2 zip unzip"); etc, but its doing all the same commands at once?, how do i make it so it does one after the other is finished?, also some m开发者_如何学Cay ask the user to enter information from apt-get, how do i allow this to show?


That pipe character (|) means that the output from sudo apt-get update is being piped into the input of sudo apt-get -y install apache2 zip unzip. Although this doesn't actually make any sense, it does mean both get launched at the same time, which is not what you want.

Either replace the single call with two individual system() calls:

system("sudo apt-get update");
system("sudo apt-get -y install apache2 zip unzip");

Note that when you call system(), your program doesn't resume until the process you launched has exited, so this means the first call will execute, then the second.

Or replace the pipe with && (isn't necessarily guaranteed to work, though it really should on any Linux system):

system("sudo apt-get update && sudo apt-get -y install apache2 zip unzip");

Which means the right-hand side of the command will execute only if the left-hand side exits without error (technically, has an exit status of 0).

You can also replace the pipe with a semicolon (;) instead, which should execute both commands in sequence regardless of the exit status of the first command.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜