2013-01-22 127 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