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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:36:10+00:00 2026-06-06T05:36:10+00:00

I am pretty new to shell scripting and I have to add a flag

  • 0

I am pretty new to shell scripting and I have to add a flag (getopts) to my script where I can overwrite download url command if the script can’t reach the url for any reason. For instance, if I add my flag then it won’t terminate my script, I can choose to continue if url can’t be reached.

Currently, I have

if "$?" -ne "0" then
echo "can't reach the url, n\ aborting"
exit

Now I need to add a flag through getopts where I can choose to ignore "$?' - ne "0" command,

I don’t know how getopts works, I am pretty new to it. Can someone please help me on how to go about it?

  • 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-06T05:36:12+00:00Added an answer on June 6, 2026 at 5:36 am

    If you only have one option, sometimes it’s simpler to just check $1:

    # put download command here
    if (( $? != 0 )) && [[ $1 != -c ]]; then
        echo -e "can't reach the url, \n aborting"
        exit
    fi
    # put stuff to do if continuing here
    

    If you’re going to accept other options, some possibly with arguments, the getopts should be used:

    #!/bin/bash
    usage () { echo "Here is how to use this program"; }
    
    cont=false
    
    # g and m require arguments, c and h do not, the initial colon is for silent error handling
    options=':cg:hm:' # additional option characters go here
    while getopts $options option
    do
        case $option in
            c  ) cont=true;;
            g  ) echo "The argument for -g is $OPTARG"; g_option=$OPTARG;; #placeholder example
            h  ) usage; exit;;
            m  ) echo "The argument for -m is $OPTARG"; m_option=$OPTARG;; #placeholder example
            # more option processing can go here
            \? ) echo "Unknown option: -$OPTARG"
            :  ) echo "Missing option argument for -$OPTARG";;
            *  ) echo "Unimplimented option: -$OPTARG";;
        esac
    done
    
    shift $(($OPTIND - 1))
    
    # put download command here
    if (( $? != 0 )) && ! $cont; then
        echo -e "can't reach the url, \n aborting"
        exit
    fi
    # put stuff to do if continuing here
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm pretty new to shell scripting and linux in general. Basically, I need to
Pretty new to ASP.NET and trying to figure out how to have the code
Im pretty new to Dreamweaver. Im having a problem. I have a website that
I'm pretty new to Java and SWT, hoping to have one image dissolve into
I have a custom shell script that runs each time a user logs in
Pretty new to the Magento e-commerce solution. Have it up and running for a
I am pretty new to Maven and I have the following question when I
I'm pretty new to using Linux commands. So suppose I have a file with
I'm very new in shell scripting, and I encountered a problem that is quite
Hi I have to execute a shell command :diff <(ssh -n root@10.22.254.34 cat /vms/cloudburst.qcow2.*)

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.