居然让我写了半天,受不了#include #include #include #include #include int main(int argc, char *argv[]){pid_t pid1, pid2int fdchar str[10]system("echo a b c d e >input.txt")if((pid1 = fork()) == -1){perror("can not create process1\n")exit(-1)}if(pid1 == 0)//process1{if((fd = open("input.txt", O_WRONLY)) == -1){perror("open input.txt failed\n")exit(-1)}lseek(fd, -1, SEEK_END)if(write(fd, "012345679", 11) == -1){printf("write the message to the file failed\n")exit(-1)}close(fd)}else{sleep(3)//let the process1 write the fileif((pid2 = fork()) == -1){perror("can not create process2\n")exit(-1)}if(pid2 == 0)//process2{if((fd = open("input.txt", O_RDONLY)) == -1){perror("open input.txt failed\n")exit(-1)}if(read(fd, str, 10) == -1){printf("get the message failed\n")exit(-1)}printf("%s\n", str)close(fd)}else//parent{waitpid(pid1, NULL, 0)waitpid(pid2, NULL, 0)}}return 0}
代码如下:
#!/bin/bash
read -p "请输入字符串:" str
if [ "$str" = "hello" ]then
echo "worl"
fi
可以将代码保存为 test.sh,然后添加权限:chmod +x test.sh,再执行:./test.sh
代码截图
评论列表(0条)