linux 脚本 lang,golang可以编写shell脚本吗
golang可以编写shell脚本吗
golang是可以编写shell脚本,首先可以通过创建一个阅读器去访问键盘,每当按下回车键时,任何的写入都会被存储到输入变量中;然后根据出入的数据来执行逻辑运算;最后将处理结果使用“fmt.Println()”将数据输出即可。
代码示例:package main
import ( "bufio"
"errors"
"fmt"
"os"
"os/exec"
"strings")
func main() {
reader := bufio.NewReader(os.Stdin) for {
fmt.Print("> ") // Read the keyboad input.
input, err := reader.ReadString('\n') if err != nil {
fmt.Fprintln(os.Stderr, err)
} // Handle the execution of the input.
err = execInput(input) if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
}
// ErrNoPath is returned when 'cd' was called without a second argument.var ErrNoPath = errors.New("path required")
func execInput(input string) error { // Remove the newline character.
input = strings.TrimSuffix(input, "\n") // Split the input separate the command and the arguments.
args := strings.Split(input, " ") // Check for built-in commands.
switch args[0] { case "cd": // 'cd' to home with empty path not yet supported.
if len(args) < 2 { return ErrNoPath
}
err := os.Chdir(args[1]) if err != nil { return err
} // Stop further processing.
return nil
case "exit":
os.Exit(0)
} // Prepare the command to execute.
cmd := exec.Command(args[0], args[1:]...) // Set the correct output device.
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
// Execute the command and save it's output.
err := cmd.Run() if err != nil { return err
} return nil}
推荐教程:《Go教程》
linux 脚本 lang,golang可以编写shell脚本吗相关推荐
- 用python写shell脚本_应用python编写shell脚本
今天同事叫我编写一个shell脚本.话说,虽然我受*nix的影响甚深,但是对于*nix里随处可见的sh脚本却是讨厌之极.为什么讨厌呢?首先是因为sh脚本那莫名其妙的语法,感觉就像随写随扔的程序,完全没 ...
- python编写shell脚本详细讲解_python编写shell脚本详细讲解
今天同事叫我编写一个shell脚本.话说,虽然我受*nix的影响甚深,但是对于*nix 里随处可见的sh脚本却是讨厌之极.为什么讨厌呢?首先是因为sh脚本那莫名其妙的语法,感觉就像随写随扔的程序,完全 ...
- ue 编写linux脚本,通过什么工具编写shell脚本更方面直观
刚开始学习写shell脚本,现在主要是通过vi编辑器,但感到很不方便和直观.通过UtralEdit倒是方便但不直观,比如shell编程中的关键字不能醒目出来.想请教一下各位大虾,有没有这么类似于像vc ...
- vi新建一个shell脚本_Linux Vim 编辑器编写 Shell 脚本
Vim 编译器 Vim 文件编译器是编写文件的好工具,它包括以下 3 种模式: 命令模式:移动光标,对文本进行增删查改等操作: 输入模式:向文件中输入文本: 末行模式:保存或退出文档,设置编辑环境. ...
- Linux配置脚本导出运行,linux服务器部署jar包以及shell脚本的书写
背景:记录在linux环境下部署jar程序的过程 1 部署过程记录 1.1 程序结构 这里的main函数就在DemRest2.java 文件中. 为了部署方便,要做到以下两点: 1 在导出的jar包中 ...
- 编写shell脚本扫描出局域网所有ip设备的mac地址
用linux四剑客和arping编写shell脚本,实现扫描出局域网所有ip设备的mac地址: 1.你要安装了arping 2.要有ifconfig命令(manjaro就没有,manjaro是ip a ...
- linux 脚本监听,Linux—编写shell脚本监控主机
编写SHELL脚本监控主机 1.用vi命令在/root目录是新建一个脚本文件sysmon.sh: 内容如下: #!/bin/bash dug=$(df -h | grep "/$" ...
- 【Linux学习】如何编写Shell脚本调用企业微信api来发消息给企业微信成员?
一.前言 最近通过python实现了发送消息给企业微信的功能,参考链接: [Jenkins学习 ]如何编写Python脚本来调用企业微信的api通知企业微信成员关于Jenkins的编译结果? http ...
- Linux中shell编程——编写shell脚本,实现功能:在用户输入年、月后自动打印出对应年月的日
实验目的 理解shell程序的设计方法 掌握shell程序编辑.运行.调试方法与过程 实验内容 shell简单编程 实验过程 要求: 1.记录实验过程,对关键过程进行文字说明并附上操作截屏. 2.逻辑 ...
最新文章
- js根据经纬度取随机附近经纬度
- Know more about CBO Index Cost
- 《ASP.NET4 从入门到精通》学习笔记4
- 【正一专栏】梅西终老巴萨可被裁判吹掉了已进之球
- @autowired注入mapper_Intellij IDEA中Mybatis Mapper自动注入警告的6种解决方案
- 如何写windows系统已保护的内存区域
- Js文件中调用其它Js函数的方法
- xbox one s驱动_续航800公里 体验6座SUV理想ONE
- 多伦多大学密西莎加计算机科学,解析加国名校多伦多大学密西沙加校区
- 项目Alpha冲刺Day3
- Atitit 团队工具链体系打造---提升团队效率的一些通用软件 attilax总结
- 一键生成 Android 录屏 gif 的脚本
- 软考中级软件设计师---下午题
- 小黄鸡 php,小黄鸡simsimi接口
- Ubuntu 环境部署 安装大全
- [W ParallelNative.cpp:212] Warning: Cannot set number of intraop threads after parallel work h
- 济南大学计算机专硕考研真题,济南大学考研真题各专业汇总
- HTML5期末大作业:饮食食品主题设计——绿色简洁生鲜超市网站设计(5页) HTML+CSS+JavaScript web前端大二实训大作业
- 推荐几个非常棒的学习计算机语言的网站
- 老码农回答为什么会有“在X有Y年的工作经验“这样的要求