Using Python instead of DOS shell scripts

J

Jeff Wagner

I have a project of converting numerous DOS cmd shell scripts to Python. Is there a tutorial to
getting started?

Thanks, Jeff
 
T

Thorsten Kampe

* Jeff Wagner (2004-09-14 06:56 +0200)
I have a project of converting numerous DOS cmd shell scripts to Python. Is there a tutorial to
getting started?

There are no tutorials for this special task. The appropriate
replacement for a batch script would be a shell script unless you want
/much/ more clearity and functionality.

[Tutorials]
* A Byte of Python
http://www.python.g2swaroop.net/byte/index.html
http://www.g2swaroop.net/files/byte/byte_of_python_xhtml_115.zip
http://www.g2swaroop.net/files/byte/byte_of_python_xhtml_single_115.zip

* Non-Programmers Tutorial For Python
http://www.honors.montana.edu/~jjc/easytut/easytut/
http://www.honors.montana.edu/~jjc/easytut/easytut.zip
http://www.honors.montana.edu/~jjc/easytut/easytut.pdf

* Learning to Program
http://www.freenetpages.co.uk/hp/alan.gauld/tutor2/
http://www.freenetpages.co.uk/hp/alan.gauld/tutor.tgz

* How to Think Like a Computer Scientist - Learning with Python
http://www.ibiblio.org/obp/thinkCSpy/
http://www.ibiblio.org/obp/thinkCSpy/dist/thinkCSpy.html.tar.gz
http://www.ibiblio.org/obp/thinkCSpy/dist/thinkCSpy.pdf

* Python 101
http://www.devshed.com/c/a/Python/Python-101-part-1-Snake-Eyes/

* Dive Into Python
http://www.diveintopython.org/toc.html
http://diveintopython.org/download/diveintopython-html-5.0.zip
http://diveintopython.org/download/diveintopython-html-flat-5.0.zip
http://diveintopython.org/download/diveintopython-pdf-5.0.zip

* Handbook of the Physics Computing Course (Python language)
http://www.chemistry.ucsc.edu/~wgscott/courses/chem200a/handbook.pdf
 
D

Daniel Dittmar

Jeff said:
I have a project of converting numerous DOS cmd shell scripts to Python. Is there a tutorial to
getting started?

Some of the more important modules for your task are probably
- os: http://www.python.org/doc/2.3.4/lib/module-os.html
- os.path: http://www.python.org/doc/2.3.4/lib/module-os.path.html
- win32 extensions: https://sourceforge.net/projects/pywin32/

You should visit the Python Cookbook
(http://aspn.activestate.com/ASPN/Python/Cookbook/), which is also
available in book form.

Daniel
 
J

John Hunter

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.
 
H

haddock

I am just getting started myself and can really recommend "learning
python" by Lutz and Aescher (O'Reilly). This book really starts at the
beginning, covers several OS, and really explains well. I only knew
some Java before, and have never been great at command line things, so
this book really helped me a lot. If you are already a pro with
computers and other languages it might be too detailed.
 
B

Byron

Hi Jeff,

Welcome to Python programming!

If you want to learn Python quickly and easily, I would HIGHLY recommend
that you check out "How to think like a computer scientist: Learning
with Python." It is a free, downloadable book that teaches Python from
the ground up. It also contains many fun, step-by-step examples for the
beginner to try out -- along with an object-oriented approach later in
the book.

Check it out:

http://www.greenteapress.com/thinkpython/

Byron
 
J

Jeff Wagner

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.

Thanks for all the good info. I think the best place for me to start is to write a small program
which will launch the DOS cmd scripts which already exist. Is there a way to launch the file located
at this location?

e:\scripts\CleanLogFiles.cmd

Also, some of these scripts exist on a different server than the one I have Python running on. Is
there a way to log onto another server via an TCP/IP address and launch DOS cmd scripts there?

In other words e:\scripts\CleanLogFiles.cmd might be on server located at 10.75.211.15 and my
workstation is located on 155.156.25.6

I will next post an example of one of the scripts.

Jeff
 
J

Jeff Wagner

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top