2

I have a binary that repacks android kernel (not mkbootimg). I'm making a script to automate the process.

I don't want any output on the screen. So I have redirected the output to a file named foo.log.

The command is - kernel_make "$zImage" "$ramdisk" "$cmdline" "$image" &> data/local/working/foo.log

My current working folder is data/local/working/. What I've to do is, if the last line of the output (foo.log) is this -

"targed file $1 is patched sucesfully, enjoy new kernel"

Then return 0 and exit. Else return 1 and exit.

I'm trying to do with grep, but as I'm new to command line, do don't have any idea of doing it the right way.

Anthon
  • 79,293
Chinmay
  • 37
  • 1
  • 8

2 Answers2

2

Grep exits by default with 0 on match and 1 on no match. As such you could do:

grep -q "targed file \$1 is patched sucesfully, enjoy new kernel" foo.log

The -q suppress any output.

To test say something like:

grep -q "targed file \$1 is patched sucesfully, enjoy new kernel" foo.log && echo OK || echo BAD

If the $1 is actually replaced with a file name you could replace \$1 with something like .* or \S\+.

You might also want to consider fixing (checking):

targed     -> target
sucesfully -> successfully

As noted in comment this matches against entire file. Use tail as expressed by @orion.

user367890
  • 1,887
  • 2
  • 16
  • 27
1

grep is the way to go, it returns 0 if a match is found. You don't actually need to output the line, so just discard the line and use the test. In your case, it would just be

lastline=$(tail -n1 logfile)
if grep pattern <<<"$lastline" &>/dev/null; then
    echo "yay, found pattern"
else
    echo "darn"
fi

Observe the "here string" construct <<< which presents the string as a file for grep to read, and &> that redirects both standard output and error output (redirect to null in this case). Pattern could be "enjoy" or something. Replace the echoes with your custom handling.

However, it would make much more sense to just check if kernel_make returns 1 if it fails. No need to parse a user-friendly log output (which will likely change or cease to be the last message). If that is the case (most likely), I'd just do

kernel_make [...arguments...] && echo "yay" || echo "nay"

(or an if statement, like in the grep case). And you are probably familiar with exit statement which you will probably use in the form exit 1 in the case of failure.

If kernel_make is the last line of the script, you don't even have to do anything...

orion
  • 12,502