如何优雅的在windows(含win7)上执行linux脚本

原文地址

目录

原文地址

 

前言

下载安装cygwin

配置cygwin的默认shell

配置apt-cyg 工具实现命令安装新的软件和常见的命令

一些配置文件

vimrc配置文件的内容

fish shell配置

写一个shell脚本执行试试

结束语


前言

习惯了linux的指令操作,在使用windos总是不舒服,于是我们可以在windows上安装cygwin来运行linux上的shell脚本。

Cygwin是一个在windows平台上运行的unix模拟环境,是cygnus solutions公司开发的自由软件。
Cygwin就是一个windows软件,该软件就是在windows上仿真linux操作系统 ,简言之,cygwin是一个在windows平台上运行的 linux模拟环境,使用一个Dll(动态链接库)来实现 这样,我们可以开发出Cygwin下的UNIX工具,使用这个DLL运行在Windows下。

下载安装cygwin

Cygwin下载的是一个安装器,可到其官网下载。
1、双击安装包

2、三种安装模式
①Install from Internet,这种模式直接从Internet安装,适合网速较快的情况;
②Download Without Installing,这种模式只从网上下载Cygwin的组件包,但不安装;
③Install from Local Directory,这种模式与上面第二种模式对应,当你的Cygwin组件包已经下载到本地,则可以使用此模式从本地安装Cygwin

说明:当你安装过,在执行该安装程序可以选择本地安装,然后添加需要扩展的命令。

第一次安装使用第一种方式进行安装

3、选择安装路径

4、在下载的同时,Cygwin组件也保存到了本地,以便以后能够再次安装,这一步选择安装过程中从网上下载的Cygwin组件包的保存位置

5、这一步选择连接的方式,选择你的连接方式,然后点击下一步,会出现选择下载站点的对话框,如下图所示

①Use System Proxy Settings 使用系统的代理设置
②Direct Connection 一般多数用户都是这种直接连接的网络,所以都是直接使用默认设置即可
③Use HTTP/FTP Proxy 使用HTTP或FTP类型的代理。如果有需要,自己选择此项后,设置对应的代理地址和端口,即可

6、选择下载站点
不同的镜像存放了不同的包,为了获得最快的下载速度,我们可以添加网易开源镜像http://mirrors.163.com/cygwin/或者 阿里云镜像http://mirrors.aliyun.com/cygwin/

7、开始加载

8、选择需要下载安装的组件包
此处,对于安装Cygwin来说,就是安装各种各样的模块而已。最核心的,记住一定要安装Devel这个部分的模块,其中包含了各种开发所用到的工具或模块
展开devel

下面推荐推几个组件

  • fish:一个shell,具有良好的交互提示
  • lynx:命令安装组件的必须工具,强烈推荐安装此项

组件可以在search框输入后搜索,然后选中组件,在new列双击,当看到版本号后,安装就会将此组件安装上。
9、确认改变,进行安装

10.添加环境变量

cygwin安装目录/bin添加到系统环境变量,方便之后在其他终端调用相关命令。

配置cygwin的默认shell

当我们没配置fish shell,使用默认的shell时我们打开cygwin的终端是这样的

前面我们安装了fish shell,现在我们要把fish shell设置为我们默认的shell,我们在打开的cygwin中连续输入以下几个命令即可:

1. cd /etc
2. echo "fish" >> profile

然后我们重启cygwin应用即可。

配置好fish后我们是这样的

提示符会显示一些类容,包括当前路径
当你输入命令式,会根据你输入命令的历史提示你完整的命令,你可以使用键盘的->(右方向键)将提示的命令补充完整

配置apt-cyg 工具实现命令安装新的软件和常见的命令

cygwin作为windows下模拟Linux环境的的工具,使得我们能在windows下非常方便的使用Linux的命令和工具,下面讲讲怎样在cygwin添加不支持的命令。

