Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-30
8
Medium Priority
?
749 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 920 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 1080 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
Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 1080 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 920 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 1080 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 1080 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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…
Suggested Courses

718 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