LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-13-2010, 02:16 PM   #1
shakazzolo
LQ Newbie
 
Registered: Jul 2010
Posts: 11

Rep: Reputation: 0
make this script faster?


This is a script that I use to cleanup the /lib/firmware and /lib/modules but its really slow... is there any smarter way for doing this?

I can only run bash scripts and compiled programs (C/C++) on that system... so python or perl is not an option

Code:
export mymod=`lsmod | awk -F' ' '{print $1}' | grep -v Module`
for file in `find . -type f`
 do
 testResult=""
 for mod in $mymod
   do
	tmpVar=`echo $file | grep -i $mod`
	if [ -n "$tmpVar" ]; then
		testResult=$mod
		break
	fi
   done

 if [ -n "$testResult" ]; then
	printf "$file is used by $testResult \n"
 else
	printf "Deleting $file\n"
	rm -Rf $file
 fi
done
 
Old 07-13-2010, 03:00 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by shakazzolo View Post
...
I can only run bash scripts and compiled programs (C/C++) on that system... so python or perl is not an option
...
Both Perl and Python are compiled programs.
 
0 members found this post helpful.
Old 07-13-2010, 04:30 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by Sergei Steshenko View Post
Both Perl and Python are compiled programs.
Segei, please realize when you post you're supposed to help out people. Having only ten posts here this user may or may not have the amount of knowledge (yet) a pro possesses or might have phrased things differently than a pro would. So do look beyond that and please ditch the pedantic tone of voice.
 
Old 07-13-2010, 04:46 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by unSpawn View Post
Segei, please realize when you post you're supposed to help out people. Having only ten posts here this user may or may not have the amount of knowledge (yet) a pro possesses or might have phrased things differently than a pro would. So do look beyond that and please ditch the pedantic tone of voice.
I'm helping the person pointing out the glaring contradiction in his/her post.

I really do not understand why the OP can't run Perl, Python or any other scripting language on his/her system.

If/when the OP in more details describes the kind and limitation of his/her system, I'll think of ways of overcoming the limitations. I suspect the system is an embedded one, but I have no proof yet. I think I can offer a solution for, say, an ARM based system, why should anyone put the carriage before the horse ?

Last edited by Sergei Steshenko; 07-13-2010 at 04:48 PM.
 
Old 07-13-2010, 04:55 PM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
And, after thinking a little bit, I think I also have an "organizational" solution, i.e. there might be a possibility to perform the needed task not by CPU power of the system under question/consideration, but, again, why should anyone put the carriage before the horse ?
 
Old 07-13-2010, 04:58 PM   #6
salasi
Senior Member
 
Registered: Jul 2007
Location: Directly above centre of the earth, UK
Distribution: SuSE, plus some hopping
Posts: 4,070

Rep: Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897Reputation: 897
Quote:
Originally Posted by shakazzolo View Post
This is a script that I use to cleanup the /lib/firmware and /lib/modules but its really slow...
The script has several stages: one possibility is to find out which stage is slow and then think about a way of speeding that up (I'd guess the 'find' part, because it is easy to waste time in find, but that's a guess and information would be better).

Also you might think of describing the operation of the script; I have no idea what other files there are that you are trying not to delete.
 
Old 07-13-2010, 05:06 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
The OP stated flat-out from the beginning that perl and python aren't available to him, so focusing on any "contradictions" in what he said is simply unproductive.


Assuming I understand the script correctly (and that's not 100% certain), it looks like it can indeed be made more efficient. It uses a slow, check-all-the-names-individually-against-each-other double-nested loop that's bound to be quite slow.

I've whipped this replacement up, which should be much faster, because it batch processes the operations. However this means it comes at the cost of losing the detailed output that showed which module matched which file. On the other hand, I've made a couple of other modifications you might like.

Code:
#!/bin/bash

$wkdir=${1:-$PWD}  #defaults to current directory if directory isn't specified.

modlist=$(lsmod | awk '($1!="Module"){print $1}')
filelist=$(find $wkdir -type f -print )

matchlist=$(  echo "$filelist" | grep -i -F "$modlist"  )
deletelist=$( echo "$filelist" | grep -iv -F "$modlist" )


#echo "These files were matched:"   #uncomment to display the list of files
#echo "$matchlist"                  #that do not match the module list
#echo

echo "=============================="
echo
echo "These files will be deleted:"  
echo "$deletelist"
echo
echo "confirm? (y/n)"

read answer

case $answer in

     Y*|y*) echo "$deletelist" | xargs #rm -rf  #uncomment the rm here when 
                                                #satisfied that it works.
            echo "All files deleted"
      ;;

     *) echo "operation aborted"
        exit 1
      ;;

esac

exit 0
You can specify the directory you want to run the script on, so you don't actually need to cd to the directory itself.

./delmods.sh /lib/modules

It will first print out a list of files that have no matches, and are due to be removed, then ask you to confirm the operation. The actual removal is done by this command:

echo "$deletelist" | xargs rm -rf

I've commented out the delete command itself until you can be sure it's working correctly.

You can also uncomment the list of files that are excluded, but it means an even longer output, so I didn't include it by default.

In any case, please make sure that it's really doing what you want it to before using it, since I'm not completely sure what the original was doing, and I may have messed something up.

Last edited by David the H.; 07-13-2010 at 05:14 PM.
 
1 members found this post helpful.
Old 07-13-2010, 05:11 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by David the H. View Post
The OP stated flat-out from the beginning that perl and python aren't available to him, so focusing on any "contradictions" in what he said is simply unproductive.
...
And I do not accept that claim in the first place. Because I know how to build Perl - it is nowadays simple. I've done it a number of times myself.

Again, the easiest solution might be outside the coding plane.
 
Old 07-13-2010, 05:14 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by Sergei Steshenko View Post
I really do not understand why the OP can't run Perl, Python or any other scripting language on his/her system. If/when the OP in more details describes the kind and limitation of his/her system, I'll think of ways of overcoming the limitations. I suspect the system is an embedded one, but I have no proof yet. I think I can offer a solution for, say, an ARM based system, why should anyone put the carriage before the horse ?
A lot of questions can't be answered qualitatively well due to lack of information. Asking for clarity should then be the way to go IMHO.
 
Old 07-13-2010, 05:16 PM   #10
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by shakazzolo View Post
I can only run bash scripts and compiled programs (C/C++) on that system... so python or perl is not an option
As you can see from the replies more information would be welcome. The more verbose you are the potentially better advice can be tailored to suit your particular situation.
 
Old 07-13-2010, 05:19 PM   #11
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
By the way, I have to wonder if a script like this is really such a good idea in the first place. All it does is delete everything that doesn't match the modules that are currently loaded into the kernel. But what if there's a module you need that isn't loaded at the time the script is run?
 
Old 07-13-2010, 05:26 PM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by David the H. View Post
By the way, I have to wonder if a script like this is really such a good idea in the first place. All it does is delete everything that doesn't match the modules that are currently loaded into the kernel. But what if there's a module you need that isn't loaded at the time the script is run?
- that was fast.
 
Old 07-13-2010, 05:26 PM   #13
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by Sergei Steshenko View Post
And I do not accept that claim in the first place. Because I know how to build Perl - it is nowadays simple. I've done it a number of times myself.
And why should you not accept it, when the poster states it up-front? The impression I get is that may not be for technical reasons, but due to some kind of company policy or similar. At the very least you should take him provisionally at his word, and ask for clarification if and when necessary. It's just a simple scripting question, after all, so why make it more complicated than it needs to be?
 
Old 07-13-2010, 05:32 PM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by David the H. View Post
And why should you not accept it, when the poster states it up-front? The impression I get is that may not be for technical reasons, but due to some kind of company policy or similar. At the very least you should take him provisionally at his word, and ask for clarification if and when necessary. It's just a simple scripting question, after all, so why make it more complicated than it needs to be?
Because in my life I've heard too many claims about impossibility which in the end happened to be the results of lack of knowledge, imagination, due diligence, negligence, etc.

And I suspect that the issue is not a scripting/coding one, but the one one of overall system architecture, though "system" might be too strong a word here.
 
Old 07-13-2010, 07:25 PM   #15
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by shakazzolo View Post
This is a script that I use to cleanup the /lib/firmware and /lib/modules but its really slow... is there any smarter way for doing this?

I can only run bash scripts and compiled programs (C/C++) on that system... so python or perl is not an option

Code:
export mymod=`lsmod | awk -F' ' '{print $1}' | grep -v Module`
for file in `find . -type f`
 do
 testResult=""
 for mod in $mymod
   do
	tmpVar=`echo $file | grep -i $mod`
	if [ -n "$tmpVar" ]; then
		testResult=$mod
		break
	fi
   done

 if [ -n "$testResult" ]; then
	printf "$file is used by $testResult \n"
 else
	printf "Deleting $file\n"
	rm -Rf $file
 fi
done
its slow because you use tools that overlap in function, eg awk and grep. Also, for every file that you find, you create another loop to grep your strings. another approach might be

Code:
lsmod | awk '!/Module/{print $1}'  > template
find . -type f -iname "<specify probable names>" | while read -r FILE
do
   grep -f template "$FILE"
   .....
done
This way you don't need to loop the modules, but make use of grep's -f to do the job.
Also, you can try using grep's -R option to recurse instead of find command.

Last edited by ghostdog74; 07-13-2010 at 07:27 PM.
 
1 members found this post helpful.
  


Reply



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
First perl script, can I make it go faster?.. jhyland87 Programming 10 03-06-2010 07:43 PM
How to make X startup faster ? wearetheborg Debian 6 04-01-2009 10:23 AM
How to make booting faster? tuxuser19 Debian 7 10-15-2005 04:42 AM
How can I make linux faster? Superdude Linux - General 7 08-23-2004 08:34 PM
how to make MPlayer faster ? PunkPT Linux - Software 1 07-30-2004 03:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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