将* bat文件转换为.sh文件

时间:2013-11-17 15:26:41

标签: file batch-file

我想将批处理文件更改为* .sh文件以在linux中运行吗?

* bat文件如下:

@echo off
rem -----------
rem Use following two lines to input value on command line
rem if "%1"=="" goto NoRunners
rem set N=%1
rem -----------
set N=8

if %N%==0 goto NoRunners

cd W:\MCMC\runner1\MCMC\tracer2D\
Echo Starting Runner 1
Start "Runner 1" /min runner

if %N%==1 goto GoHome

cd W:\MCMC\runner2\MCMC\tracer2D\
Echo Starting Runner 2
Start "Runner 2" /min runner

if %N%==2 goto GoHome

cd W:\MCMC\runner3\MCMC\tracer2D\
Echo Starting Runner 3
Start "Runner 3" /min runner

if %N%==3 goto GoHome

cd W:\MCMC\runner2\MCMC\tracer2D\
Echo Starting Runner 4
Start "Runner 4" /min runner

if %N%==4 goto GoHome

cd W:\MCMC\runner3\MCMC\tracer2D\
Echo Starting Runner 5
Start "Runner 5" /min runner

if %N%==5 goto GoHome

cd W:\MCMC\runner3\MCMC\tracer2D\
Echo Starting Runner 6
Start "Runner 6" /min runner

if %N%==6 goto GoHome

cd W:\MCMC\runner3\MCMC\tracer2D\
Echo Starting Runner 7
Start "Runner 7" /min runner

if %N%==7 goto GoHome

cd W:\MCMC\runner3\MCMC\tracer2D\
Echo Starting Runner 8
Start "Runner 8" /min runner

if %N%==8 goto GoHome

cd W:\MCMC\runner3\MCMC\tracer2D\
Echo Starting Runner 9
Start "Runner 9" /min runner



:GoHome
cd ..\..\..\
goto End

:NoRunners
Echo No Runners started

:End
pause

2 个答案:

答案 0 :(得分:0)

你真的必须手工翻译整个脚本。不过,我已经有了一段时间的想法来制作至少DOS批处理语言的bash实现(扩展和Windows特定命令将更难实现),但它需要批次

答案 1 :(得分:0)

这是我的sh脚本,用sed自动执行此任务。在包含批处理文件的目录中运行它(备份的扩展名为.bak):

#!/bin/sh
files="*.bat"
sed -i.bak "s/%\([^%]\+\)%/\$\1/g" $files #Variables conversion
sed -i "s/%\([0-9]\+\)\b/\$\1/g" $files
sed -i "s/if \+not \+defined \+\([^ ]\+\) /[ -z \"\$\1\" ] \&\& /gI" $files
sed -i "s/if \+defined \+\([^ ]\+\) /[ -n \"\$\1\" ] \&\& /gI" $files
sed -i "s/if \+not \+exist \+\([^ ]\+\) /[ ! -f \$\1 ] \&\& /gI" $files
sed -i "s/if \+exist \+\([^ ]\+\) /[ -f \$\1 ] \&\& /gI" $files
sed -i "s/if \+not \+\([^ ]\+\) /[ ! \1 ] \&\& /gI" $files
sed -i "s/if \([^ ]\+\) /[ \1 ] \&\& /gI" $files
sed -i "s/\\\\/\//gI" $files  #path conversion
sed -i "s/\^|/|/gI" $files #backslashed pipe conversion
sed -i "s/\"==\"/\" == \"/gI" *.bat # == conversion
sed -i "s/\brem /# /gI" $files #comments conversion
sed -i "s/\bnul\b/\/dev\/null/gI" $files # 'nul' conversion
sed -i "s/\bset //gI" $files # 'set' conversion
sed -i "s/\btype /cat /gI" $files # 'type' conversion
sed -i "s/\bfind /grep /gI" $files # 'find' conversion
sed -i "s/\bdel\(ete\)\? /rm /gI" $files # 'del' conversion

我在这里发布了这个脚本: https://github.com/meshr-net/helpers/blob/master/bat2sh.sh