1.安装cygwin工具的安装工具apt-cyg,此工具类似于Ubuntu下的apt-get工具,所有的工具都是通过他安装,用过Ubuntu的人应该都知道。
由于刚开始安装cygwin,有很多命令没有安装,这里采用手动配置该工具

step1、下载apt-cyg的源码
浏览器打开apt-cyg源码

step2、解压后将apt-cyg文件拷贝到cygwin安装目录/usr/local/bin目录下
你可以使用命令,也可以直接使用图形化的操作进行拷贝。
使用命令拷贝的方法如下:

cd 你刚刚下载文件的保存路径
cp apt-cyg /usr/local/bin/

然后你就可以看看apt-cyg命令是否可以了,在cygwin的终端输入apt-cyg --help,如果出现下面这样的结果就表示可以了。

然后你就可以使用apt-cyg来安装一些组件,比如安装python3组件,使用apt-cyg install python3即可完成。

依据个人习惯,可以安装一些组件,比如你可以使用命令安装vim,wget,tree,openssh:

apt-cyg install vim wget tree openssh

一些配置文件

  • vim的配置文件路径$HOME/.vimrc
  • fish配置文件是在$HOME/.config/fish/这个文件夹下面

当第一次安装的时候,上述文件可能不存在,需要我们自己创建。
为了款速配置,我放出自己的配置文件。

vimrc配置文件的内容

"设置 backspace的工作方式
	set nocompatible 
	set backspace=indent,eol,start


    "显示行号
    set nu

    "忽略大小写
    set ignorecase 
	"开启文件类型检测
	filetype plugin indent on
    "不忽略大小写
    "set noignorecase 

    "开启高亮
    syntax on

    "显示光标当前位置
    set ruler

    "启用鼠标
    set mouse=a

    "显示状态栏
    set laststatus=2
	
	
	"============
    "设置缩进"
    "============
    "set smartindent             " 开启新行时使用智能自动缩进
    "set cindent                 " 按照C语言语法自动缩进
    set shiftwidth=4            " 配置缩进空格数为4
    set tabstop=4               " 配置TAB键移动距离为4个空格
	set autoindent				"自动对齐
	
	
	"========
    "括号匹配"
    "========
    "set showmatch               " 插入括号时,短暂地跳转到匹配的对应括号
    "set matchtime=2             " 短暂跳转到匹配括号的时间
	
	
"本文件配置vim新建文件时自动生成的内容"
	"===========
	"配置新建文件的模板(自动化完成)
	"===========
"==================================make file============================================"
	"cpp文件自动生成模板"
	autocmd BufNewFile *.cpp exec ":call SetTitle()"
	    func SetTitle()
	        call setline(1,"/**") 
	        call append(line("."), " *   Copyright (C) ".strftime("%Y")." All rights reserved.")
	        call append(line(".")+1, " *") 
	        call append(line(".")+2, " *   FileName      :".expand("%:t")) 
	        call append(line(".")+3, " *   Author        :hpy")
	        call append(line(".")+4, " *   Email         :yuan_hp@qq.com")
	        call append(line(".")+5, " *   Date          :".strftime("%Y年%m月%d日")) 
	        call append(line(".")+6, " *   Description   :") 
	        call append(line(".")+7, " */") 
	        call append(line(".")+8, "")
	    endfunc
	"自动将光标定位到末尾"
	"autocmd BufNewFile * normal G"
"=============================================================================="	
	"配置shell脚本新建时的模板"
	autocmd BufNewFile *.sh exec ":call SetShTitle()"
	    func SetShTitle()
	        call setline(1,"#!/usr/bin/env bash") 
	        call append(line("."), "#-------------------------------------------------------") 
	        call append(line(".")+1, "#	FileName	: ".expand("%:t"))
	        call append(line(".")+2, "#	Author		:hpy")
	        call append(line(".")+3, "#	Date		:".strftime("%Y年%m月%d日"))
	        call append(line(".")+4, "#	Description	:")
	        call append(line(".")+5, "#-------------------------------------------------------")
			call append(line(".")+6, "")
	    endfunc
