Register
It is currently Sat Nov 22, 2014 5:46 pm

sed commands work in onliner but not in .bashrc function


All times are UTC - 6 hours


Post new topic Reply to topic  [ 2 posts ] 
Author Message
 PostPosted: Fri Apr 25, 2014 12:17 am   

Joined: Mon Jul 01, 2013 7:24 pm
Posts: 19
I have the following in my rc.xml (for openbox on Debian stable) file to keep app window decorations to an absolute minimum:

Code:
<!-- Q. How do I get rid of ALL window decorations, including a window's titlebar?-->
<!-- A. <application class="*"><decor>no</decor></application> or so in the applications section. -->
<application class="*">
  <decor>no</decor>
</application>


but sometimes, like when viewing photos I want to have the titlebar restored to quickly see what the name of the current photo being viewed is. So I wrote a function in my .bashrc to handle the process, or so I thought:

Code:
function tbt () {
    f="$HOME/.config/openbox/rc.xml"

    #get line number of application open tag
    lnNum=$(cat -n "$f"|grep "How do I get rid of ALL window decorations, including a window's titlebar"|cut -f1|sed -r 's/^[ \t]*//'|sed -r 's/[ \t]*$//') ; echo "\$lnNum1: ${lnNum}"

    ((lnNum = lnNum + 2)) ; echo "\$lnNum2: ${lnNum}"   #add 2 to the line num to get to actual tag that needs altered
    #try first to uncomment the tags
    sed -ri "${lnNum}s/<\!--(application class=.\*.>)/<\1/" "$f"        #uncomment tags
    stat="$?"
    if [[ $stat == 0 ]] ; then  #'if' previous command was run then uncomment closing tag
        ((lnNum = lnNum + 2)) ; echo "\$lnNum3: ${lnNum}"   #add 2 to $lnNum to edit closing tag
        sed -ri "${lnNum}s/(<\/application)-->/\1>/" "$f"   #uncomment closing tag
    else    #'else' tags must need to be commented out instead
        sed -ri "${lnNum}s/<(application class=.\*.>)/<\!--\1/" "$f"    #'else' comment the tags out
        ((lnNum = lnNum + 2)) ; echo "\$lnNum4: ${lnNum}"
        sed -ri "${lnNum}s/(<\/application)>/\1-->/" "$f"
        stat="$?"; if [[ $stat != 0 ]] ; then notify-send "Aborting. Command failed: sed -ri ${lnNum}s/<(application class=+\*+>)/<\!--\1/ ${f}" ; exit 69 ; fi
    fi

    #restart openbox
    openbox --restart
}


but the function does not work, although all the echoed line numbers reported are correct. Both of the following oneliners copied ans pasted from the above function work as desired depending on which is required to alter the state of the tags in rc.xml:

Code:
#to uncomment
sed -ri "1289s/<\!--(application class=.\*.>)/<\1/" $HOME/.config/openbox/rc.xml ; if [[ "$?" == 0 ]] ; then sed -ri "1291s/(<\/application)-->/\1>/" $HOME/.config/openbox/rc.xml ; fi ; openbox --reconfigure


Code:
#to comment out
sed -ri "1289s/<(application class=.\*.>)/<\!--\1/" $HOME/.config/openbox/rc.xml ; if [[ "$?" == 0 ]] ; then sed -ri "1291s/(<\/application)>/\1-->/" $HOME/.config/openbox/rc.xml ; fi ; openbox --reconfigure


So I wonder if anyone can see the problem I am missing in my function preventing it from working as desired?


Top
 Profile  
 PostPosted: Fri Apr 25, 2014 5:25 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 567
hi,

sed always returns an exit code equal to zero, unless there is syntax error(s).


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Yahoo [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP