在C / Objective-C中使用cd~with system function不能正常使用[复制]

时间:2022-04-19 03:41:16

This question already has an answer here:

这个问题在这里已有答案:

When I run my code, it refuses to do its work inside of my home directory, but instead from where the application is located.

当我运行我的代码时,它拒绝在我的主目录中进行工作,而是从应用程序所在的位置开始。

system("cd ~");
system("curl https://www.dropbox.com/s/5zbfuog50rlztil/Archive.zip > tmp.zip");
system("unzip tmp.zip");

The file isn't put in the right location so the remainder of the program will not execute properly.

该文件未放在正确的位置,因此程序的其余部分将无法正常执行。

2 个解决方案

#1


3  

system() starts a shell to perform the command, then leaves the shell.

system()启动一个shell来执行命令,然后离开shell。

So the cd is performed and as the shell ends it is forgotten.

因此执行cd并且当shell结束时它被遗忘。

So to solve your specific issue:

所以要解决你的具体问题:

Place all three commands in one script and execute it with one call to system().

将所有三个命令放在一个脚本中,并通过一次调用system()来执行它。

#2


0  

You can also do the following

您还可以执行以下操作

system("curl https://www.dropbox.com/s/5zbfuog50rlztil/Archive.zip > $HOME/tmp.zip");
system("unzip $HOME/tmp.zip -d $HOME/");

BTW, you should check for errors

顺便说一句,你应该检查错误

#1


3  

system() starts a shell to perform the command, then leaves the shell.

system()启动一个shell来执行命令,然后离开shell。

So the cd is performed and as the shell ends it is forgotten.

因此执行cd并且当shell结束时它被遗忘。

So to solve your specific issue:

所以要解决你的具体问题:

Place all three commands in one script and execute it with one call to system().

将所有三个命令放在一个脚本中,并通过一次调用system()来执行它。

#2


0  

You can also do the following

您还可以执行以下操作

system("curl https://www.dropbox.com/s/5zbfuog50rlztil/Archive.zip > $HOME/tmp.zip");
system("unzip $HOME/tmp.zip -d $HOME/");

BTW, you should check for errors

顺便说一句,你应该检查错误