我尝试了命令
cat tmp/file{1..3} > newFile
并且工作完美
但是当我编译并执行以下c程序时
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main() {
char command[40];
int num_of_points = 3;
sprintf(command,"cat tmp/file{1..%d} > file.Ver",num_of_points);
system(command);
}
消息
cat: tmp/file{1..3}: No such file or directory
appears
系统似乎没有进行大括号扩展
系统似乎没有进行大括号扩展
问题是调用的 shellsystem()
,它不是 Bash,而是另一个不支持大括号扩展的 shell。
您仍然可以致电bash
与选项-c
为了使用bash
with system()
。例如:
system("bash -c 'echo The shell is: $SHELL'")
bash
本身将在另一个 shell 之上运行(即:shellsystem()
调用),但是echo
命令肯定会在 Bash 中运行。
通过在代码中应用相同的原则:
sprintf(command,"bash -c 'cat tmp/file{1..%d} > file.Ver'",num_of_points);
将创建适当的command
您需要传递给的字符串system()
,这样命令cat tmp/file{1..%d} > file.Ver
在 Bash 中运行并执行大括号扩展。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)