简体   繁体   English

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

[英]Convert Windows Batch Script to Linux Shell Script

I have the following Windows batch script (.bat file). 我有以下Windows批处理脚本(.bat文件)。 I would like to convert this into a Linux shell script. 我想将其转换为Linux Shell脚本。 Please help... 请帮忙...

@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

Here is what I have tried so far: 到目前为止,这是我尝试过的:

#!/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

here is what i have tried so far 到目前为止,这是我尝试过的

#!/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

I'm not great at Bash (I've tried to convert before; ended up turning it into an exe, asking people to get Wine Compatibility layer ) but I believe I know part of your problem. 我对Bash不太擅长(我曾尝试进行转换;最终将其转换为exe文件,要求人们获取Wine Compatibility层 ),但我相信我知道您遇到的问题的一部分。 You cannot use labels (:example) and goto, (goto example) in bash scripting. 您不能在bash脚本中使用标签(:example)和goto(goto示例)。 You have to use functions. 您必须使用函数。 In your case: 在您的情况下:

#!/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()
}

Here is a great guide to converting Batch to Shell. 是将Batch转换为Shell的绝佳指南。 It hasn't helped me a lot, but I think you'll find it useful. 它对我没有多大帮助,但我认为您会发现它很有用。

Also, what are you trying to accomplish with this program? 另外,您想用此程序完成什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM