Thread: bash search script improvement
hi,
i've switched ubuntu 10.10 few months ago , have started tinkering around bash scripting. ive been working on script searches filesystem specific file or directory, reason being find gui based search bit slow. have no prior programming/scripting experience, , ive written script want [ albeit imperfectly] in under 5 seconds.
problem search function imperfect, ie can identify directories, if last character of directory provided user. that.
also, don't know if ive commented script correctly [too much? little? ] , importantly, if code wasteful / plain bad.
along of these lines appreciated...
ps: i've searched on forum couldnt find better place post this. if im wrong, let me know , ill happily repost in correct section...code:#! /bin/bash # script search specific file or directory in home folder , mounted filesystems, excluding /bin, /var , on... # disclaimer- know zilch, zero, nothing sed or awk. sed usage has been copied/modified google searches, , dont know how works # [despite having read few guides]... # whole sript built around grep searches, find compiling file lists, , wc -l line counts.. # function make index files, searched grep calls later locate files.... function makeindex { echo "indexing files..." cd /media sudo find ./* -type f > /tmp/medialist.txt cd /home sudo find ./* -type f > /tmp/homelist.txt echo "indexing directories..." cd / sudo find . -type d > /tmp/dirlisttemp.txt # add comma end of each line in dirlist, grep calls [later] can identify specific directory names # ie, grep searchterm returns "etc-searchterm-etc", grep "searchterm," returns searchterm, , not subdirs sed 's/\(.*\)/\1,/' < /tmp/dirlisttemp.txt > /tmp/dirlist.txt rm -f /tmp/dirlisttemp.txt # read number of lines in index files determine total objects indexed, file sizes (stat) , report user set `wc -l /tmp/dirlist.txt` dirnum="$1" set `wc -l /tmp/medialist.txt` medianum="$1" set `wc -l /tmp/homelist.txt` homenum="$1" totalnum=$(($homenum+$medianum)) set `wc -l /tmp/dirlist.txt` dirnum="$1" echo "$medianum files indexed in /media" echo "$homenum files indexed in /home" echo "$dirnum directories indexed" size=0 x in /tmp/homelist.txt /tmp/medialist.txt /tmp/dirlist.txt ; set `stat -t $x` size=$(($size+$2)) done sizef=$(($size/1000000)) echo "total index size $sizef megabytes" sleep 1 } # check - missing command line arguments / argument -u regenerate index [ "$1" = "-u" ] && { echo "regenerating index files..." ; makeindex ; exit ; } [ "$1" = "" ] && { echo "missing argument...." ; echo 'usage: search.sh "file/dir name" -d[irectory search]' ; echo "or search.sh -u rebuild index" ; exit ; } # assign $1 variable searchname, because value of $1 modified later in script cannot used search searchname="$1" # check index files ls /tmp/homelist.txt &> /dev/null || { echo "logfile not found...generating file index, may take while..." ; makeindex ; } ls /tmp/dirlist.txt &> /dev/null || { echo "logfile not found...generating file index, may take while..." ; makeindex ; } ls /tmp/medialist.txt &> /dev/null || { echo "logfile not found...generating file index, may take while..." ; makeindex ; } #search directory matches if $2 -d if [ "$2" = "-d" ]; echo "searching directory" # search user inputted value followed comma, subfolders ignored # however, means last character of dirname has provided user, ie # dir named "test1" cannot found cmd line input of "test" grep -i "$searchname""," /tmp/dirlist.txt > /home/$user/dirsearch.txt set `wc -l /home/$user/dirsearch.txt` dirnum="$1" echo ""$dirnum" directories found." if [ "$dirnum" -ge "1" ]; echo "displaying results. results have been saved /home/$user/dirsearch.txt" echo "" cat /home/$user/dirsearch.txt fi echo "press enter continue searching in file lists or ctrl-c exit" read userwaste fi echo "searching ""$1"" in index..." rm -f /home/$user/searchresults.txt # search files | ignore folders [crude code, needs improvement dont know how] ... grep -i "$searchname" /tmp/homelist.txt | grep -i -v "/""$searchname""/" > /home/$user/searchresults.txt grep -i "$searchname" /tmp/medialist.txt | grep -i -v "/""$searchname""/" >> /home/$user/searchresults.txt set `wc -l /home/$user/searchresults.txt` matchnum="$1" [ "$matchnum" = "0" ] && { echo "" ; echo "no results found" ; exit ; } echo "" echo "displaying results..." echo "" cat /home/$user/searchresults.txt echo "" echo "a total of $matchnum matches found" echo "results saved /home/$user/searchresults.txt"
i took rough @ script. looks you're trying keep file , directory cache, search can done fast.
don't yet problem have script. post example of failed search?
regards.
Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk [SOLVED] bash search script improvement
Ubuntu
Comments
Post a Comment