1
#include <iostream>
using namespace std;

int main(void){
  int number = 0;
  cout << "Please enter a number: ";
  cin >> number ;
  cout << "the number you enter is " << number << endl;

  return 0;}

这是我的程序,它接受一个参数并将其打印出来。

#! /bin/bash
number=1
echo "1" | ./a.out #>> result.txt

这是我的 bash 脚本,它试图将参数传递给程序。

1
Please enter a number: the number you enter is 1

这是结果.txt。我希望它更像这样:

Please enter a number: 1
the number you enter is 1

我应该如何修复它,以便脚本更像人类一样传递参数。

并且 bash 是一种非常好的脚本语言来完成这种工作还是有其他更好的脚本语言。(谷歌说 tcl 对于这种交互式程序来说比 bash 更好?)

4

1 回答 1

0

除非我误解了这个问题,否则如果你想将参数传递给你的 c++ 程序,你应该将 argc 和 argv 添加到你的 main 函数中。

你的程序.cpp:

#include <iostream>
using namespace std;

int main(int argc, char *argv[]) {
    cout << "your number is " << argv[1] << endl;
    return 0;
}

外壳脚本(send-arg.sh):

#!/bin/sh

./program 42

输出:

./send-arg.sh
your number is 42
于 2018-11-17T23:22:07.830 回答