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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-31-2017, 03:08 AM
|
#1
|
LQ Newbie
Registered: Aug 2017
Posts: 8
Rep: 
|
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
|
|
|
08-31-2017, 03:20 AM
|
#2
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
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?
|
|
|
08-31-2017, 03:33 AM
|
#3
|
LQ Newbie
Registered: Aug 2017
Posts: 8
Original Poster
Rep: 
|
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
|
|
|
08-31-2017, 03:47 AM
|
#4
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
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 kakistocrat; 08-31-2017 at 03:49 AM.
|
|
|
08-31-2017, 04:09 AM
|
#5
|
LQ Newbie
Registered: Aug 2017
Posts: 8
Original Poster
Rep: 
|
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
|
|
|
08-31-2017, 04:17 AM
|
#6
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
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.
|
|
|
08-31-2017, 04:21 AM
|
#7
|
LQ Newbie
Registered: Mar 2017
Location: Hamburg
Distribution: Debian
Posts: 17
Rep: 
|
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.
|
|
|
08-31-2017, 07:35 AM
|
#8
|
LQ Newbie
Registered: Aug 2017
Posts: 8
Original Poster
Rep: 
|
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 
|
|
|
08-31-2017, 07:57 AM
|
#9
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
What is in /var/www/html/Traffic/TP-Link/TP-Link_14/TP-Link_14.cfg regarding the background and foreground colors.
|
|
|
08-31-2017, 08:00 AM
|
#10
|
LQ Newbie
Registered: Mar 2017
Location: Hamburg
Distribution: Debian
Posts: 17
Rep: 
|
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.
|
|
|
08-31-2017, 08:07 AM
|
#11
|
LQ Newbie
Registered: Aug 2017
Posts: 8
Original Poster
Rep: 
|
Hi Effman,
your briliant.
You speak german too I think, so: Danke es funktioniert endlich 
|
|
|
08-31-2017, 08:12 AM
|
#12
|
LQ Newbie
Registered: Mar 2017
Location: Hamburg
Distribution: Debian
Posts: 17
Rep: 
|
Haha is it that obvious I'm german too? Glad I could help!
|
|
|
08-31-2017, 08:15 AM
|
#13
|
LQ Newbie
Registered: Aug 2017
Posts: 8
Original Poster
Rep: 
|
Haha no, because of your location.
Your english is perfect 
|
|
|
08-31-2017, 10:14 AM
|
#14
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
|
So just for clarification, your original question was about modifying a cron job rather than about modifying some XHTML files?
|
|
|
All times are GMT -5. The time now is 12:20 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|