首页 > 系统相关 >dart集成shell脚本调用功能

dart集成shell脚本调用功能

时间:2023-08-19 12:56:03浏览次数:27  
标签:shell await dart print pwd 调用 var password

pubspec.yaml里添加依赖:shell: any

import 'dart:io';

import 'package:shell/shell.dart';

void main(List<String> arguments) async {
  var shell = Shell();
  var password = Platform.environment['PASSWORD'];
  print('Password: $password');
  //password = "passwd";

  // Pipe results to files, easily.
  var echo = await shell.start('echo', arguments: ['hello world']);
  var kkk0 = await echo.stderr.readAsString();
  var kkk = await echo.stdout.readAsString();
  print("KKK-$kkk0");
  print("FFF-$kkk");

  // You can run a program, and expect a certain exit code.
  //
  // If a valid exit code is returned, stderr is drained, and
  // you don't have to manually.
  //
  // Otherwise, a StateError is thrown.
  var find = await shell.start('find', arguments: ['.']);
  await find.expectExitCode([0]); // Can also call find.expectZeroExit()

  // Dump outputs.
  print(await find.stdout.readAsString());

  // You can also run a process and immediately receive a string.
  var pwd = await shell.startAndReadAsString('pwd', arguments: []);
  print('cwd: $pwd');

  // Navigation allows you to `cd`.
  // 这个目录要存在
  shell.navigate('./lib');
  pwd = await shell.startAndReadAsString('pwd', arguments: []);
  print('cwd: $pwd');

  // We can make a separate shell, with the same settings.
  var forked = Shell.copy(shell)
    ..sudo = true
    ..password = password;

  // Say hi, as an admin!
  var superEcho = await forked.start('echo', arguments: ['hello, admin!']);
  await superEcho.expectExitCode([0, 1]);
  // 这里会要求sudo来执行,前面的password要先设置正确
  print("HHH-${await superEcho.stdout.readAsString()}");
}

 

标签:shell,await,dart,print,pwd,调用,var,password
From: https://www.cnblogs.com/silentdoer/p/17642338.html

相关文章

  • 在Python中,当你调用一个类的方法时,需要将类的实例作为第一个参数传递给方法。 括号
    classClass_test:def__init__(self):pass#若无则报错AttributeError:'Class_test'objecthasnoattribute'fun_zip'deffun_zip(self,df_arg=pd.DataFrame(),bool_arg=False):#......
  • Xshell 连接本地虚拟机成功案例
    1、首先打开虚拟机,登录到操作系统;输入“ifconfig”,在弹出的一段中,inet地址就是本地虚拟机的ip地址。2、接着打开xshell软件如果没有可以到 http://www.linuxidc.com/Linux/2016-08/134086.htm 下载xshell点击“新建”按钮,或者用快捷键Alt+N,新建会话在会话中的主机中输入刚刚得到......
  • python调用java的jar包,在scrapy中处理加密逻辑
    APP采集过程中有些请求是需要加密处理的,之前的方式是通过frida-inject的方式处理的,但是这需要连接手机,好在本次处理的APP加密逻辑不是很复杂,加密逻辑都在java层,于是便将里面的java层的加密逻辑单独摘出来,想单独的做成一个jar包,这样就不用再用python做一个相同的算法还原了经过半......
  • 记录Python调用企业微信报错"errcode":44004,"errmsg":"Warning: wrong json format.
    1.通过单独的文件调用接口发送消息无异常;2.通过嵌套调用接口报错:"errcode":44004,"errmsg":"Warning:wrongjsonformat.;3.检查json文本格式无异常;4.怀疑json文本赋值有问题,增加trycache捕获,没有报错,但是可以正常调用接口!!5.打印json文本,比对trycatch的区别,发现多了个空格,依葫......
  • shell中if判断
    一、字符串判断1、常规判断str1=str2当两个串有相同内容、长度时为真str1!=str2当串str1和str2不等时为真-nstr1当串的长度大于0时为真(串非空)-zstr1当串的长度为0时为真(空串)str1当串st......
  • shell命令-补充
    1.whilewhilereadline 实现输出符合要求的行2.case基本结构进阶:在case外套用while实现循环输出实现简易计算器:3.grep作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。grep全称是GlobalRegularExpressionPrin......
  • .net【C#】调用 webservice
    【C#】调用webservice的三种方法总结 WebService,WCF,WebApi区别与特点......
  • shell select命令语句 用户多选
    select命令语句,默认只能输入一个选择项。但有时候需要让用户输入多个选项,就需要加for循环处理多选项了。一、示例代码#!/usr/bin/envbashchoices=('one''two''three''four''five')#samplechoicesselectdummyin"${choices[@]}";do#present......
  • C#调用微软api文本转语音
    目录1.注册微软云服务,搭建文本转语音标准应用(每月500万字免费好像)2.Visualstudio使用nuget给程序安装Microsoft.CognitiveServices.Speech框架3.引用命名空间4.文本转语音参考代码5.文本转语音下载到本地参考代码1.注册微软云服务,搭建文本转语音标准应用(每月500万字免费好......
  • shell 学习之文本处理工具
    视频:07_小工具使用diff【正常模式】_哔哩哔哩_bilibili1.grep 2.cut 3.sort  4.uniq 5.tee从标准输入读取并写到标准输出和文件,即双向覆盖重定向(屏幕输出/文本输入)。 6.diff 逐行比较文件的不同。 语法:diff[选项]file1file2 1)正常显示......