Solved

Convert Unix Script to Windows Batch

Posted on 2014-10-29
3
222 Views
Last Modified: 2014-11-03
I have the following script that gets invoked a program, it invokes the script, makes the changes, and then reads the file back into the program. The script works for Unix, but I now need to have the same type of script for window.

#!/bin/ksh
cp $1 /tmp/tmpfile.$$
sed -e 's/OWNER root/OWNER nobody/' /tmp/tmpfile.$$ > $1

Appreciate the assistance in advance.
0
Comment
Question by:PAH_in_MI
3 Comments
 
LVL 20

Expert Comment

by:n2fc
ID: 40411348
The UNIX "sed" command is the issue... Nothing native in DOS like it...

You can install the GnuWin32 package, though

See: http://gnuwin32.sourceforge.net/

And be able to run sed on the windows box... Then just change your script as follows:

copy %1 %TEMP%\tmpfile.$$
sed -e "s/OWNER root/OWNER nobody/" %TEMP%\tmpfile.$$ > %1

All you would need to do is download the "sed" component and place in a directory that is in your path...

Download EITHER:
1) The sed setup file:
http://sourceforge.net/projects/gnuwin32/files//sed/4.2.1/sed-4.2.1-setup.exe/download

or
2) The bin & dependent DLL's:
http://sourceforge.net/projects/gnuwin32/files//sed/4.2.1/sed-4.2.1-bin.zip/download

http://sourceforge.net/projects/gnuwin32/files//sed/4.2.1/sed-4.2.1-dep.zip/download
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40414678
You can't perform that simple replacement inside your application? Sounds strange.
The pure DOS Prompt way is:
@echo off
setlocal EnableDelayedExpansion
copy %1 %TEMP%\tmpfile.$$
(for /F "tokens=*" %%L in (%TEMP%\tmpfile.$$)  do (
  set line=%%L
  echo,line=!line:OWNER root=OWNER nobody!
)) > %1

Open in new window

But that can fail if certain characters are contained in the lines to parse. Usual recommendation is to use chgstr.exe from http://www.paulslore.com/index.php?option=com_content&task=view&id=49&Itemid=46
0
 

Author Closing Comment

by:PAH_in_MI
ID: 40419311
Thank you.
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question