将Windows批处理脚本转换为Linux Shell脚本

将Windows批处理脚本转换为Linux Shell脚本,linux,shell,batch-file,Linux,Shell,Batch File,我有以下Windows批处理脚本(.bat文件)。我想将其转换为Linux shell脚本。请帮忙 @echo off setlocal for /f "tokens=2-7 delims=_.-" %%A in ('dir /B TACOS_*') do ( setlocal enabledelayedexpansion call :getmonth %%B ren TACOS*_*%%A-%%B-%%C*_*%%D-%%E-%%F_UTC.csv TACOS_%%A!mon!%%

我有以下Windows批处理脚本(.bat文件)。我想将其转换为Linux shell脚本。请帮忙

@echo off
setlocal
for /f "tokens=2-7 delims=_.-" %%A in ('dir /B TACOS_*') do (
  setlocal enabledelayedexpansion
  call :getmonth %%B
  ren TACOS*_*%%A-%%B-%%C*_*%%D-%%E-%%F_UTC.csv TACOS_%%A!mon!%%C_%%D%%E%%F.csv
  endlocal
)

:getmonth
if "%1" equ "Jan" set mon=01
if "%1" equ "Feb" set mon=02
if "%1" equ "Mar" set mon=03
if "%1" equ "Apr" set mon=04
if "%1" equ "May" set mon=05
if "%1" equ "Jun" set mon=06
if "%1" equ "Jul" set mon=07
if "%1" equ "Aug" set mon=08
if "%1" equ "Sep" set mon=09
if "%1" equ "Oct" set mon=10
if "%1" equ "Nov" set mon=11
if "%1" equ "Dec" set mon=12
goto :eof
endlocal
以下是我迄今为止所尝试的:

#!/bin/bash
set +v

for -f "tokens=2-7 delims=_.- A in ('Is-I -B TACOS_*'); do (
    getmonth B
    mv -iv TACOS*_*A-B-C*_*D-E-F_AST.csv TACOS_A!mon!C_DEF.csv
)


:getmonth
if "$1" equ "Jan" then mon=01
if "$1" equ "Feb" then mon=02
if "$1" equ "Mar" then mon=03
if "$1" equ "Apr" then mon=04
if "$1" equ "May" then mon=05
if "$1" equ "Jun" then mon=06
if "$1" equ "Jul" then mon=07
if "$1" equ "Aug" then mon=08
if "$1" equ "Sep" then mon=09
if "$1" equ "Oct" then mon=10
if "$1" equ "Nov" then mon=11
if "$1" equ "Dec" then mon=12
goto :eof

这是我到目前为止所做的尝试

#!/bin/bash
set +v

for -f "tokens=2-7 delims=_.- A in ('Is-I -B TACOS_*'); do (
    getmonth B
    mv -iv TACOS*_*A-B-C*_*D-E-F_AST.csv TACOS_A!mon!C_DEF.csv
)


:getmonth
if "$1" equ "Jan" then mon=01
if "$1" equ "Feb" then mon=02
if "$1" equ "Mar" then mon=03
if "$1" equ "Apr" then mon=04
if "$1" equ "May" then mon=05
if "$1" equ "Jun" then mon=06
if "$1" equ "Jul" then mon=07
if "$1" equ "Aug" then mon=08
if "$1" equ "Sep" then mon=09
if "$1" equ "Oct" then mon=10
if "$1" equ "Nov" then mon=11
if "$1" equ "Dec" then mon=12
goto :eof
我不擅长Bash(我以前尝试过转换;最后将它转换为exe,要求人们获取),但我相信我知道你的部分问题。在bash脚本中不能使用标签(:example)和goto(goto example)。你必须使用函数。就你而言:

#!/bin/bash
set +v

for -f "tokens=2-7 delims=_.- A in ('Is-I -B TACOS_*'); do (
    getmonth() B
    mv -iv TACOS*_*A-B-C*_*D-E-F_AST.csv TACOS_A!mon!C_DEF.csv
)


function getmonth()
{
if "$1" equ "Jan" then mon=01
if "$1" equ "Feb" then mon=02
if "$1" equ "Mar" then mon=03
if "$1" equ "Apr" then mon=04
if "$1" equ "May" then mon=05
if "$1" equ "Jun" then mon=06
if "$1" equ "Jul" then mon=07
if "$1" equ "Aug" then mon=08
if "$1" equ "Sep" then mon=09
if "$1" equ "Oct" then mon=10
if "$1" equ "Nov" then mon=11
if "$1" equ "Dec" then mon=12
eof()
}
是将批处理转换为Shell的极好指南。这对我帮助不大,但我想你会发现它很有用


另外,你想用这个程序实现什么?

我对linux shell脚本一无所知。。。我的经理给了我3个小时在shell中提交脚本……如果你对它一无所知,那么这似乎是一个不切实际的最后期限……Linux文档项目对Bash shell中的脚本编写有一些介绍。@GolezTrol请看下面我试图转换的代码……您需要弄清楚批处理文件在做什么,然后弄清楚如何在Linux shell(很可能是bash)中实现这一点。你不能只是逐行替换斜杠到破折号和
%
$
然后期望它工作。。。如果仅通过运行批处理脚本无法判断它在做什么,请从Windows命令行键入
help for
help set
并阅读这些帮助页面。另外,您的意思是
ls-l
,而不是
Is-I
。。。但是你真正想要的只是
ls
而不是
-l
。有用的链接,谢谢。。批处理文件示例有很大的偏差(可能更好)。就个人而言,我喜欢结合使用
call:target
setlocal
goto:eof
来沙箱我的批处理“函数”。然而,比较表是有用的。当我开始转换我的一个更复杂的开发脚本时,我将使用这些脚本。