我想运行一个 .sh 脚本来重命名desktop/reports/don
我计算机目录中的文件。我需要的一个例子是重命名:
TACOS_2013-Jan-22__00-50-00_UTC.csv
至
TACOS_20130122_005000.csv
我有以下使用 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
month["Jan"]=01
month["Feb"]=02
month["Mar"]=03
month["Apr"]=04
month["May"]=05
month["Jun"]=06
month["Jul"]=07
month["Aug"]=08
month["Sep"]=09
month["Oct"]=10
month["Nov"]=11
month["Dec"]=12
directory="desktop/reports/Don/"
for path in "${directory}TACOS_"*; do
path=${path#${directory}}
newpath=${path:0:10}${month[${path:11:3}]}${path:15:2}
newpath=${newpath}__$(tr -d '-' <<< ${path:19:8}).csv
echo "${directory}${path}" "${directory}${newpath}" # Run this one first!!!
# mv "YOUR/PATH/${path}" "YOUR/PATH/${newpath}"
done