Page 1 of 1

Bash und rc.xml

Posted: Sun 13 Jan 2013, 18:56
by RSH
Hi.

I want to configure the rc.xml file from openbox wm, actually adding key bindings to switch desktop from keyboard. I know how and where to insert the code into the rc.xml file manually.

But I want to oi this from bash script.

So, I need to read the whole content of the rc.xml file until xml tag <keyboard> was found. Then I need to insert my own code and after this I need to get the rest of the rc.xml file to build up the new increased file.

I could do this without any help from you experts, using a loop, reading line by line until <keyboard> is found, echo my code and reading the rest of the rc.xml file line by line.

I'm just asking for a less code solution, maybe a two liner or something like that?

Thanks,

RSH

Would you please have a look, here: http://murga-linux.com/puppy/viewtopic. ... 293#677293

Re: Bash und rc.xml

Posted: Sun 13 Jan 2013, 19:12
by L18L
RSH wrote:Hi.

I want to configure the rc.xml file from openbox wm, actually adding key bindings to switch desktop from keyboard. I know how and where to insert the code into the rc.xml file manually.

But I want to oi this from bash script.

So, I need to read the whole content of the rc.xml file until xml tag <keyboard> was found. Then I need to insert my own code and after this I need to get the rest of the rc.xml file to build up the new increased file.

I could do this without any help from you experts, using a loop, reading line by line until <keyboard> is found, echo my code and reading the rest of the rc.xml file line by line.

I'm just asking for a less code solution, maybe a two liner or something like that?

Thanks,

RSH

Would you please have a look, here: http://murga-linux.com/puppy/viewtopic. ... 293#677293
try

Code: Select all

sed -i 's/<keyboard>/<keyboard>my code/' rc.xml

Posted: Sun 13 Jan 2013, 20:49
by RSH
try
Code:
sed -i 's/<keyboard>/<keyboard>my code/' rc.xml
Thanks, but did not work. I did try to insert the code directly into your code

Code: Select all

sed -i 's/<keyboard>/<keyboard>'<keybind key="A-1"><action name="Desktop"><desktop>1</desktop></action></keybind><keybind key="A-2"><action name="Desktop"><desktop>2</desktop></action></keybind><keybind key="A-3"><action name="Desktop"><desktop>3</desktop></action></keybind><keybind key="A-4"><action name="Desktop"><desktop>4</desktop></action></keybind>/' /root/Desktop/rc.xml
and tried to read it from file.

Code: Select all

MyCode=`cat /root/Desktop/mycode.xml`
sed -i 's/<keyboard>/<keyboard>$MyCode/' /root/Desktop/rc.xml

Code: Select all

MyCode=`cat /root/Desktop/mycode.xml`
sed -i 's/<keyboard>/<keyboard>'$MyCode'/' /root/Desktop/rc.xml

Code: Select all

MyCode=`cat /root/Desktop/mycode.xml`
sed -i 's/<keyboard>/<keyboard>"$MyCode"/' /root/Desktop/rc.xml
Nothing works

Posted: Sun 13 Jan 2013, 21:11
by SFR
Hey Rainer

You need to escape all / occurrences; also you can add a few \n to make the result more readable:

Code: Select all

sed -i 's/<keyboard>/&\n<keybind key="A-1"><action name="Desktop"><desktop>1<\/desktop><\/action><\/keybind>\n<keybind key="A-2"><action name="Desktop"><desktop>2<\/desktop><\/action><\/keybind>\n<keybind key="A-3"><action name="Desktop"><desktop>3<\/desktop><\/action><\/keybind>\n<keybind key="A-4"><action name="Desktop"><desktop>4<\/desktop><\/action><\/keybind>/' /root/Desktop/rc.xml
(btw: & instead of the second <keyboard> will simply leave it unchanged and append the rest of string to it.)

Greetings!

Posted: Sun 13 Jan 2013, 21:48
by seaside
RSH,

Use | as a separator, otherwise you'll have to escape everything.

Code: Select all

sed  's|<keyboard>|<keyboard><keybind key="A-1"><action name="Desktop"><desktop>1</desktop></action></keybind><keybind key="A-2"><action name="Desktop"><desktop>2</desktop></action></keybind><keybind key="A-3"><action name="Desktop"><desktop>3</desktop></action></keybind><keybind key="A-4"><action name="Desktop"><desktop>4</desktop></action></keybind>|' 
Cheers,
s

Posted: Sun 13 Jan 2013, 21:55
by RSH
SFR wrote:Hey Rainer

You need to escape all / occurrences; also you can add a few \n to make the result more readable:
Uuhh yeah!

The escapes. I've had this once in a bash script with jpeps. This is weird stuff, if one doesn't really know or understand, how to use this.

However, thank you for the useful (as always) code. Will play with it and surely reach some state of success - later.

Meanwhile I have used my "Pascal Working-Path" and solved it for testings with this code: :lol:

Code: Select all

MyCode=`cat /root/Desktop/mycode.xml`

---


INSERT="true"
while read LINE
do
	echo "$LINE" >> /root/Desktop/new_rc.xml
	if [[ "$LINE" == *keyboard* ]]; then
		if [ "$INSERT" = "true" ]; then
			INSERT="false"
			echo "$MyCode" >> /root/Desktop/new_rc.xml
			sleep .1
		fi
	fi
done < /root/Desktop/rc.xml
:lol:
seaside wrote:RSH,

Use | as a separator, otherwise you'll have to escape everything.
Yes, thanks. This is a very useful extension to my bash knowledge. I've always wondered why sometimes "/" is used and sometimes "|" is used in such sed codes?

Again, thanks to all.