我想通过python脚本在gnome-terminal中运行一个名为client.sh的进程,并希望将参数作为输入传递来执行它。以下是我的代码
String[] potential_player_pieces = new String[10]; // Array storing the playing pieces available
potential_player_pieces[0]= "*";
potential_player_pieces[1]= "|";
potential_player_pieces[2]= "?";
potential_player_pieces[3]= "@";
potential_player_pieces[4]= "&";
potential_player_pieces[5]= "¬";
potential_player_pieces[6]= "!";
potential_player_pieces[7]= "%";
potential_player_pieces[8]= "<\n";
String[] player_pieces = new String[players+1]; // String to store the playing pieces that the players have chosen
for (int i=1; i<=players; i++) // Loops to the number of players and asks them what playing piece they want
{
System.out.print("Player " + i + " pick a playing piece:"); // Asks the players the question
for (int j=0; j<=8; j++){
System.out.print(potential_player_pieces[j]); // Displays the possible playing pieces
}
player_pieces[i] = reader.nextLine();//Saves the player chioces to the array made above
}
我的意图是将输入作为“1”发送到client.sh进程。 但是使用上面的代码我的进程[client.sh]没有得到任何输入。 如何将输入发送到子流程?
答案 0 :(得分:0)
您只需使用参数调用命令:
process = subprocess.Popen(['sudo gnome-terminal -x ./client.sh', '1'], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)
使用shell=True
时请小心,请参阅this。
如果您需要&#34;模仿&#34;参数输入,使用:
process = subprocess.Popen(['sudo gnome-terminal -x ./client.sh'], stdin=PIPE, stdout=PIPE, stderr=PIPE, shell=False)
然后:
process.stdin.write("1" + "\n")