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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:36:15+00:00 2026-06-03T02:36:15+00:00

I have this bash script which pases a text file and I would like

  • 0

I have this bash script which pases a text file and I would like to make it a little simpler and shorter to make it more efficient, does anybody have any ideas on how i can do this?

$vi

function displayHelp
{
        echo "Use '-f' to set the file to be used "
        echo "Use '-s' to sort the data bya column"
        echo "Use '-m' to output the rows which match this expression"
}

function displayColumn
{
    columnnumber="$2"
    awk '{print $'$columnnumber'}' $1

}

function displayParameter
{   
    parameter="$3"
    columnnumber="$2"
    awk -v s=$3 -v c=$2 '$c ~ s { print $0 }' $1
}

while getopts f:s:m:h opt
do
  case "$opt" in
    h) displayHelp;;
    f) filepath="$OPTARG";;
    s) column="$OPTARG"  
    displayColumn $filepath $column
    ;;
    m) searchParam="$OPTARG"
    displayParameter $filepath $column $searchParam
    ;;

  esac
done
  • 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-03T02:36:16+00:00Added an answer on June 3, 2026 at 2:36 am

    In displayColumn(), you’re not using -v variable passing, but in displayParameter() you are. You should always use -v instead of embedding shell variables.

    Also in displayParameter() you assign two variables and never use them.

    You should always quote shell variables when they are expanded. It’s not necessary to quote them on the right hand side of an assignment when they appear alone.

    There’s no enforcement that -f is required, if that’s what you intend. Without supplying it (and its required argument), your script will try to read from stdin (which may be what you intend).

    There’s no enforcement that -m and -s are required. Without both of them being present, the AWK command will output an error message.

    In general, required arguments should be handled as positional parameters and option flags should be used for options (and their arguments).

    Your question is possibly better suited for Code Review.

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

Sidebar

Related Questions

I have a bash script file which starts with a function definition, like this:
I have a bash script that looks like this: #!/bin/sh previousRelease=`git describe --tags --match
I have a script that looks like this #!/bin/bash function something() { echo hello
I have a bash script which calls another bash script, like so: #!/bin/bash echo
I have a little Bash script which suspends the computer after a given number
I have pulled the Bash script from here , which checks the AVI file
I have a simple test bash script which looks like that: #!/bin/bash cmd=rsync -rv
Let's imagine I have a bash script, where I call this: bash -c some_command
I have a bash script that has this function in it: function start_vi() {
For testing purposes I have this shell script #!/bin/bash echo $$ find / >/dev/null

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.