zoukankan      html  css  js  c++  java
  • c#利用脚本,本地执行linux命令

    如题,需要注意脚本的编码应该是ascii(阿里的centos8上是这样)。

    代码:

    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Text;
    
    namespace mylinux
    {
        class Program
        {
            static void Main(string[] args)
            {
                setCommand();
                doSh();
    
            }
            static void setCommand()
            {
                StreamWriter sw = new StreamWriter("test.sh", false, Encoding.ASCII);
                string t = "";
                sw.WriteLine("#!/bin/bash");
                Console.WriteLine("请输入Linux脚本命令(exit退出):");
                t = Console.ReadLine();
                while(t.ToLower()!="exit")
                {
                    sw.WriteLine(t);
                    Console.WriteLine("请输入Linux脚本命令(exit退出):");
                    t = Console.ReadLine();
                }
                sw.Close();
            }
            static void doSh()
            {
                Process process = new Process();
                process.StartInfo.FileName = "sh";
                process.StartInfo.Arguments = "test.sh";
                process.Start();
                process.WaitForExit();
                process.Close();
            }
        }
    }

    运行结果:

  • 相关阅读:
    LeetCode 9. Palindrome Number(回文数)
    POJ 1080 Human Gene Functions
    springcloud之gateway
    canal快速入门
    Java8新特性
    vue整合echarts
    vue整合微信支付
    Thymeleaf快速入门
    vue整合阿里云播发器
    vue之vuex
  • 原文地址:https://www.cnblogs.com/wanjinliu/p/12853635.html
Copyright © 2011-2022 走看看