0

I have the following Windows batch script (.bat file). I would like to convert this into a Linux shell script. 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
6
  • i have no clue about linux shell script... My managers has given me 3 hours to submit the script in shell.... Commented Jan 22, 2013 at 14:46
  • That seems an unrealistic deadline if you haven't got any knowledge about it... Commented Jan 22, 2013 at 15:27
  • The Linux Documentation project has a couple of introductions to scripting in the Bash shell. Bash Guide for Beginners and Advanced Bash-Scripting Guide Commented Jan 22, 2013 at 16:32
  • @GolezTrol Please see the code below i have tried to convert... Commented Jan 22, 2013 at 17:21
  • You need to figure out what the batch file is doing, and then figure out how to do that in your Linux shell (most likely bash). You can't just do a line-by-line replacement of slashes to dash and % to $ and expect it to work... If you can't tell what the batch script is doing just by running it, then from the Windows command line type help for and help set and read those help pages. Also, you meant ls -l, not Is-I... but what you really want is just ls without -l. Commented Jan 24, 2013 at 5:14

1 Answer 1

2

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. You cannot use labels (:example) and goto, (goto example) in bash scripting. 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. 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?

Sign up to request clarification or add additional context in comments.

1 Comment

Helpful link, thanks.. The batch file example is pretty biassed (and it probably could be better). Personally, I like to use a combination of call :target, setlocal and goto :eof to sandbox my Batch "functions". However, thee comparison tables are useful. I'll make use of those when I get around to converting one of my more complex dev scripts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.