On Tue, 14 Sep 2004 06:00:56 -0500, John Hunter <jd******@ace.bsd.uchicago.edu> wrotf:
>> "Jeff" == Jeff Wagner <JW*****@hotmail.com> writes:
Jeff> I have a project of converting numerous DOS cmd shell
Jeff> scripts to Python. Is there a tutorial to getting started?
It might be a good idea to post one of the bat scripts you want to
convert. The resident gurus might show you a python equivalent, which
you could then study for inspiration.
Here is one of the scripts:
SETLOCAL
REM Check that we have enough command line arguments
IF [%4]==[] (
echo.Expecting 4 command line arguments: {INPUT_PREFIX} {OUTPUT_PREFIX} {INPUT_PATH} {OUTPUT_PATH}
GOTO :EOF
)
REM Pick up the environment variables that define the current operational
REM environment.
REM
REM NOTE: This line assumes that your current working directory is the
REM scripts directory!!
call .\Environment.cmd
REM read the configuration parameters
REM
set INPUT_PREFIX=%1
shift
set OUTPUT_PREFIX=%1
shift
set INPUT_PATH=%1
shift
set OUTPUT_PATH=%1
shift
REM VAR_DESIGNATOR is the symbol that surrounds a variable to be substituted in
REM the configuration files.
set VAR_DESIGNATOR=#
REM This variable holds the names of the strings that will be replaced in the
REM files. Each name must also correspond to a variable set above.
set ENV_NAMES=NAME INSTALLDIR SESSION BGIF SPFTNAME
REM This is the list of files that need to have substitutions done
set FILES=dConfig.xml rConfig.xml pConfig.xml FMConfig_local.xml FMConfig_NA.xml
REM the directory where scripts live (namely, the replaceStringInFile.pl
REM script)
set SCRIPTS_DIR=.\
REM generaet a random number to help distinguish my temporary files from other
REM processes
set MYRAND=%RANDOM%
REM First substitute the computer names in all the files
FOR %%F IN (%FILES%) DO (
IF EXIST %INPUT_PATH%.\%INPUT_PREFIX%%%F. (
REM Make an initial copy of the file to start from, as-is
copy /Y %INPUT_PATH%.\%INPUT_PREFIX%%%F %OUTPUT_PATH%.\%OUTPUT_PREFIX%%%F > nul
call :RESET_COUNTER
FOR %%S in (%COMPUTER_NAMES%) DO (
REM figure out what computer we are substituting for
call :INC_COUNTER
call :REPLACE_CURRENT_COMPUTER %%S %%F
REM delete the existing destination file, if it exists
del /Q %OUTPUT_PATH%.\%OUTPUT_PREFIX%%%F
REM rename/move the temporary file
move /Y %TEMP%.\%OUTPUT_PREFIX%%%F%MYRAND% %OUTPUT_PATH%.\%OUTPUT_PREFIX%%%F
)
)
REM else file does not exist
)
REM Substitute the environment variables in ENV_NAMES.
FOR %%F IN (%FILES%) DO (
IF EXIST %OUTPUT_PATH%.\%OUTPUT_PREFIX%%%F. (
REM (This loop assumes that all the files in FILES already exist in the
REM OUTPUT_PATH, since the prior loop put them there.)
FOR %%E in (%ENV_NAMES%) DO (
CALL :SET_ENV_VALUE %%E
CALL :REPLACE_ENV_VALUE %%E %%F
REM delete the original file
del /Q %OUTPUT_PATH%.\%OUTPUT_PREFIX%%%F
REM rename the temporary file
move /Y %TEMP%.\%OUTPUT_PREFIX%%%F%MYRAND% %OUTPUT_PATH%.\%OUTPUT_PREFIX%%%F
)
)
REM else file does not exist-skip it
)
ENDLOCAL
GOTO :EOF
REM ************************************************** **************************
REM Because the command shell expands the for loops only once, anything that
REM varies from one loop execution to the next has to be contained in a
REM separate subroutine. The following subroutines are, therefore, called from
REM within the loops above.
REM ************************************************** **************************
:RESET_COUNTER
set COMPUTER_COUNTER=0
GOTO :EOF
:INC_COUNTER
set /A COMPUTER_COUNTER+=1
set COMPUTER_KEYWORD=COMPUTER%COMPUTER_COUNTER%
REM i.e., exit the subroutine
GOTO :EOF
:REPLACE_CURRENT_COMPUTER
%SCRIPTS_DIR%.\replaceStringInFile.pl %VAR_DESIGNATOR%%COMPUTER_KEYWORD%%VAR_DESIGNATOR% %1
%OUTPUT_PATH%.\%OUTPUT_PREFIX%%2 %TEMP%.\%OUTPUT_PREFIX%%2%MYRAND%
GOTO :EOF
:SET_ENV_VALUE
REM Get value of %%1
echo.set ENV_VALUE=%%%1%% >%TEMP%.\setENV_VALUE%MYRAND%.bat
call %TEMP%.\setENV_VALUE%MYRAND%.bat
REM Clean up after myself
del /Q %TEMP%.\setENV_VALUE%MYRAND%.bat
GOTO :EOF
:REPLACE_ENV_VALUE
REM do substitution, placing results in a temporary file
%SCRIPTS_DIR%.\replaceStringInFile.pl %VAR_DESIGNATOR%%1%VAR_DESIGNATOR% %ENV_VALUE%
%OUTPUT_PATH%.\%OUTPUT_PREFIX%%2 %TEMP%.\%OUTPUT_PREFIX%%2%MYRAND%
GOTO :EOF