|
zonghuaJul 17, 2016
1
echo $str > yours.txt
|
|
2
@zonghua fp =popen("echo $input >/proc/echo_config","w"); 这样写有问题吗?
|
|
fugeJul 17, 2016
3
@zhongshaohua python ? Popen? or open? ,,,,,,,,,,,,,,,,
|
|
4
@fuge c 语言
|
|
5
#include <stdio.h>
#include <unistd.h> #include <sys/types.h> int main(){ pid_t childpid; if ((childpid=fork()) == 0){ char * execvp_str[] = {"echo", "executed by execvp",">", "~/abc",NULL}; if (execvp("echo",execvp_str) <0 ) perror("error on exec"); return 0; }else{ //parent process wait(&childpid); printf("execvp done\n\n"); } } 这样为什么也写不进?? |
|
changshuJul 17, 2016
6
2L 的问题在于 /proc 下面的是 proc fs, 不存在的 proc 文件即便 root 通过 IO 写入也创建不了.
5L 的问题在于 exec 系列函数不执行 shell 解释器, >和~这种 shell 语法认不得, 全当字符串输出了. |
|
7
@changshu 2L 方法如果在内核先创建了文件 这样是不是就应该能写入? 5L 这种方法正确的应该怎么写? 求老司机发车
|
|
8bitJul 17, 2016
8
试试管道
|
|
9
@8bit 2L 中的方法管道可以 但是内容在数组 str 里面 不知道怎么 echo 过去
|
|
chzyerJul 17, 2016
10
bash -c "echo $input >/proc/echo_config", 通过调用 bash 去解析 bash 脚本,接下来你想 popen 还是 exec 就随意了
|
|
11
@chzyer 还是没理解
|
|
12
@chzyer fp = popen("bash -c 'echo $input > ~/abc'","w"); 是这样吗?这样也还是写不进去啊
|
|
8bitJul 17, 2016 via Android
13
突然想起来,直接在 shell 中调用你的程序,参数可以通过 main 的入口传入
|
|
chzyerJul 17, 2016
14
@zhongshaohua 你要先确保 $input 不是空啊在这个环境里面。我不知道你这个 $input 是怎么获得的。
|
|
neoblackcapJul 18, 2016
15
为什么要这么做?记得是有系统调用获取环境变量的啊,直接写文件不就好了吗?
|
|
msg7086Jul 18, 2016
16
1. 你没说清楚你需要用 C 语言来做。
2. 你说的是用 Shell 写入,但是用 C 语言写入的话不需要过 Shell 。 3. 你应该先学习下什么叫变量,什么叫字符串,如何拼接字符串。 |
|
ncisoftJul 18, 2016 via Android
17
友情提示: fprintf fopen
|
|
zhaoxiting1997Jul 18, 2016 via Android
18
在 c 语言下有多种输出文件的方法,用 shell 命令不是一种好的选择:既不能在 windows 或者其他系统下用,如果输出是用户可编辑的又存在着构造特定字符串执行任意代码的风险。
|
|
changshuJul 18, 2016
19
@zhongshaohua 有 proc 文件的话没问题, 直接 2L 那方法就行, 没必要改 5L 了, 就你现在这需求折腾 5L 也就是实现一个简易版的 system 或者 popen
|
|
jixiangqdJul 18, 2016
20
为何要这么搞?直接开文件写不行?
|
|
21
@jixiangqd 最后是 open write
|