2013-01-22 129 views
0

我有以下的Windows批处理脚本(.bat文件)。我想将其转换成Linux shell脚本。请帮助...转换的Windows批处理脚本的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 
+0

我不知道Linux的shell脚本线索...我的经理给了我3个小时提交脚本壳.... – user1991965

+0

这似乎是一个不现实的最后期限,如果你还没有得到任何关于它的知识... – GolezTrol

+0

Linux文档项目对Bash shell中的脚本进行了一些介绍。 [击指南初学者](http://tldp.org/LDP/Bash-Beginners-Guide/html/index.html)和[Advanced Bash脚本编程指南(http://tldp.org/LDP/abs/ HTML/index.html的) – Martin

回答

1

我不是在Bash很棒(我试图转换之前,最终把它变成一个EXE,要求人们得到Wine Compatibility layer),但我相信我知道你的问题的一部分。你不能在bash脚本中使用标签(:example)和goto,(例如)。你必须使用功能。你的情况:

#!/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是一个伟大的指南将批量壳牌。它并没有帮助我很多,但我认为你会发现它很有用。

此外,什么是你想用这个程序来完成?

+0

有用的链接,谢谢..批处理文件的例子很有偏见(它可能会更好)。就我个人而言,我喜欢将'call:target','setlocal'和'goto:eof'组合起来用于sandbox我的批处理“函数”。但是,他们的比较表是有用的。当我开始转换我的一个更复杂的开发脚本时,我会利用这些。 – ZaLiTHkA