- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在学校作业上遇到了很多麻烦。我应该将管道和 I/O 重定向添加到 c 中的 shell 实现中。我已经让它与 I/O 重定向一起工作并且管道自己工作但我需要支持这样的东西“sort < file.txt | grep main | cat > output”。我不知道如何让它同时与两者一起工作。任何帮助将不胜感激。
int get_args(char* cmdline, char* args[]){
int i = 0;
/* if no args */
if((args[0] = strtok(cmdline, "\n\t ")) == NULL)
return 0;
while((args[++i] = strtok(NULL, "\n\t ")) != NULL) {
if(i >= MAX_ARGS) {
printf("Too many arguments!\n");
exit(1);
}
}
/* the last one is always NULL */
return i;
}
void pipehandler(char* args[], int nargs){
int num_cmds = 0;
char *command[256];
int filedes[2]; // pos. 0 output, pos. 1 input of the pipe
int filedes2[2];
int i = 0;
//calculate the number of commands
for(int i = 0; i < nargs; i++){
if (strcmp(args[i], "|") == 0){
num_cmds++;
}
}
num_cmds++;
if(num_cmds <= 1){
return;
}
int j = 0;
int end = 0;
pid_t pid;
while(args[j] != NULL && end != 1){
int k = 0;
while (strcmp(args[j],"|") != 0){
command[k] = args[j];
j++;
if (args[j] == NULL){
// 'end' variable used to keep the program from entering
// again in the loop when no more arguments are found
end = 1;
k++;
break;
}
k++;
}
// Last position of the command will be NULL to indicate that
// it is its end when we pass it to the exec function
command[k] = NULL;
j++;
// Depending on whether we are in an iteration or another, we
// will set different descriptors for the pipes inputs and
// output. This way, a pipe will be shared between each two
// iterations, enabling us to connect the inputs and outputs of
// the two different commands.
if (i % 2 != 0){
pipe(filedes); // for odd i
}else{
pipe(filedes2); // for even i
}
pid=fork();
if(pid==-1){
if (i != num_cmds - 1){
if (i % 2 != 0){
close(filedes[1]); // for odd i
}else{
close(filedes2[1]); // for even i
}
}
printf("Child process could not be created\n");
return;
}
if(pid==0){
// If we are in the first command
if (i == 0){
dup2(filedes2[1], STDOUT_FILENO);
}
// If we are in the last command, depending on whether it
// is placed in an odd or even position, we will replace
// the standard input for one pipe or another. The standard
// output will be untouched because we want to see the
// output in the terminal
else if (i == num_cmds - 1){
if (num_cmds % 2 != 0){ // for odd number of commands
dup2(filedes[0],STDIN_FILENO);
}else{ // for even number of commands
dup2(filedes2[0],STDIN_FILENO);
}
// If we are in a command that is in the middle, we will
// have to use two pipes, one for input and another for
// output. The position is also important in order to choose
// which file descriptor corresponds to each input/output
}else{ // for odd i
if (i % 2 != 0){
dup2(filedes2[0],STDIN_FILENO);
dup2(filedes[1],STDOUT_FILENO);
}else{ // for even i
dup2(filedes[0],STDIN_FILENO);
dup2(filedes2[1],STDOUT_FILENO);
}
}
execvp(command[0],command);
}
// CLOSING DESCRIPTORS ON PARENT
if (i == 0){
close(filedes2[1]);
}
else if (i == num_cmds - 1){
if (num_cmds % 2 != 0){
close(filedes[0]);
}else{
close(filedes2[0]);
}
}else{
if (i % 2 != 0){
close(filedes2[0]);
close(filedes[1]);
}else{
close(filedes[0]);
close(filedes2[1]);
}
}
waitpid(pid,NULL,0);
i++;
}
}
int searchIO(char* args[], int *nargs, char** input_file, char** output_file, int* in, int* out, int* ap){
int ioRedirection = -1;
//search through the array args
printf("searching for io redirections\n");
for(int i = 0; i < *nargs; i++){
if(strcmp(args[i], "<") == 0){
//if you find < then you need to redirect the stdin to be from a file
//if you are here then the input file is in args[i+1]
*input_file = args[i+1];
printf("this is the input file %s \n", *input_file);
//we need to remove the < and input_file from args[]
int j;
for(j = i; j < *nargs; j++){
//move what is in args[j+2] to args[j];
args[j] = args[j+2];
}
//then we need to execute the comand
*nargs = *nargs-2;
*in = 1;
i = 0;
ioRedirection = 1;
}else if (strcmp(args[i], ">") == 0){
//if you are here then the output file is in args[i+1]
//so aparently execute what is in args[0] to args[i]
*output_file = args[i+1];
//we need to remove the > and output_file from args[]
int j;
for(j = i; j < *nargs; j++){
//move what is in args[j+2] to args[j];
if(j < *nargs){
args[j] = args[j+2];
}
}
*nargs = *nargs-2;
*out = 1;
i = 0;
ioRedirection = 1;
}else if (strcmp(args[i], ">>") == 0){
//if you are here then the output file is in args[i+1]
//so aparently execute what is in args[0] to args[i]
*output_file = args[i+1];
//we need to remove the > and output_file from args[]
int j;
for(j = i; j < *nargs; j++){
//move what is in args[j+2] to args[j];
args[j] = args[j+2];
}
*nargs = *nargs-2;
*out = 1;
*ap = 1;
i = 0;
ioRedirection = 1;
}
}
return -1;
}
void execute(char* cmdline){
int pid, async;
char* args[MAX_ARGS];
char *input_file;
char *output_file;
int _in, _out, _ap;
_in = 0;
_out = 0;
_ap = 0;
int nargs = get_args(cmdline, args);
pipehandler(args, nargs);
searchIO(args, &nargs, &input_file, &output_file, &_in, &_out, &_ap);
if(nargs <= 0) return;
if(!strcmp(args[0], "quit") || !strcmp(args[0], "exit")) {
exit(0);
}
if(!strcmp(args[nargs-1], "&")) { async = 1; args[--nargs] = 0; }
else async = 0;
pid = fork();
if(pid == 0) { /* child process */
if(_in == 1){
int input = open(input_file, O_RDONLY);
dup2(input, STDIN_FILENO);
close(input);
}
if(_out == 1){
int output;
if(_ap){
output = open(output_file, O_APPEND | O_CREAT);
}else{
output = open(output_file, O_CREAT | O_WRONLY, 0666);
}
dup2(output, STDOUT_FILENO);
close(output);
}
execvp(args[0], args);
/* return only when exec fails */
perror("exec failed");
exit(-1);
} else if(pid > 0) { /* parent process */
if(!async) waitpid(pid, NULL, 0);
else printf("this is an async call\n");
} else { /* error occurred */
perror("fork failed");
exit(1);
}
}
int main (int argc, char* argv [])
{
char cmdline[BUFSIZ];
for(;;) {
printf("$ ");
if(fgets(cmdline, BUFSIZ, stdin) == NULL) {
perror("fgets failed");
exit(1);
}
execute(cmdline) ;
}
return 0;
}
最佳答案
if pipes are being used the only place to use I/O redirections is in the first command and in the last one but not in the middle right?
只有当 stdin/stdout 被重定向时,这才是正确的。
I/O redirections and the pipes work by themselves but … I have no idea how to make it work with both at the same time.
重组您的实现。如果你调用它就不能工作
pipehandler(args, nargs);
searchIO(args, &nargs, &input_file, &output_file, &_in, &_out, &_ap);
一个接一个,这也导致管道的第一个命令被执行两次的错误(首先来自 pipehandler
,然后来自 searchIO
,参数错误)。相反,您可以让 main
调用 pipehandler
,然后从 pipehandler
中执行
每个单独的命令(即使有只有一个,没有管道)。可以轻松诊断冲突的 I/O 重定向和管道。
关于c - 在 c 中使用管道和 I\O 重定向的 Shell 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40925272/
我相信我在子 shell 中调用 exit 会导致我的程序继续: #!/bin/bash grep str file | while read line do exit 0 done
我相信我在子 shell 中调用 exit 会导致我的程序继续: #!/bin/bash grep str file | while read line do exit 0 done
我有几个脚本,它们的第一部分看起来是一样的。这部分的功能是识别脚本在哪台机器上运行并相应地设置几个变量。它看起来像这样: ENV=`echo $LOGNAME | cut -c1-8` if
这是我正在尝试做的事情。我有 4 个 shell 脚本。脚本 1 需要先运行,然后是 2,然后是 3,然后是 4,并且它们必须按此顺序运行。脚本 1 需要运行(并在后台等待)2 才能正常运行,但是脚本
我有一个名为 a.sh 的脚本,其中的内容是: //a.sh: #!/bin/bash temp=0 while [ "$temp" -ne 500 ] do echo `date`
在snakemake中,使用shell()函数执行多个命令的推荐方式是什么? 最佳答案 您可以调用shell()多次内run规则块(规则可以指定 run: 而不是 shell: ): rule pro
我有一个 shell 脚本,我向其中传递了一些参数。Test1.sh -a 1 -b 2 -c“一二三” 在 Test1.sh 中,我按以下方式调用另一个 shell 脚本。Test2.sh $* 我
我有 2 个 shell 脚本。 第二个shell脚本包含以下函数第二个.sh func1 func2 first.sh 将使用一些参数调用第二个 shell 脚本, 将使用特定于该函数的一些其他参数
我有一个 Unix shell 脚本 test.sh。在脚本中,我想调用另一个 shell,然后从子 shell 执行 shell 脚本中的其余命令并退出 说清楚: test.sh #! /bin/b
我想在 shell 脚本中更改路径环境变量。路径变量需要在shell脚本执行后修改。 最佳答案 我知道有两种方法可以做到这一点。第一种是在当前 shell 的上下文中运行脚本: . myscript.
此 shell 脚本按预期运行。 trap 'echo exit' EXIT foo() { exit } echo begin foo echo end 这是输出。 $ sh foo.sh
我正在使用 vimshell在 vim 中执行命令 nnoremap vs :VimShellPop 使用此键映射,我可以打开 vim shell 并执行诸如“捆绑安装”之类的命令,然后 输入 exi
我想连接到不同的 shell(csh、ksh 等)并在每个切换的 shell 中执行命令。 下面是反射(reflect)我的意图的示例程序: #!/bin/bash echo $SHELL csh e
我目前正在尝试使用 BNF 和 LL 解析器在 C 中重新编写 shell。 否则,我需要知道 shell 运算符的优先级是什么| , > , > , & , ; ? 有没有人可以提供给我? 谢谢 最
不幸的是,我没有suspend 命令(busybox/ash)。但是我可以使用 kill -STOP $$ 从后台 shell (sh &) 返回到父 shell(以及 fg 之后)。 但是我不想输入
我需要知道,当用户切换到另一个 shell 时,通过单击它。 我试过 shellListener.shellDeactivated()但是当 shell 失去对它自己的控件的焦点时,会触发此事件,这意
file1.txt aaaa bbbb cccc dddd eeee file2.txt DDDD cccc aaaa 结果 bbbb eeee 如果能不区分大小写就更好了! 谢谢! 最佳答案 gre
我见过解压缩目录中所有 zip 文件的循环。但是,在运行此之前,我宁愿确保我将要运行的内容正常工作: for i in dir; do cd $i; unzip '*.zip'; rm -rf *.z
我对编程还很陌生,但我想知道 vim、emacs、nano 等 shell 文本编辑器如何能够控制命令行窗口。我主要是一名 Windows 程序员,所以可能在 *nix 上有所不同。据我所知,只能将文
我有一个包含第 7 列日期的文件,我的要求是将它与今天的日期进行比较,如果小于它,则删除该完整行。 此外,如果第 7 列中提到的任何日期超过 15 天,则将其修改为最多 15 天 下面的例子- now
我是一名优秀的程序员,十分优秀!