0

I try to code a bash 4 (or sh, if that's possible) script that does the following: 1st parameter: the path to the compressed modules to search, could be "." 2nd the search pattern to look for

The script so far looks like this (be aware that it is not foolproof, it not checks if both parameters are valid, it not even checks if 2 parameters are given at all, all that I would add if I get the script to work as I want it to)

#!/bin/bash
for module in $(find $1 -type f -name "*.xzm"); do
    echo $module
    lsxzm $module | grep "$2"
done

lsxzm is a script that lists the contents of the compressed live Linux module. Now, a possible result for such a search would look like so (lets consider I saved the script as "/usr/local/bin/lsxzmgrep" and made it executable):

/porteus/base# lsxzmgrep . libtiff.so.3
./000-kernel.xzm
./001-core.xzm
/usr/lib64/libtiff.so.3
/usr/lib64/libtiff.so.3.9.7
./001-de-core_locales.xzm
./002-xorg.xzm
./003-xfce.xzm
./010-nvidia-304.137-k.4.16.3-x86_64-don.xzm

I would like to have only this output:

/porteus/base# lsxzmgrep . libtiff.so.3
./001-core.xzm
/usr/lib64/libtiff.so.3
/usr/lib64/libtiff.so.3.9.7

How can this be done? All I can think of is temporarily buffering the output of each grep from the for do done loop, and only print it together with the name of the module when grep did find something, done by examining the $? of grep.

But that would be a less than elegant solution.

Now I wonder, is a shell script, especially for bash 4, able to get the same result without the need of buffering the grep result and examining the return value of the grep command to get the same result: only print the name of the module and the listed file name(s) when the grep command did actually find something?

2 Answers 2

1

You should first get the grep results, then print data out:

#!/bin/bash
for module in $(find $1 -type f -name "*.xzm"); do
    result=$(lsxzm $module | grep "$2")
    if [ $? -eq 0 ]; then
        # grep returned OK, write out results
        echo $module
        echo "$result"
    fi
done

To do this, the output of grep is stored into a variable "result". The exit status is checked; if it is 0 (ok), then grep succeeded (in finding the search string), so data is printed out.

2
  • Thanks Linuxfan, that does the trick. Seems there is no neat trick without the need to store the grep result in a variable and check grep status.
    – Rava
    Dec 25, 2018 at 7:05
  • @Rava You can avoid to store the result by invoking grep two times. Use the grep option -l the first time, it prints out the filename instead of its matches. Not sure if it is better. Otherwise, the idea to filter the whole result, as Walter suggests, is not bad, but I am not enough acquainted to sed/awk to tell. Dec 25, 2018 at 7:11
0

You only want to echo the module when grep returns something.

for module in $(find $1 -type f -name "*.xzm"); do
    lsxzm ${module} | grep "$2" | sed -r "s#.+#${module}\n&#"
done
3
  • Something seems to go wrong, using your code the same inquiry as in my initial post gives these errors: $ lsxzmgrep . libtiff.so.3 sed: -e expression #1, char 10: number option to s' command may not be zero sed: -e expression #1, char 11: unknown option to s' Last error is repeated several times.
    – Rava
    Dec 25, 2018 at 0:42
  • I tried adding br with the "2 spaces at the end of line" but for some reason I cannot save the edit.
    – Rava
    Dec 25, 2018 at 0:53
  • Sorry, I tested mith a random value for module. In your case the module will have slashes, and that will confuse the sed command/ I updated my answer and use #.
    – Walter A
    Dec 26, 2018 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.