如何写一个linux脚本,给程序A输入字符串

如何写一个linux脚本,给程序A输入字符串,第1张

居然让我写了半天,受不了#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

代码截图


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8658246.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-19
下一篇 2023-04-19

发表评论

登录后才能评论

评论列表(0条)

保存