Solved

export VAR1=$( hostname ) in Korn shell in Unix = set VAR1=??? in DOS?

Posted on 2008-10-30
8
731 Views
Last Modified: 2013-12-26
How to get SET variable command in DOS coming from the result by executing a command . For example,
want to get the result from
SC QUERY "PLUG AND PLAY" | FIND "STOPPED"
and then set it to a VAR1. Something like
set VAR1=( SC QUERY "PLUG AND PLAY" )
In Korn shell, we have VAR1=$( ps -ef|grep ORA ). How about in DOS?
0
Comment
Question by:jl66
  • 4
  • 2
  • 2
8 Comments
 
LVL 38

Accepted Solution

by:
Shift-3 earned 230 total points
ID: 22845308
You can use FOR /F to loop through the output of a command.  See here for syntax:
http://www.ss64.com/nt/for_cmd.html

This could be used to set a variable, as below, though you might want to play with the tokens parameter to grab a particular part of the output.  Change all instances of %G to %%G if including this in a batch script.


for /F %G in ('SC QUERY "PLUG AND PLAY" ^| FIND "STOPPED"') do set var1=%G

Open in new window

0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 270 total points
ID: 22845327
You can use the for command for this -- see for /? and set /? for details:

@echo off
for /f "tokens=* delims=" %%a in ('yourcommand ^| find "xyz"') do set var=%%a
echo Variable var is now = %var%

From a command line use %a instead of %%a.  You can also split up "tokens" to take part of the data from a line etc.  

Also as you can see with the pipe above if you need to use a pipe or > or < in the command then escape it with a circumflex character ^.

EE screen layout playing up at the moment so can't see what I'm typing so sorry if garbled at all!

hth

Steve
0
 

Author Comment

by:jl66
ID: 22847613
Thanks both experts for the info.
The further question is that  if I would like to loop each line in a file and change certain patterns in each line, how should I do it in DOS batch?
For example,
type test.txt
This is a test, which tests DOS commands.
In each line, change test to production.
.......
I expect to get the following:
This is a production, which productions DOS commands.
In each line, change production to production.
......
Is it possible to do it with DOS batch?
Thanks in advance.
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 270 total points
ID: 22848203
No problem.... we crossed posts a bit there as you can imagine...

You CAN do text file processing with a batch file like that BUT it is very easy to break it as it involves using a for loop to read a file a line at a time, process it and write it back out to a different file.  Very easy to break it if there any special characters in the file such as |<>" etc.

If it is a known quantity we can probably knock something up, though that should be a new question.  Otherwise would recommend finding an exe utility to do the search and replace or make a VBScript to loop throught the file then run it from batch with cscript //nologo yourvbscript.vbs

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 38

Assisted Solution

by:Shift-3
Shift-3 earned 230 total points
ID: 22849886
You can do rudimentary text substitution with variables in batch.
http://www.ss64.com/nt/syntax-replace.html

The script below will do what you request, though as dragon-it says, it will fail if there are special characters in the file.  VBScript is usually a better tool for this kind of task.  See this article for an example:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/feb05/hey0208.mspx


@echo off

setlocal enabledelayedexpansion
 

for /F "tokens=*" %%G in ('type "test.txt"') do (

 set line=%%G

 echo !line:test=production!>>newfile.txt

)

Open in new window

0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 270 total points
ID: 22849922
Never thought of using the substitution %x:A=b% in some of those ways.... one for the bookmarks, thanks :-)

Steve
0
 

Author Comment

by:jl66
ID: 22851720
Greatly appreciate both for the advice. My situation is kind of special.
Continue the question: Is there any way to change (replace) several words in a line via one write (>> newtest.txt)?
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 270 total points
ID: 22851994
You could just do two replaces updating the variable instead of writing it out on first replacement:
set line=%%G
 set line=!line:test=production!
 echo !line:another=somethingelse!>>newfile.txt

Steve
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now