"=============================================================================="
	"配置verilog脚本新建时的模板"
		autocmd BufNewFile *.v exec ":call SetVTitle()"
		    func SetVTitle()
		        call setline(1,"`timescale 1ns / 1ps") 
		        call append(line("."), "// ********************************************************************") 
		        call append(line(".")+1, "//	FileName	: ".expand("%:t"))
		        call append(line(".")+2, "//	Author		:hpy")
		        call append(line(".")+3, "//	Email		:yuan_hp@qq.com")
		        call append(line(".")+4, "//	Date		:".strftime("%Y年%m月%d日"))
		        call append(line(".")+5, "//	Description	:")
		        call append(line(".")+6, "// --------------------------------------------------------------------")
		        call append(line(".")+7, "module " .expand("%:r") ."(")
		        call append(line(".")+8, "	input clk, ")
		        call append(line(".")+9, "	input rst_n")
		        call append(line(".")+10, ");")
		        call append(line(".")+11, " ")
		        call append(line(".")+12, "always@(posedge clk or negedge rst_n)")
		        call append(line(".")+13, "begin")
		        call append(line(".")+14, "	if(!rst_n)begin")
		        call append(line(".")+15, " ")
		        call append(line(".")+16, "	end ")
		        call append(line(".")+17, "	else begin ")
		        call append(line(".")+18, " ")
		        call append(line(".")+19, "	end ")
		        call append(line(".")+20, "end")
		        call append(line(".")+21, " ")
		        call append(line(".")+22, "endmodule")		        		    		      
		    endfunc

"=============================================================================="	
	"配置tcl脚本新建时的模板"
	autocmd BufNewFile *.tcl exec ":call SetTclTitle()"
	    func SetTclTitle()
	        call setline(1,"#!/usr/bin/env tclsh") 
	        call append(line("."), "#-------------------------------------------------------") 
	        call append(line(".")+1, "#	FileName	: ".expand("%:t"))
	        call append(line(".")+2, "#	Author		:hpy")
	        call append(line(".")+3, "#	Email		:yuan_hp@qq.com")
	        call append(line(".")+4, "#	Date		:".strftime("%Y年%m月%d日"))
	        call append(line(".")+5, "#	Description	:")
	        call append(line(".")+6, "#-------------------------------------------------------")
			call append(line(".")+7, "")
	    endfunc

"=============================================================================="	
	"配置python3脚本新建时的模板"
	autocmd BufNewFile *.py exec ":call SetPyTitle()"
	    func SetPyTitle()
	        call setline(1,"#!/usr/bin/env python3") 
	        call append(line("."), "# -- coding:utf-8 --")   
	        call append(line(".")+1, "#-------------------------------------------------------") 
	        call append(line(".")+2, "#	FileName	: ".expand("%:t"))
	        call append(line(".")+3, "#	Author		:hpy")
	        call append(line(".")+4, "#	Email		:yuan_hp@qq.com")
	        call append(line(".")+5, "#	Date		:".strftime("%Y年%m月%d日"))
	        call append(line(".")+6, "#	Description	:")
	        call append(line(".")+7, "#-------------------------------------------------------")
			call append(line(".")+8, "")
	    endfunc

	    
	"自动将光标定位到末尾"
	autocmd BufNewFile * normal G
	
	
