Question

I have a C++ program and it reads various inputs from user (standard input) interactively and it works fine. But now I need to execute this program from a shell script (bash) and need to pass these inputs from this script only instead of passing from user manually. My binary name is myprog and this what I have tried in the script.. but it is not working.

myprog << \
  __EOF__ | tail -n+21 | grep ":" >> logfile.log 2> /dev/null
   $bdy
   $bdm
   $bdd
   $bth
   $btm
   $bts
   $edy
   $edm
   $edd
   $eth
   $etm
   $ets

EOF

These variables contains some date and time, like year = 2014, month 01, hour = 04, minute = 59 and second = 20

Was it helpful?

Solution

Your C++ program has nothing else to do then reading the data from stdin (or cin for C++). It's your decision on the bash shell to create a temporary object with the data and pass it to the process that runs the executable that have been compiled from the C++ source.

Use a bash script like this

myprog << EOF | tail -n+21 | grep ":" >> logfile.log 2> /dev/null
foo
bar
EOF

Please note the exact macth between the end indicator in the last line and behind the <<.

Here a further example:

Harper@AMSEL ~/Documents
$ cat fff
cat << EOF -b | tail >> logfile.log 2>/dev/null
this text is used
these chars too
EOF

Harper@AMSEL ~/Documents
$ sh ./fff

Harper@AMSEL ~/Documents
$ cat logfile.log
     1  this text is used
     2  these chars too

Harper@AMSEL ~/Documents

You may have to check what arguments of you command is evaluated locally and what is used with the ssh at the remove machine. Probably the example above helps to isolate the problem from myprog.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top