System-wide search

When looking for wherever a program or a filename appears in the system, a short scrip is "findinner" which another script calls with a long parameter list consisting of path names ending with ".sh" or ".menu". "findinner" looks like this:

# If not .savenn file, show name and result of grep.
#
for i in "$@"
do
  echo $i |grep -q 'save[01][0-9]'
  if [ $? -ne 0 ]
  then
    echo "<<<<<<<<<<<<<<<< $i >>>>>>>>>>>"
    grep HardCodedSearchItem $i
  fi
done

Is there a way to not have to modify "findinner" every time it is used? I would prefer to leave the scripts alone and just provide the search term as a parameter.

Is there any way to do this? TIA.

Pass the search pattern as the 1st argument to your script, then:

# If not .savenn file, show name and result of grep.
#
pattern=$1
shift
for i in "$@"
do
  echo $i |grep -q 'save[01][0-9]'
  if [ $? -ne 0 ]
  then
    echo "<<<<<<<<<<<<<<<< $i >>>>>>>>>>>"
    grep "$pattern" "$i"
  fi
done
1 Like

Btw., this:

could also be written:

  if ! echo $i |grep -q 'save[01][0-9]'
  then
  [....]
 

generally:

if <cmd> ; then

the if-branch will be executed if <cmd> returns 0, the else-branch otherwise.

if ! <cmd> ; then

the if-branch will be executed if <cmd> returns <>0, the else-branch if it returns 0.

I hope this helps.

bakunin

Could you not also use find to drive this rather than building your own where you have to feed it a list? You don't give much information about what else there is, but this might help:-

find . -name "*save[0-9][0-9]*" -type f -exec grep "$pattern" {} \;

If the file name search is for path names, then you could do something more like this:-

find *save[0-9][0-9]* -type f -exec grep "$pattern" {} \;

Do either of these give you an option? They should be more efficient than calling grep for every parameter, especially as the list gets longer and would reduce the risks of too long a command line getting rejected.

You could even combine them if that helps:

  • If you want all files that match save[0-9][0-9] and finish .sh then you can find . -name "*save[0-9][0-9]*.sh" .....
  • If you want files in directories that match save[0-9][0-9] and finish .sh then you can find *save[0-9][0-9]* -name "*.sh" ......
  • If you want some other variation or combination we can probably help there too.

Are any of these useful?
Robin

I changed the script findinner using Don's suggestions. The script that calls it now starts with the search term "$1" followed by paths ending in star names.

Thanks again.