
C++ -------> g++ -------> bash :
#include <cstdlib>
using namespace std;
int main(){
system("mail -s test_mail [email protected]");
system("test msg");
system(".");
return 0;
}
最初のシステム コマンドは正常に実行されますが、その後は別の入力ストリーム (stdin ではない) から入力を受け取るように見えます。system("test msg") は、メール プログラムから cntl+c で抜けた後にのみ実行され、その後にのみ bash は "test msg" を解釈しようとします。
答え1
system()
実際にはコマンドラインを実行するだけです。パイプのようなものではありません。
サンプルプログラムを動作させるための最小限の変更は、
int main()
{
system("echo test msg | mail -s test_mail [email protected]");
return 0;
}
なぜなら、メッセージはmail
パイプ経由でプログラムの標準入力に送られるからです。
より実用的なのは、メッセージを一時ファイルに書き込んで、mail
そのファイルから標準入力をリダイレクトすることです。