问题描述
我尝试了命令
cat tmp/file{1..3} >新建文件
并且完美运行
但是当我编译并执行以下c程序时
But when i compile and execute the following c program
#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
出现
好像系统没有做大括号扩展
It seems like system does not make brace expansion
推荐答案
问题是system()
调用的shell,不是Bash,而是另一个不支持大括号扩展的shell.
The problem is the shell invoked by system()
, it is not Bash, but another shell which does not support brace expansion.
您仍然可以使用选项 -c
调用 bash
以便将 bash
与 system()
一起使用.例如:
You can still call bash
with the option -c
in order to use bash
with system()
. For example:
system("bash -c 'echo The shell is: $SHELL'")
bash
本身将在另一个 shell 之上运行(即:shell system()
调用),但 echo
命令将肯定在 Bash 中运行.
bash
itself will run on top of the other shell (i.e.: the shell system()
invokes), but the echo
command will definitely run in Bash.
通过在您的代码中应用相同的原则:
By applying the same principle in your code:
sprintf(command,"bash -c 'cat tmp/file{1..%d} > file.Ver'",num_of_points);
将创建您需要传递给 system()
的正确 command
字符串,以便命令 cat tmp/file{1..%d}>file.Ver
在 Bash 中运行并进行大括号扩展.
will create the proper command
string you need to pass to system()
, so that the command cat tmp/file{1..%d} > file.Ver
is run in Bash and brace expansion is performed.
这篇关于带有系统函数的c程序中的大括号扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!