By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,320 Members | 2,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,320 IT Pros & Developers. It's quick & easy.

Using Python instead of DOS shell scripts

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

Thanks, Jeff
Jul 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
On 2004-09-14, Jeff Wagner <JW*****@hotmail.com> wrote:
I have a project of converting numerous DOS cmd shell scripts
to Python. Is there a tutorial to getting started?


Yes.

Try looking at www.python.org.

Or maybe google for "python tutorial"?

--
Grant Edwards grante Yow! Is it FUN to be
at a MIDGET?
visi.com
Jul 18 '05 #2

P: n/a
Hello Jeff,
I have a project of converting numerous DOS cmd shell scripts to Python. Is there a tutorial to
getting started?

Just read the tutorial at http://docs.python.org/tut/tut.html

Shana Tova.
--
------------------------------------------------------------------------
Miki Tebeka <mi*********@zoran.com>
http://tebeka.spymac.net
The only difference between children and adults is the price of the toys







Jul 18 '05 #3

P: n/a
* 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/..._xhtml_115.zip
http://www.g2swaroop.net/files/byte/...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...py.html.tar.gz
http://www.ibiblio.org/obp/thinkCSpy/dist/thinkCSpy.pdf

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

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

* Handbook of the Physics Computing Course (Python language)
http://www.chemistry.ucsc.edu/~wgsco...a/handbook.pdf
Jul 18 '05 #4

P: n/a
Jeff Wagner wrote:
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
Jul 18 '05 #5

P: n/a
>>>>> "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.
Jul 18 '05 #6

P: n/a
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.
Jul 18 '05 #7

P: n/a
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
---

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

Thanks, Jeff

Jul 18 '05 #8

P: n/a
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.


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
Jul 18 '05 #9

P: n/a
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
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.