0

I use getopts to obtain a MAC address and grep that MAC address through log files. It looks like this:

#!/bin/bash

while getopts ":m:hx:" opt; do
  case $opt in
    m)
        cat /var/log/vmlog/Verimatrix.log | grep $OPTARG | grep VCAS080455
        cat /var/log/vmlog/Verimatrix.log | grep $OPTARG | grep VCAS080285
        cat /var/log/vmlog/Verimatrix.log | grep $OPTARG | grep VCAS080290
      ;;
    h)
        echo "./search_mac.sh -m <mac address> will filter the logs by mac address"
        echo "./search_mac.sh -h will print this message"
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done

I want to export the result to a file when the -x option is used:

./search_mac.sh -m 00067B6D87F0 -x /home/nico/extract.txt

I don't understand at this point how to obtain the argument from -x to be into my m) part of my case.

A little help would be great.

Thanks

dda
  • 6,030
  • 2
  • 25
  • 34
Nicolas de Fontenay
  • 2,170
  • 5
  • 26
  • 51
  • 1
    You win a UUOC (Useless Use of Cat) Award. Three times! Why not: `grep "$OPTARG" /var/log/vmlog/Verimatrix.log | grep -E 'VCAS080455|VCAS080285|VCAS080290'` so you only scan the log file once. You could reduce the second grep to `grep -E 'VCAS080(455|285|290)'` without straining the brain cells too hard. – Jonathan Leffler Dec 04 '12 at 02:54

1 Answers1

3

I think the best approach is to save the values of the option-arguments in shell variables, and then run your commands at the end:

#!/bin/bash

m_arg=
x_arg=

while getopts ":m:hx:" opt; do
  case $opt in
    m) m_arg="$OPTARG" ;;
    x) x_arg="$OPTARG" ;;
    h)
        echo "./search_mac.sh -m <mac address> will filter the logs by mac address"
        echo "./search_mac.sh -h will print this message"
        exit 0
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
  esac
done

if [[ "$x_arg" ]] ; then
    exec > "$x_arg"          # redirect STDOUT to argument of -x
fi

< /var/log/vmlog/Verimatrix.log grep -- "$m_arg" | grep VCAS080455
< /var/log/vmlog/Verimatrix.log grep -- "$m_arg" | grep VCAS080285
< /var/log/vmlog/Verimatrix.log grep -- "$m_arg" | grep VCAS080290

That said . . . this option doesn't seem all that useful to me, since -x /home/nico/extract.txt would just mean the same thing as > /home/nico/extract.txt. Am I missing something?

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • Hi thanks for the quick answer. I'm still a noob at bash. I'm not quite sure how the redirect works in your example. Is it a typo or should there be $x_arg in front of your redirect such as it would be : $x_arg < /var/log/vmlog/Verimatrix.log grep -- "$m_arg" | grep VCAS080455 ? – Nicolas de Fontenay Dec 04 '12 at 02:43
  • The `exec` notation with no command and only I/O redirection means that from that point in the script onwards, the I/O redirection is changed. In this case, anything that writes to standard output will now have its output go to whatever is in `$x_arg` (`/home/nico/extract.txt`). – Jonathan Leffler Dec 04 '12 at 02:51
  • Thanks again :) You are correct ultimately what I want is just > But that file would be variable and that's why it's a parameter. I just don't know any better way... Yet. – Nicolas de Fontenay Dec 04 '12 at 02:56
  • Note that you can do: `file=/home/nico/extract.txt; ./search_mac.sh -m 00067B6D87F0 > $file`. That is, the file name in a redirection can be specified by a variable. – Jonathan Leffler Dec 04 '12 at 03:02