"============================key======================================="
	"==========="
	"配置markdown编辑时的快捷键"
	"==========="
	
 	autocmd Filetype markdown inoremap // <Esc>/<++><CR>:nohlsearch<CR>c4l
 	"加粗"
 	autocmd Filetype markdown inoremap /b **** <++><Esc>F*hi
 	"删除线"
 	autocmd Filetype markdown inoremap /d ~~~~ <++><Esc>F~hi
 	"分割线"
 	autocmd Filetype markdown inoremap /- <Enter>---<Enter>
	"添加代码块"
	autocmd Filetype markdown inoremap /[ ```<Enter><++><Enter>```<Enter><Enter>
	"添加行内代码"
	autocmd Filetype markdown inoremap /e ``<++><Esc>F`i
	"文字下划线"
	autocmd Filetype markdown inoremap /u <u></u><++><Esc>F<F<i
	"换行实现
	autocmd Filetype markdown inoremap /<Enter> <Space><Space><Space><br/><Space><Space><Enter><Enter>
	"自动填充3个空格
	autocmd Filetype markdown inoremap /<Space> <Space><Space><Space>
	"上标
	autocmd Filetype markdown inoremap /6 <sup></sup><++><Esc>F<F<i
	"下标
	autocmd Filetype markdown inoremap /7 <sub></sub><++><Esc>F<F<i
	"链接
	autocmd Filetype markdown inoremap /h [](<++>)<Esc>F]i

 	"/ 生成"
 	autocmd Filetype markdown inoremap /z /

	"双引号"
	autocmd Filetype markdown inoremap " ""<LEFT>

	"花括号"
	autocmd Filetype markdown inoremap { {}<LEFT>

	"小括号"
	autocmd Filetype markdown inoremap ( ()<LEFT>	

	func! Mdgoto()
		if expand('%:t') != 'SUMMARY.md'
			return 
		endif 
		let currentLine = getline(".")
		let len = strlen(currentLine)
		"echo currentLine
		let a1= stridx(currentLine, '(') + 1
		let a2= stridx(currentLine, ')') - 1 
		let a3 = a2-a1+1
	
		if a3 > 0
			let sub=strpart(currentLine,a1,a3)
		endif
		exec "vsp " .sub
		vertical res +200
	
	endfunction
	autocmd Filetype markdown map <C-]> :call Mdgoto()<CR>"
	autocmd Filetype markdown map <C-q> :wq<CR>"
	"调出自定义输入快捷键帮助
	autocmd Filetype markdown inoremap /,, <Esc>:!/home/yhp/.vim/mconf/keyhelp markdown<CR><Esc>i
	

"==================================================================="

	"==========="
	"配置shell编辑时的快捷键"
	"==========="
	
 	autocmd Filetype sh inoremap // <Esc>/<++><CR>:nohlsearch<CR>c4l
 	"加粗"
 	autocmd Filetype sh inoremap /e echo "<++>"<++><Esc>/<++><CR>:nohlsearch<CR>c4l
	"引用"
	autocmd Filetype sh inoremap /y $() <++><Esc>F)i
	"算数运算"
	autocmd Filetype sh inoremap /a $[]<++><Esc>F]i
	"if流程"
	autocmd Filetype sh inoremap /i if [ ];then<++><Esc>F]i
	"case流程"
	autocmd Filetype sh inoremap /c <Esc>:r !echo -e 'case <++> in\n	<++>)<++>;;\nesac'<Esc>/<++><CR>:nohlsearch<CR>c4l

 	"/ 生成"
 	autocmd Filetype sh inoremap /z /

	"单引号"
	autocmd Filetype sh inoremap ' ''<LEFT>

	"双引号"
	autocmd Filetype sh inoremap " ""<LEFT>

	"花括号"
	autocmd Filetype sh inoremap { {}<LEFT>

	"小括号"
	autocmd Filetype sh inoremap ( ()<LEFT>	

	"中括号"
	autocmd Filetype sh inoremap [ []<LEFT>
	"字符串替换"
	autocmd Filetype sh inoremap /t ${/<++>/<++>}<++><Esc>F/F/i
	"调出自定义输入快捷键帮助
	autocmd Filetype sh inoremap /,, <Esc>:!/home/yhp/.vim/mconf/keyhelp shell<CR><Esc>i
"==================================================================="

	"==========="
	"配置verilog编辑时的快捷键"
	"==========="
	
 	autocmd Filetype verilog inoremap // <Esc>/<++><CR>:nohlsearch<CR>c4l
 	"always语句"
 	"autocmd Filetype verilog inoremap /a always@(<++>)<Enter>begin <Enter><Tab><++> end <Esc>/<++><CR>:nohlsearch<CR>c4l
	autocmd Filetype verilog inoremap /a <Esc>:r !echo -e 'always@(<++>)\nbegin\n	<++>\nend'<CR><Esc>/<++><CR>:nohlsearch<CR>c4l
	
 	"if 语句"
 	autocmd Filetype verilog inoremap /i if()begin<++>end<Esc>F)i

 	"else 语句"
 	autocmd Filetype verilog inoremap /e else beginend<Esc>Fei

 	"initial 语句"
 	"autocmd Filetype verilog inoremap initial initial beginend<Esc>Fei

  	"assign 语句"
 	autocmd Filetype verilog inoremap /= assign = <++><Esc>F=i

  	"module例化 设置"
  	autocmd Filetype verilog inoremap /. .(<++>)<++><Esc>F(i


  	"monitor 语句"
  	autocmd Filetype verilog inoremap /m $monitor("",<++>)<++><Esc>F"i

 	"display 语句"
 	autocmd Filetype verilog inoremap /dd $display()<++><Esc>F)i	

 	"行注释 语句"
 	autocmd Filetype verilog inoremap /z /

 	"parameter 语句"
 	autocmd Filetype verilog inoremap /p parameter <++> = <++>;<Esc>/<++><CR>:nohlsearch<CR>c4l


 	"localparam 语句"
 	autocmd Filetype verilog inoremap /l localparam <++> = <++>;<Esc>/<++><CR>:nohlsearch<CR>c4l

 	"reg 语句"
 	autocmd Filetype verilog inoremap /r reg[ : 0 ] <++><Esc>F:i
 	"wire 语句"
 	autocmd Filetype verilog inoremap /w wire[ : 0 ] <++><Esc>F:i

 	"常量 语句"
 	autocmd Filetype verilog inoremap /d <++>'d<++><Esc>/<++><CR>:nohlsearch<CR>c4l

 	"case 语句"
 	autocmd Filetype verilog inoremap /c case()<++>endcase<Esc>F)i
 	
 	"begin 语句"
 	autocmd Filetype verilog inoremap /b beginend<Esc>Fei

 	"? : 语句"
 	autocmd Filetype verilog inoremap /s ? <++> : <++><Esc>F?i

 	"function 语句"
 	autocmd Filetype verilog inoremap /f <Esc>:r !echo -e 'function [ <++> : 0 ]<++>;\n 	<++>\nendfunction'<CR><Esc>/<++><CR>:nohlsearch<CR>c4l

 	"中括号"
 	autocmd Filetype verilog inoremap [ []<LEFT>	


 	"大括号"
 	autocmd Filetype verilog inoremap { {}<LEFT>

 	"小括号"
 	autocmd Filetype verilog inoremap ( ()<LEFT>

 	"调出自定义输入快捷键帮助
	autocmd Filetype verilog inoremap /,, <Esc>:!~/.vim/mconf/keyhelp verilog<CR><Esc>i	
"==================================================================="

	"==========="
	"配置 python 编辑时的快捷键"
	"==========="
	
 	autocmd Filetype python inoremap // <Esc>/<++><CR>:nohlsearch<CR>c4l

 	"print 语句"
 	autocmd Filetype python inoremap /p print()<++><Esc>F)i

 	"多行注释"
 	autocmd Filetype python inoremap /n '''<++>'''<++><Esc>/<++><CR>:nohlsearch<CR>c4l

 	"if语句"
 	autocmd Filetype python inoremap if if :<++><Esc>F:i
 	
 	"else语句"
 	autocmd Filetype python inoremap else else:

 	"elif语句"
 	autocmd Filetype python inoremap elif elif :<++><Esc>F:i 	

 	"while 语句"
 	autocmd Filetype python inoremap while while :<++><Esc>F:i

 	"for 语句"
 	autocmd Filetype python inoremap for for <++> in <++> :<++><Esc>/<++><CR>:nohlsearch<CR>c4l

 	"def 函数语句"
 	autocmd Filetype python inoremap def def () :<++><Esc>F(i

  	"input 语句"
 	autocmd Filetype python inoremap input input()<++><Esc>F)i
 	 	
 	"中括号"
 	autocmd Filetype python inoremap [ []<LEFT>	


 	"大括号"
 	autocmd Filetype python inoremap { {}<LEFT>

 	"小括号"
 	autocmd Filetype python inoremap ( ()<LEFT>

 	"单引号"
 	autocmd Filetype python inoremap ' ''<LEFT>

  	"双引号"
 	autocmd Filetype python inoremap " ""<LEFT>

使用时只需把上面的类容拷贝到$HOME/.vimrc文件即可配置vim编辑器。

fish shell配置

上面提过fish shell的配置路径在$HOME/.config/fish/文件夹下,以下我将简要介绍一些一些配置文件。
当不存在上述路径时,我们使用命令mkdir -p $HOME/.config/fish进行创建,然后我们使用cd $HOME/.config/fish切换到该路径下。

然后我们新建配置文件config.fish,这个文件将会是配置fish shell的文件。使用命令vim config.fish,
文件内容可根据自己的需要进行配置,我的文件内容如下所示:

 

上面的函数还可以单独写成一个文件,在当前路径下建立functions文件夹,里面放置我们的函数,里面的函数将会自动发的成为一条fish支持的命令。但是有一定的格式要求,就是文件名要求命令名.fish,文件中的函数名称要是命令名。比如我实现一个web命令,还命令可以快速打开一些指定网页,那么这个文件名就要是web.fish,我自己的web.fish的文件内容如下所示:

#!/usr/bin/env fish
#UFUNCTION=命令直接打开指定web页面
#-------路径存储的函数-----------
function cat_web_list
printf "\
#-h 显示帮助
#blbl 打开哔哩哔哩 https://www.bilibili.com/
#github 打开个人github页面 https://github.com/yuan-hp
#gitee 打开gitee个人主页 https://gitee.com/
#kancloud 打开看云文档 https://www.kancloud.cn/dashboard
#pon 打开ProcessOn在线作图页面 https://www.processon.com/diagrams
#idata 打开iData文献搜索页面 https://www.cn-ki.net/
#zh 打开知乎 https://www.zhihu.com/
"

end

function web
    set cmd_name "web" #设计的指令名称
        set real_cmd "open"  #真正执行的指令
        set data_src "cat_web_list"  #数据来源
    set cnt (count $argv)
        if test $cnt -gt 0
            set CMD_IN $argv
        else
        echo "使用 $cmd_name -h 查看帮助!"
        return
        end
        switch $CMD_IN
    case "-h"
                echo "Usage: $cmd_name [option]   快速打开一些网页"
                echo
                cat_web_list | awk '{gsub(/#/, "");print $1,$2}' | column -s \  -t
                echo
                return
        case "*"
            set find_id "$data_src | awk '/#$argv/{print \$3}' "
                #echo $find_id
                set find_id (eval $find_id)
                if test -n "$find_id"
            eval "$real_cmd $find_id"
            return
                end
                echo "使用 $cmd_name -h 查看帮助!"
                return
        end

end

然后我们重新打开cygwin的终端输入web -h可以查看该命令是否成功!

那么我现在打开bilibil就可以在cygwin的终端输入web blbl就可直接打开bilibili的网页

这样我们就能实现自定义的一些命令。

关于fish shell的使用,就简单介绍到这里,详细了解可以到fish shell的官网进行学习,
fish shell官网

写一个shell脚本执行试试

如下所示:

结束语

通过配置cygwin,我们可以获得使用linux的一种体验,上面的介绍对其使用做了简单的介绍,很多的功能需要自己慢慢琢磨。

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值