passing variables from bash to executable (which reads argument with stdin) -


i have following test.cpp c++ program

#include <stdio.h> #include <stdlib.h> #include <iostream>  using namespace std;  int main() {     float a,b,c;     cout<<"give 1st number";     cin>>a;     cout<<"give 2nd number:";     cin>>b;      c=a+b;     cout<<"\n"<<a<<"+"<<b<<"="<<c<<endl;  return 0; } 

and want create shell script gives input variables. know how pass 1 variable, , know if there way pass 2 variables... following test.sh file not working

#!/bin/bash  g++ test.cpp -o testexe chmod +x testexe  a=1 b=2  ./testexe <<< $a $b 

to compatible not bash, /bin/sh -- while avoiding pipeline overhead -- use heredoc:

./testexe <<eof $a $b eof 

if don't care pipeline overhead (and still maintaining /bin/sh compatibility, answer using <<< lacks):

printf '%s\n' "$a" "$b" |  ./testexe 

if don't care /bin/sh compatibility:

./testexe <<<"$a"$'\n'"$b" 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -