LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-31-2017, 03:08 AM   #1
erik__492
LQ Newbie
 
Registered: Aug 2017
Posts: 8

Rep: Reputation: Disabled
Smile Bash Script to change color and picture


Hello guys,

I've got question about bash script.

For your information, there are html files witch are updating automatically every min.
So far okay, but I want to change the background color from #FFFFFF to #000000 and a picture.

All the html files are located in many directories :

'/var/www/html/Traffic/TP-Link/TP-Link_14/'
'/var/www/html/Traffic/TP-Link/TP-Link_15/'
'/var/www/html/Traffic/Netgear/Netgear_10/'
'/var/www/html/Traffic/Netgear/Netgear_11/'
'/var/www/html/Traffic/Netgear/Netgear_12/'
'/var/www/html/Traffic/Netgear/Netgear_13/'


The html command for the picture is: <img src="mb_logo.png" width="600" height="150" align="right">


So my question here is how I have to write a bash command for this, that when the html files are updating, they update with this properties.

Thank you for your answers.

Regards Erik
 
Old 08-31-2017, 03:20 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,001
Blog Entries: 3

Rep: Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632
Bash won't really work with HTML / XHTML but you could do XHTML modifications with perl pretty easily.

However, let's step back and check how you are doing the background colors. Hopefully that is with CSS using an external stylesheet.
Code:
<link rel="stylesheet" href="/erik/css/default_style.css" 
        media="screen, projection" type="text/css" />
If so, then just change the one line in the one stylesheet and all the XHTML documents pointing to it will have their backgrounds changed.

About adding the image, you can insert a line easily using sed. But first you must identify a pattern in the file which you can use for a reference. Which unique HTML element do you want the image inserted before or after?
 
Old 08-31-2017, 03:33 AM   #3
erik__492
LQ Newbie
 
Registered: Aug 2017
Posts: 8

Original Poster
Rep: Reputation: Disabled
Hi,

i think you don't understood me.

To set the color and picture is no problem but the html updates every minute so my changes that I did are deleted.

When I set the color after one min the color is set back how it was.

Now I want to have something that automaticly put in this changes when the html file gets updated.

Do you understand what I mean ?

Regards Erik
 
Old 08-31-2017, 03:47 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,001
Blog Entries: 3

Rep: Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632
Kind of.

Please show the exact string you want to replace to make the color change.

It would be more practical to make the changes upstream in the script that is generating the XHTML files in the first place. Even then the CSS advice still stands.

But if you are going to watch the directory with inotify / incron and run a script, that script could run sed to insert lines or even replace lines. However, for that to work you have to be able to identify the exact place in the XHTML file for sed to be able do its thing. For example, if I want "foobar" added after the 15th line in index.html, making a backup copy of the original:

Code:
sed -i.bak -e  '15afoobar' index.html
If I want "foobar" added before an element, say P of the "note" class, it is also rather easy:

Code:
sed -i.bak -e '/<p class="note">/ifoobar' index.html
If I want to add "foobar" after that same element, it is more tricky:

Code:
sed -i.bak -n '1h;1!H;${g;s/<p class="note".*<\/p>/&\nfoobar/;p;}' index.html
When it gets that tricky you are better off using perl. Again, it is easier if you make the changes upstream in the source of the script that is making your XHTML files.

Last edited by Turbocapitalist; 08-31-2017 at 03:49 AM.
 
Old 08-31-2017, 04:09 AM   #5
erik__492
LQ Newbie
 
Registered: Aug 2017
Posts: 8

Original Poster
Rep: Reputation: Disabled
okay thats the string:

<style type="text/css">
body {
background-color: #ffffff;
}

thats how I need it:

<style type="text/css">
body {
background-color: #000000;
color: #FFFFFF;
}


And the command that updates the html file: env LANG=C /usr/bin/mrtg /var/www/html/Traffic/TP-Link/TP-Link_14/TP-Link_14.cfg
 
Old 08-31-2017, 04:17 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,001
Blog Entries: 3

Rep: Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632
Ok. If that is a binary then there is not much chance of being able to modify it. The next best thing is to have whatever is calling that binary to also call your script.

As for your script, if that string is unique and it does not vary at all, not even in the number of spaces, then you can use sed still:

Code:
sed -i.orig -E -e 's|(background-color:) #ffffff;|\1 #000;\ncolor: #fff;|' index.html
If there is variation in that string or if it is not unique in each file then you must create a different pattern. See "man sed" and "man 7 regex" for reference.

I hope that is enough to get you started.
 
Old 08-31-2017, 04:21 AM   #7
Effman
LQ Newbie
 
Registered: Mar 2017
Location: Hamburg
Distribution: Debian
Posts: 17

Rep: Reputation: Disabled
Hi Erik,
you say changing the color/picture is no problem. So your only Problem is, that it changes back every minute, right?
Do you know how the command that does these unwanted changes is executed? If it is via cron, the easiest way would be to edit the cron entry and add your code to change color & image.
 
Old 08-31-2017, 07:35 AM   #8
erik__492
LQ Newbie
 
Registered: Aug 2017
Posts: 8

Original Poster
Rep: Reputation: Disabled
Hi Effman,

yes you are right it's in a crontab.

For your information I'm using the tool MRTG to monitor traffic.

The command that changes is: env LANG=C /usr/bin/mrtg /var/www/html/Traffic/TP-Link/TP-Link_14/TP-Link_14.cfg

The cfg file I#ve created with cfgmaker via SNMP from a swich.

So do you understand what I mean ?

Regards Erik
 
Old 08-31-2017, 07:57 AM   #9
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,001
Blog Entries: 3

Rep: Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632
What is in /var/www/html/Traffic/TP-Link/TP-Link_14/TP-Link_14.cfg regarding the background and foreground colors.
 
Old 08-31-2017, 08:00 AM   #10
Effman
LQ Newbie
 
Registered: Mar 2017
Location: Hamburg
Distribution: Debian
Posts: 17

Rep: Reputation: Disabled
Hi Erik,
when your cron is exexuted every minute it should look similar to this:

*/1 * * * * root env LANG=C /usr/bin/mrtg /var/www/html/Traffic/TP-Link/TP-Link_14/TP-Link_14.cfg

You just have to change it to:

*/1 * * * * root env LANG=C /usr/bin/mrtg /var/www/html/Traffic/TP-Link/TP-Link_14/TP-Link_14.cfg && your_script_that_changes_color_and_picture.sh

It does not prevent the change but it changes it back to your settings right after.
 
Old 08-31-2017, 08:07 AM   #11
erik__492
LQ Newbie
 
Registered: Aug 2017
Posts: 8

Original Poster
Rep: Reputation: Disabled
Hi Effman,


your briliant.

You speak german too I think, so: Danke es funktioniert endlich
 
Old 08-31-2017, 08:12 AM   #12
Effman
LQ Newbie
 
Registered: Mar 2017
Location: Hamburg
Distribution: Debian
Posts: 17

Rep: Reputation: Disabled
Haha is it that obvious I'm german too? Glad I could help!
 
Old 08-31-2017, 08:15 AM   #13
erik__492
LQ Newbie
 
Registered: Aug 2017
Posts: 8

Original Poster
Rep: Reputation: Disabled
Haha no, because of your location.

Your english is perfect
 
Old 08-31-2017, 10:14 AM   #14
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,001
Blog Entries: 3

Rep: Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632Reputation: 3632
So just for clarification, your original question was about modifying a cron job rather than about modifying some XHTML files?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Change Bash Folder Color carlosinfl Linux - General 2 11-20-2013 11:39 AM
Change executable file color in Bash casela Linux - Newbie 1 03-18-2012 07:30 PM
bash script ? Change PS1 color on per user basis tommyttt Linux - Software 5 03-01-2010 12:12 AM
How can I change text color in BASH evansd321 Linux - General 1 09-10-2008 04:00 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration