Replace Text Section using Bash

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

Replace Text Section using Bash

#1 Post by RSH »

Hi.

I want to replace the OrigFbPanel-Section with the NewFbPanel-Section in the code below. Presented Code-"solution" did not work.

Any hint, what I'm doing wrong?

Thanks,

RSH
#!/bin/bash -a
#------------------------------------------------------------------------------
# Leeres Skript für LazY Puppy
# 2012-12-28 RSH for LazY Puppy
#------------------------------------------------------------------------------

# Menu Hierarchy
HirFile=/usr/sbin/pfbpanel
MyHirFile=/tmp/TempInstall/add2fbpanel
MyHir=`cat $MyHirFile`
TempFile=/tmp/TempInstall/add2fbpanel
HirTempFile=/new_pfbpanel
MYHIRINSERT="true"
MYHIRINSERT2="true"
MYHIRINSERT3="true"

OrigFbPanelItems=" fbpanel-xdgmenu /etc/xdg/menus/puppy-desktop.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-system.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-setup.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-utility.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-filesystem.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-graphic.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-document.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-calculate.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-personal.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-network.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-internet.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-multimedia.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-fun.menu >> /tmp/fbpanel-menu"

NewFbPanelItems=" fbpanel-xdgmenu /etc/xdg/menus/puppy-desktop.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-system.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-setup.menu >> /tmp/fbpanel-menu
echo separator \{ >> /tmp/fbpanel-menu
echo \} >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-vsppup.menu >> /tmp/fbpanel-menu
echo separator \{ >> /tmp/fbpanel-menu
echo \} >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-utility.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-filesystem.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-graphic.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-document.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-calculate.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-personal.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-network.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-internet.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-multimedia.menu >> /tmp/fbpanel-menu
fbpanel-xdgmenu /etc/xdg/menus/puppy-fun.menu >> /tmp/fbpanel-menu"

sed -i 's|'$OrigFbPanelItems'|'$NewFbPanelItems'|' $HirFile


exit 0

#------------------------------------------------------------------------------
# End
#------------------------------------------------------------------------------
Terminal gives me an unterminated s command message.
[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]

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

#2 Post by seaside »

RSH,

Maybe double quotes-

Code: Select all

sed -i 's|"OrigFbPanelItems"|"NewFbPanelItems"|' $HirFile 
Cheers,
s

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

#3 Post by RSH »

Unfortunately not. :cry:

I have tried already this:

Code: Select all

sed -i 's|'$OrigFbPanelItems'|'$NewFbPanelItems'|' $HirFile
and this:

Code: Select all

sed -i 's|"OrigFbPanelItems"|"NewFbPanelItems"|' $HirFile
and also this:

Code: Select all

sed -i 's|'"OrigFbPanelItems"'|'"NewFbPanelItems"'|' $HirFile
Results are equal:

sed: -e expression #1, char 74: unterminated 's' command - or: the whole $HirFile is scrolling through the terminal (the just double quoted version)

char changes between 2 (single quoted) and 74 (songle-double quoted) :?:
[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]

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#4 Post by amigo »

You need to use double quotes on the *outside* part of the sed command. Since the replace values are represented as variables they should be expanded by the shell before being passed to sed. Your use of single quotes prevents the shell from doing this.
Should do what you want:

Code: Select all

sed -i "s|$OrigFbPanelItems|$NewFbPanelItems|" $HirFile

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

#5 Post by RSH »

amigo wrote:You need to use double quotes on the *outside* part of the sed command. Since the replace values are represented as variables they should be expanded by the shell before being passed to sed. Your use of single quotes prevents the shell from doing this.
Should do what you want:

Code: Select all

sed -i "s|$OrigFbPanelItems|$NewFbPanelItems|" $HirFile
Sorry, unfortunately doesn't work also.

sed: -e expression #1, char 74: unterminated 's' command
[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]

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

#6 Post by seaside »

RSH,

It looks like the last quoting by Amigo is right, so there is probably something in the variables causing the error.

Code: Select all

# OrigFbPanelItems='one two three'
# NewFbPanelItems='four five six'
# HirFile='one two three'
# sed  "s|$OrigFbPanelItems|$NewFbPanelItems|" <<<"$HirFile"
four five six
One down, only a zillion more to go :D

Cheers,
s

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#7 Post by amigo »

May be simpler to simply read the original file line-by-line while writing the contents to a new file and inserting your changes at the right time/place. May be that you need to escape the instances of '>' chars. I believe I'd use a nice little read/check-for-right-place/write op for that. sed could be made useful to do it, but pure shell-manipulation will be much simpler too achieve

Yeah, shell scripting can be used by Lazy people, but as soon as you want to do some serious changes, you can't be a Lazy coder. It's kind like the difference between being a Slacker and being the guy who creates Slack -or vice-versa. If you want to make things simple for your user, then you need to do complex things for them (very anti-Slack behaviour). Or, *you* can be the Slacker and makes users do things for themselves. Slack-user!=Slack-creator. Alles Klar?

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

#8 Post by RSH »

May be simpler to simply read the original file line-by-line while writing the contents to a new file and inserting your changes at the right time/place. May be that you need to escape the instances of '>' chars. I believe I'd use a nice little read/check-for-right-place/write op for that. sed could be made useful to do it, but pure shell-manipulation will be much simpler too achieve
Exactly this I've tried before. It has worked for the /usr/bin/obmenu-refresh file, but on the pfbpanel file it loses every "\" in every line, somewhere between reading it and writing it to the temp file.

That's why I've come back to this here.

I've gotten some sed lines before from seaside and L18L on another issue, so I thought, why not try to use something similar like this?

Code: Select all

sed -i 's/<keyboard>/<keyboard>my code/' rc.xml
So, by now, it looks like the One-Click-VSP-Puppy-Builder will be a One-Click-plus-One-manual-step-VSP-Puppy-Builder. :lol:

Thanks for the help
[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