Bash und rc.xml

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

Bash und rc.xml

#1 Post 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
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

Re: Bash und rc.xml

#2 Post 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

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#3 Post 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
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#4 Post 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!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#5 Post 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

User avatar
RSH
Posts: 2397
Joined: Mon 05 Sep 2011, 14:21
Location: Germany

#6 Post 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.
[b][url=http://lazy-puppy.weebly.com]LazY Puppy[/url][/b]
[b][url=http://rshs-dna.weebly.com]RSH's DNA[/url][/b]
[url=http://murga-linux.com/puppy/viewtopic.php?t=91422][b]SARA B.[/b][/url]

Post Reply