shell 脚本重定向
时间: 2023-10-23 08:13:10 浏览: 54
重定向是shell脚本中常用的技术,可以将命令的输入、输出重定向到文件或者其他设备。常见的重定向操作符有以下几种:
1. 标准输出重定向(>):将命令的标准输出重定向到指定文件中,如果文件不存在,则会创建该文件并将输出写入文件。如果文件已存在,那么会先清空文件内容再写入输出。
例如:`echo "Hello, World!" > output.txt` 将输出写入 output.txt 文件。
2. 标准错误重定向(2>):将命令的标准错误输出重定向到指定文件中。
例如:`ls /path/to/nonexistent 2> error.txt` 将错误信息写入 error.txt 文件。
3. 追加重定向(>>):将命令的输出追加到指定文件中,而不是覆盖文件内容。
例如:`echo "New content" >> output.txt` 将 "New content" 追加到 output.txt 文件末尾。
4. 标准输入重定向(<):将文件内容作为命令的输入。
例如:`sort < input.txt` 将输入文件 input.txt 的内容作为 sort 命令的输入。
5. Here Document:用于在脚本中直接提供多行输入。
例如:
```
cat << EOF > output.txt
Line 1
Line 2
EOF
```
将多行输入重定向到 output.txt 文件。
这些是shell脚本中常用的重定向操作符,可以根据需要选择合适的方式来进行重定向。
相关问题
shell脚本重定向
Shell脚本重定向是指将命令的输入或输出从默认的位置重定向到其他位置,以实现更灵活的操作。常见的重定向符号包括">"、"<"、">>"、"<<"等。例如,将命令的输出重定向到文件中可以使用">"符号,将命令的输入从文件中读取可以使用"<"符号。重定向还可以用于将命令的输出传递给另一个命令,或将命令的错误输出重定向到文件中。在Shell脚本中,重定向是非常常用的操作,可以帮助我们更好地控制命令的输入输出。
C语言实现Shell脚本重定向函数check4redirection和执行函数execute代码
以下是一个简单的C语言实现Shell脚本重定向函数check4redirection和执行函数execute的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#define MAXARGS 20
#define MAXLINE 80
int check4redirection(char *cmd, int *in, int *out) {
int i, len, pos = 0;
char *args[MAXARGS];
char *infile, *outfile;
len = strlen(cmd);
for (i = 0; i < len; i++) {
if (cmd[i] == '<') {
infile = strtok(&cmd[i+1], " \t\n");
*in = open(infile, O_RDONLY);
if (*in == -1) {
perror("Open file error");
return -1;
}
cmd[i] = '\0';
}
if (cmd[i] == '>') {
outfile = strtok(&cmd[i+1], " \t\n");
*out = open(outfile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (*out == -1) {
perror("Open file error");
return -1;
}
cmd[i] = '\0';
}
}
args[pos++] = strtok(cmd, " \t\n");
while ((args[pos++] = strtok(NULL, " \t\n")) != NULL);
args[pos] = NULL;
if (*in != STDIN_FILENO) {
if (dup2(*in, STDIN_FILENO) == -1) {
perror("Dup2 error");
return -1;
}
close(*in);
}
if (*out != STDOUT_FILENO) {
if (dup2(*out, STDOUT_FILENO) == -1) {
perror("Dup2 error");
return -1;
}
close(*out);
}
return execvp(args[0], args);
}
int execute(char *cmd) {
int pid, status, in = STDIN_FILENO, out = STDOUT_FILENO;
if ((pid = fork()) == -1) {
perror("Fork error");
return -1;
}
else if (pid == 0) {
return check4redirection(cmd, &in, &out);
}
else {
while (wait(&status) != pid);
return status;
}
}
int main(int argc, char *argv[]) {
char line[MAXLINE];
int status;
while (1) {
printf("myShell> ");
fgets(line, MAXLINE, stdin);
if (strcmp(line, "exit\n") == 0)
exit(0);
status = execute(line);
printf("Exit status: %d\n", status);
}
return 0;
}
```
检查重定向的函数check4redirection接受一个命令字符串,以及两个指针(in和out),用于存储输入和输出的文件描述符。该函数首先使用strtok函数将命令字符串拆分为命令和参数数组args,然后在命令字符串中获取输入和输出文件名,并打开相应文件。最后,该函数使用dup2函数将输入和输出文件描述符复制到STDIN_FILENO和STDOUT_FILENO,并关闭原始文件描述符。最后,该函数使用execvp函数执行指定的命令和参数。
执行函数execute接受一个命令字符串并在新进程中调用check4redirection函数。如果fork失败,则返回-1。在子进程中,调用check4redirection函数执行命令。在父进程中,等待子进程执行完毕,并返回子进程的退出状态。主函数使用fgets从标准输入读取命令行,并在循环中执行命令。如果用户输入exit,则退出程序。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)