Solved

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

Posted on 2008-10-30
8
729 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 38

Assisted Solution

by:Shift-3
Shift-3 earned 230 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Utilizing an array to gracefully append to a list of EmailAddresses
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

12 Experts available now in Live!

Get 1:1 Help Now