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 6831613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:45:28+00:00 2026-05-26T22:45:28+00:00

I am trying to parse an option in a bash script. how can I

  • 0

I am trying to parse an option in a bash script. how can I use getopts to see whether an optional flag has been entered.

FILE1=$1
FILE2=$2
outputfile=''
while getopts "o" OPTION
do
    case $OPTION in
    o)
       outputfile=$OPTARG
    ;;
    esac
done
if [ ! $outputfile -eq '' ]
then
    cat $FILE1 | paste - | $FILE1 - | tr "\t" "\n" | paste $FILE1 $FILE2 | tr '\t' '\n' > $outputfile

else
    cat $FILE1 | paste - | $FILE1 - | tr "\t" "\n" 
    paste $FILE1 $FILE2 | tr '\t' '\n' 
fi
  • 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-05-26T22:45:29+00:00Added an answer on May 26, 2026 at 10:45 pm

    There are a number of problems here. You need to parse options (the getopts loop) first, then remove them from the argument list (with shift $(($OPTIND-1))), then get FILE1 and FILE2 from $1 and $2. Second, you need to tell getopts that -o takes an argument (getopts "o:"). Third, your getopts loop should include checking for an invalid option (and you should probably also make sure both FILE1 and FILE2 were specified). Fourth, when checking whether $outputfile is blank, you need double-quotes around it and then use a string test (-eq checks for numeric equality, and will give an error if you use it to compare anything other than numbers). Fifth, you should have double-quotes around the filenames in case they have any funny characters in them. Finally, the actual commands you’re trying to execute (paste, tr, etc) don’t make sense (so I pretty much left them alone). Here’s my shot at a rewrite:

    #!/bin/sh
    outputfile=''
    while getopts "o:" OPTION
    do
        case $OPTION in
        o)
            outputfile="$OPTARG"
        ;;
        [?])
            echo "Usage: $0 [-o outfile] file1 file2" >&2
            exit 1
        ;;
        esac
    done
    shift $(($OPTIND-1))
    
    if [ $# -ne 2 ]; then
        echo "Usage: $0 [-o outfile] file1 file2" >&2
        exit 1
    fi
    FILE1="$1"
    FILE2="$2"
    
    if [ -n "$outputfile" ]; then
        cat "$FILE1" | paste - | "$FILE1" - | tr "\t" "\n" | paste "$FILE1" "$FILE2" | tr '\t' '\n' > "$outputfile"
    else
        cat "$FILE1" | paste - | "$FILE1" - | tr "\t" "\n"
        paste "$FILE1" "$FILE2" | tr '\t' '\n'
    fi
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to parse incoming options in my bash script, and save the
I am trying to use optparse (to parse command line options to my script)
Trying to parse Maplines for an airport. Each airport can have X number of
I have been trying to parse Java exceptions that appear in a log for
I am trying to use HtmlAgilityPack to parse HTML, but am having problems. Sample
Trying to parse an HTML document and extract some elements (any links to text
Trying to parse some XML but apparently this is too much for a lazy
Trying to parse an SQL string and pull out the parameters. Ex: select *
Im trying to parse the string located in /proc/stat in a linux filesystem using
I'm trying to parse a grammar in ocamlyacc (pretty much the same as regular

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.