Windows bat 调用Linux shell命令

您所在的位置:网站首页 linux批处理文件怎么运行 Windows bat 调用Linux shell命令

Windows bat 调用Linux shell命令

2023-09-08 15:59| 来源: 网络整理| 查看: 265

Windows使用Linux shell命令

众所周知,Linux下的与windows的cmd不是一个量级的东西。对于使用惯了Linux,MacOS的经常切换到cmd会抓狂。但是偶尔项目下又必须实现一些bat脚本。特别是在持续集成CI/CD部署时。 那么用windows bat调用gitbash,再调用Unix-like的命令的将是个很好的方法,至少能解决大部分需求。

img_0eac6bea751d62a6ca6d1c562448ecd2.png gitbash.png 安装git-bash

安装 gitforwindows,https://gitforwindows.org (安装不表),虽然主要是实现git客户端,但是附带的bash很有用。

安装bash组件,并添加到系统参数(PATH),安装后,任意文件夹右键,会出现【Git Bash Here】打开后就默认进入类似Linux的终端。

bat 调用shell

使用bat调用sh脚本,

新建一个 test.bat 文件,写入以下内容:

echo "windows .bat call .shell script"" "C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "pwd" "C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "cp a.txt b.txt" "C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "./helloworld.sh" "C:\Program Files (x86)\Git\bin\sh.exe" --login -i -c "curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxx' -H 'Content-Type: application/json' -d '{\"msgtype\": \"text\", \"text\": {\"content\":\"VIOM服务器58后端部署成功-\"}}'"

需要指定git-bash 安装目录 需要注意,部分windows字符,如双引号,单引号需要使用\ 进行转义。

这样就能实现

调用原生命令(在window下的bash只有部分Linux的系统命令)

调用常见的文件操作

调用本地shell脚本

调用curl外部命令等



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3