• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 754
  • Last Modified:

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

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
jl66
Asked:
jl66
  • 4
  • 2
  • 2
6 Solutions
 
Shift-3Commented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
jl66Author Commented:
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Steve KnightIT ConsultancyCommented:
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
 
Shift-3Commented:
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
 
Steve KnightIT ConsultancyCommented:
Never thought of using the substitution %x:A=b% in some of those ways.... one for the bookmarks, thanks :-)

Steve
0
 
jl66Author Commented:
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
 
Steve KnightIT ConsultancyCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now