Solved

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

Posted on 2008-10-30
8
742 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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
 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
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…
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…
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…

751 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