Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8937901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:29:49+00:00 2026-06-15T10:29:49+00:00

I use getopts to obtain a MAC address and grep that MAC address through

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:29:50+00:00Added an answer on June 15, 2026 at 10:29 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a bash function that I'm trying to use getopts with and am
Edited Now my code is like this: #!/usr/bin/perl # import packages use Net::POP3; use
I'm using this minifier: #!/usr/bin/perl use Getopt::Std; use strict; my %opts; getopts('f:t:h', \%opts) ||
I'm trying to use getopts in bash to parse command line arguments, but I
How can I use Getopt::Long method if the input command execution is like this:
Can you use the bash getopts function twice in the same script? I have
I'd like to enable/disable the comments within my Perl program that make use of
what is the easiest, most straight forward, way to use getopts in bash script.
I have this bash script which pases a text file and I would like
Is it possible to use getopts to process multiple options together? For example, myscript

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.