x
ETF2L is looking for new Staff to recruit! Are you interested in supporting the league? Then click here for more details on what we can offer and how to apply! 

Forum

Batch file help (programming kinda)

Created 13th September 2011 @ 16:01

Add A Reply Pages: 1

AcidReniX

RaWr ::

Sorry for the lame topic, but I figured there’s a number of smart people who come on here and might be able to help me.

I’m trying to make a batch file that I can paste a block of text into, and it assigns certain parts of it into variables.

The boring background would be that I have a cpanel server and when I create an account, it emails me the details in the following format:

+===================================+
| New Account Info |
+===================================+
| Domain: bob.com
| Ip: 123.132.123.123 (n)
| HasCgi: y
| UserName: bobusr
| PassWord: KASd9k290
| CpanelMod: x3
| HomeRoot: /home
| Quota: 200 Meg
| NameServer1: ns1.bobsnameservers.com
| NameServer2: ns2.bobsnameservers.com
| NameServer3:
| NameServer4:
| Contact Email: [email protected]
| Package: Starter
| Feature List: default
| Language: en
+===================================+

I just want to be able to copy this whole block, paste it into a command prompt, and have it collect the following, each into a variable:

Domain name
IP
Username
Password

Other things are optional.
I imagine the easiest way to do this, would be to filter out the first rubbish lines, and then do some regular expressions to eliminate the:

| blahblahblah:

The number of charactors will always be the same before the actual data that I need so I guess the easiest way is to get any information on each line, after a certain number of charactors.

I think it would be pretty easy, but I’m not great at these!
If no one can help then it’s not the end of the world, but thought i’d ask incase some guru is randomly reading and doesn’t mind typing out a couple of lines for me :)

Thank you guys!
Arx

(I don’t really mind if it was in another language, say, PHP or something, but batch files are generally what I use for this kind of things as they arem well…. rediculously easy).

Spike Himself

TC

Assuming you don’t mean to have a DOS batch file, awk and sed will probably do the job fairly easily (needs more Chris in here!!). I’m not really familiar with either sed or awk, so I brew this little piece of shit together for you. No guarantee it will work, not tested at all.

#!/bin/sh

FIELDS="Domain: Ip: UserName: PassWord:"

while [ ! "EOF" -eq "${INPUT}" ]
do
read INPUT
FIELD=`echo ${INPUT} | awk '{print $2}'`
for F in ${FIELDS}
do
if [ ${F} -eq ${FIELD} ]
then
DATA="${DATA}n${INPUT#*:}"
fi
done
done

echo ${DATA}

Once again, 0 guarantee it will work. In fact it probably won’t. But on the off chance it does, you’re welcome :D

edit; hurray for code tags, shame about the lack of indentation though :(
edit2; alright it’s not going to be readable. might work if you quote my post and copy from there..


Last edited by Spike Himself,

octochris

(0v0)

Quoted from Spike Himself

Assuming you don’t mean to have a DOS batch file, awk and sed will probably do the job fairly easily (needs more Chris in here!!). I’m not really familiar with either sed or awk, so I brew this little piece of shit together for you. No guarantee it will work, not tested at all.

#!/bin/sh

FIELDS="Domain: Ip: UserName: PassWord:"

while [ ! "EOF" -eq "${INPUT}" ]
do
read INPUT
FIELD=`echo ${INPUT} | awk '{print $2}'`
for F in ${FIELDS}
do
if [ ${F} -eq ${FIELD} ]
then
DATA="${DATA}n${INPUT#*:}"
fi
done
done

echo ${DATA}

Once again, 0 guarantee it will work. In fact it probably won’t. But on the off chance it does, you’re welcome :D

edit; hurray for code tags, shame about the lack of indentation though :(
edit2; alright it’s not going to be readable. might work if you quote my post and copy from there..

about 75% of those lines are bugs, nice :D

here’s a quick one in bash. i know you said batch, but i assume spike knows something i don’t about what you’re actually asking for.

http://sprunge.us/iKai

Output:

$ ./acidcpanel << EOF
> +===================================+
> | New Account Info |
> +===================================+
> | Domain: bob.com
> | Ip: 123.132.123.123 (n)
> | HasCgi: y
> | UserName: bobusr
> | PassWord: KASd9k290
> | CpanelMod: x3
> | HomeRoot: /home
> | Quota: 200 Meg
> | NameServer1: ns1.bobsnameservers.com
> | NameServer2: ns2.bobsnameservers.com
> | NameServer3:
> | NameServer4:
> | Contact Email: [email protected]
> | Package: Starter
> | Feature List: default
> | Language: en
> +===================================+
> EOF
The domain is: bob.com
The IP is: 123.132.123.123
The username is: bobusr
The password is: KASd9k290


Last edited by octochris,

Spike Himself

TC

Quoted from octochris

i know you said batch, but i assume spike knows something i don’t about what you’re actually asking for.

Quoted from Spike Himself

Assuming you don’t mean to have a DOS batch file […]

also fu :(

AcidReniX

RaWr ::

Thanks guys,
I was actually asking for a batch, not a bash file. The way I would have gone about it is to create a batch file on my desktop, run it, copy that block of text into it and it would echo out a HTML code into a HTML file using those values to fill in the blanks. Can then copy and paste the outputted website into an email and bam!

There will be better ways to do it though. I don’t know much about bash files but I can generally work with anything (as long as it’s supported on Windows ;p), so I’ll have a play with that one.

Thanks for your help guys!

Add A Reply Pages: 1