我正在尝试使用以下代码捕获我在python脚本中执行的bash行的输出:
<md-autocomplete
md-hide-autocomplete-on-enter
md-items="item in querySearch(searchText)"
md-item-text="item.name">
<span md-highlight-text="searchText">{{item.name}} :: {{item.type}}</span>
</md-autocomplete>
这很好但我想改变它,以便将bash代码直接写入这个python脚本(不需要单独的.sh文件)。我知道我可以使用 import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.support.v4.app.FragmentActivity;
import java.util.List;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener{
private DrawerLayout drawerLayout;
private Toolbar toolbar;
private ListView listView;
private String[] planets;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_appbar);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp(R.id.fragment_navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
drawerLayout=(DrawerLayout)findViewById(R.id.drawerLayout);
planets=getResources().getStringArray(R.array.planets);
listView=(ListView) findViewById(R.id.drawerList);
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, planets));
listView.setOnItemClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
return super.onOptionsItemSelected(item);
}
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(this, planets[position] + " was selected", Toast.LENGTH_LONG).show();
selectItem(position);
selectItem2(view);
}
public void selectItem(int position) {
listView.setItemChecked(position, true);
setTitle(planets[position]);}
public void selectItem2 (View view){
switch (view.getId()) {
case 0:
Intent intent = new Intent(view.getContext(), physical_fragment.class);
startActivityForResult(intent, 0);}}
public void setTitle(String title){
getSupportActionBar().setTitle(title);
}
}
从python运行bash代码,但我无法弄清楚如何捕获输出(通常会打印到终端,因为我使用了错误的术语)。有人可以帮忙吗?
我在CentOS上运行python 2.7
答案 0 :(得分:1)
subprocess.Popen的第一个参数是参数列表(除非您指定shell=True
)。
所以你可以像
那样做你的例子call_bash = subprocess.Popen(['ssh', 'me@cli.globusonline.org', 'scp me#comp:/~/blah.text ep#comp:/register/'], stdout=subprocess.PIPE)
output = call_bash.communicate()[0]
这将使用ssh
和me@cli.globusonline.org
两个参数调用scp me#comp:/~/blah.text ep#comp:/register/
命令。