现在的位置: 首页 > 综合 > 正文

管道输出作为程序的输入

2013年08月08日 ⁄ 综合 ⁄ 共 722字 ⁄ 字号 评论关闭

今天看到论坛上有人提问通过shell代替终端输入,发现这种方法和交互式有一点异曲同工的作用,记下来,以后使用。

1.写一个需要有输入的程序

#include<stdlib.h>
#include<stdio.h>
int main()
{
  printf("input the string:");
  char temp[20];
  gets(temp);
  printf("%s\n",temp);
  return 0;
}

用C写完后编译命名新文件为simple

#!/usr/bin/env python

a = raw_input('input the string :')
print a

连接对应输入的程序:

#!/bin/bash

parm="ls -al"
result=`echo "$parm" | python py1.py`
echo $result

#!/bin/bash

parm="ls -al"
result=`echo "$parm" | simple`
echo $result

运行结果:

sh -x auto_input.sh 
+ parm=ls -al
+ echo ls -al
+ python py1.py
+ result=input the string :ls -al
+ echo input the string :ls -al
input the string :ls -al

~$ sh -x auto_input.sh 
+ parm=ls -al
+ echo ls -al
+ ./simple
+ result=input the string:ls -al
+ echo input the string:ls -al
input the string:ls -al

通过以上结果可以看出:变量parm中的值作为了程序的输入,实现了免去人工输入的步骤,不过后续步骤如何进行添砖加瓦,还要更多尝试;

抱歉!评论已关闭.