Page 1 of 1

DOS Batch files

Posted: Thu Aug 24, 2006 12:54 am
by Neo
Has anyone scripted around with DOS batch files?

I am trying to assign the output of a command to a a variable but have not been able to do so.
Here is what I was trying out

Code: Select all

set MY_VAR=`ipconfig | find "IP Address"`
but this does not get set.
Any ideas on how to do this would be great.

Re:DOS Batch files

Posted: Thu Aug 24, 2006 2:13 am
by Pype.Clicker
the "help" program should tell you much about all this... i'm not sure there's the equivalent of backquotes in MS-DOS, but there should be a way to retrieve information from the last executed program ... though i'm unsure of whether this is restricted to the "return code" or if it also applies to the output.

At worst, there is probably a way to prompt a specific item from the user, and you could easily pipe the output of your statement to that "input-and-set-env-variable" program ...

Re:DOS Batch files

Posted: Thu Aug 24, 2006 7:20 am
by Neo
Ok I tried to get this info from the 'ipconfig' but am not able to extract the IP in DOS.

What I tried is

Code: Select all

ipconfig | find "IP Address" | f:\cygwin\bin\sed -e 's/.*: //'
which gave me the IP, but when I tried to assign this to a DOS environment variable using

Code: Select all

set MYVAR="ipconfig | find 'IP Address' | f:\cygwin\bin\sed -e 's/.*: //'"
it gave me
MYVAR="ipconfig | find 'IP Address' | f:\cygwin\bin\sed -e 's/.*: //'"
Any ideas with this would be really great.

Re:DOS Batch files

Posted: Thu Aug 24, 2006 8:12 am
by Neo
Ok I ended up writing that output to a file and reading from it. :)
Anyway I am trying to append the string ":0.0" to the string from the file but dont know how to do this.

Anyone know how to append the string ":0.0" to the another string in DOS?

Re:DOS Batch files

Posted: Thu Aug 24, 2006 8:33 am
by Pype.Clicker
i'd say the easiest way around would be to do it in your "sed" command ... e.g. tell it to replace the last character with ":0.0" :)

Otherwise, you could try to

Code: Select all

echo ":0.0" >>the_file_with_address
(hoping that concatenation redirection works in DOS)

Re:DOS Batch files

Posted: Thu Aug 24, 2006 9:02 am
by Neo
That DOS method is crazy it appends ":0.0" and anyway it puts it in the next line.

How do I do that with sed?

Re:DOS Batch files

Posted: Thu Aug 24, 2006 9:39 am
by Neo
Ok managed to do that by using SET twice
The second time with

Code: Select all

SET VAR=%VAR%:0.0
Anyway would still like to know how to do it with sed?

Thanks for all the help.

Re:DOS Batch files

Posted: Thu Aug 24, 2006 9:40 am
by Pype.Clicker
your sed statement says
s/ (substitute) .*: (everything before ':') // (with nothing) ...

Instead you should say "substitute '<anything> : <address>' with '<address>:0.0'

s/regexp/replacement/
Attempt to match regexp against the pattern space. If successful, replace that
portion matched with replacement. The replacement may contain the special charac-
ter & to refer to that portion of the pattern space which matched, and the special
escapes \1 through \9 to refer to the corresponding matching sub-expressions in
the regexp.
If you know enough of regexps, then ".*: ([0-9\.]*)" should be clearly <anything> : <IP> for you.

That suggests me 's/.*: \([0-9\.]*\)/\1:0.0/' should do the trick :P


http://www.gnoppix.org/pages/rute/node11.html

Re:DOS Batch files

Posted: Thu Aug 24, 2006 9:53 am
by Pype.Clicker
oh, and just out of curiousity, how did you throw the content of a file into an environment variable ??

Re:DOS Batch files

Posted: Thu Aug 24, 2006 9:23 pm
by B.E
Can I ask you why don't you use cygwin win to do this. moreover you use sed from cygwin which needs a the cygwin dll anyway so why not use the enviroment. From what I can see you are trying to automate adding the display number to an the host's ip adress. Which means that you need a X server running. Cygwin has a X server that you can use for this.

Re:DOS Batch files

Posted: Fri Aug 25, 2006 1:18 am
by Neo
Pype.Clicker wrote: oh, and just out of curiousity, how did you throw the content of a file into an environment variable ??
That was an interesting obstacle which I solved with help from the net. Here is how you do that

Code: Select all

FOR /F %%x in ('TYPE F:\CYGWIN\ipaddress.txt') DO SET MYVAR=%%x
Crude but effective. I tried piping the commands but was unable to do that hence the need for a file.

Re:DOS Batch files

Posted: Fri Aug 25, 2006 8:47 am
by Neo
B.E wrote: Can I ask you why don't you use cygwin win to do this. moreover you use sed from cygwin which needs a the cygwin dll anyway so why not use the enviroment. From what I can see you are trying to automate adding the display number to an the host's ip adress. Which means that you need a X server running. Cygwin has a X server that you can use for this.
Yes that is what I thought of doing at first, but thought it would be better to do it at CYGWIN start from DOS rather than making it shell specific (like .bashrc/.cshrc)