0
点赞
收藏
分享

微信扫一扫

主流语言运行系统命令


C# 版本 带返回值的:

int i = 0;
do {
    Console.WriteLine(i++);
    Console.WriteLine();
    Cmd.RunCmd("ipconfig");
    Thread.Sleep(5000);
} while (1 == 1);
class Cmd
{
    private static string CmdPath = @"cmd.exe";
    /// <summary>
    /// 执行cmd命令 返回cmd窗口显示的信息
    /// 多命令请使用批处理命令连接符:
    /// <![CDATA[
    /// &:同时执行两个命令
    /// |:将上一个命令的输出,作为下一个命令的输入
    /// &&:当&&前的命令成功时,才执行&&后的命令
    /// ||:当||前的命令失败时,才执行||后的命令]]>
    /// </summary>
    ///<param name="cmd">执行的命令</param>
    public static string RunCmd(string cmd)
    {
       cmd = cmd.Trim().TrimEnd('&') + "&exit";//说明:不管命令是否成功均执行exit命令,否则当调用ReadToEnd()方法时,会处于假死状态
       // cmd = cmd.Trim();
        using (Process p = new Process())
        {
            p.StartInfo.FileName = CmdPath;
            
            p.StartInfo.Arguments = "/c " + cmd; 
            p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
            p.StartInfo.RedirectStandardInput = true; //接受来自调用程序的输入信息
            p.StartInfo.RedirectStandardOutput = true; //由调用程序获取输出信息
            p.StartInfo.RedirectStandardError = true; //重定向标准错误输出
            p.StartInfo.CreateNoWindow = false; //不显示程序窗口
            p.Start();//启动程序

            //向cmd窗口写入命令
            // p.StandardInput.WriteLine(cmd);
            // p.StandardInput.AutoFlush = true;

            //获取cmd窗口的输出信息
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();//等待程序执行完退出进程
            Console.WriteLine("Err code:" + p?.ExitCode);
            p.Close();
            //将cmd的输出展示在控制台
            Console.WriteLine(output);

            return output;
        }
    }

    public static bool RunCmdPs(string cmdStr)
        {
            bool result = false;
            try
            {
                using (Process myPro = new Process())
                {
                    //指定启动进程是调用的应用程序和命令行参数
                    ProcessStartInfo psi = new ProcessStartInfo(CmdPath, cmdStr);
                    psi.UseShellExecute   = true;
                    psi.CreateNoWindow = true;

   //zprocessStartInfo.RedirectStandardOutput = false;
                    // myPro.StartInfo = psi;
                    Process? process = Process.Start(psi);
                    myPro.WaitForExit();
                    Console.WriteLine("Err code:" + process?.ExitCode);
                    result = true;
                }
            }
            catch
            {

            }
            return result;
        }
}

运行结果:

主流语言运行系统命令_Code

Python版本的 带返回值的:

def run_cmd_Popen_fileno(cmd_string):
    """
    执行cmd命令,并得到执行后的返回值,python调试界面输出返回值
    :param cmd_string: cmd命令,如:'adb devices'
    :return:
    """
    import subprocess
    
    print('运行cmd指令:{}'.format(cmd_string))
    return subprocess.Popen(cmd_string, shell=True, stdout=None, stderr=None).wait()

# 注意调用顺序 先定义函数
result = run_cmd_Popen_fileno("docker-compose build")
i = 0
while result !=0:
    print("第" + str(i) + "次构建")
    print("---------------------------------------------------")
    result = run_cmd_Popen_fileno("docker-compose build")
    i = i + 1


举报

相关推荐

0 条评论