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

  • Home
  • SEARCH
  • 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 8772609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:04:37+00:00 2026-06-13T18:04:37+00:00

I would like to write a script that requires -c and -f where each

  • 0

I would like to write a script that requires -c and -f where each requires an option.

When I run my script below I get some unexpected errors:

$ ./user.sh -c
./user.sh: option requires an argument -- c

Usage: user.sh -c username -f filename
   -c username
   -f SSH public key

$ ./user.sh -c gg
Error: You have not given a filename.

In the first case, I would have liked it said I am missing the option for -c and in the second case I would have liked it said I am missing -f.

Question

How do I make such error handing, and what am I doing wrong?

user.sh

#!/bin/bash                                                                                             

usage () {
    echo "Usage: user.sh -c username -f filename"
    echo "   -c username"
    echo "   -f SSH public key"
    echo ""
}

if ! [ "$*"  ]; then
    usage
    exit 1
fi

while getopts "c:f:" opt; do
    case $opt in
        c) user=$OPTARG;;
        f) filename=$OPTARG;;
        \?)
            echo
            usage
            exit 1;;
        *) echo "Internal error: Unknown option.";;
    esac
done

if ! [ $filename ]; then
    echo "Error: You have not given a filename."
    exit 1
fi

if ! [ $user ]; then
    echo "Error: You have not given an username."
    exit 1
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-06-13T18:04:38+00:00Added an answer on June 13, 2026 at 6:04 pm

    The c: says ‘the -c option must be followed by a username’.

    The error message says ‘the -c option was not followed by a username’.

    Granted, it didn’t mention ‘username’ but that’s because it doesn’t know what it is that follows the option -c.

    The getopts built-in cannot handle mandatory options; you have to code that for yourself by checking that the mandatory options were in fact passed. It also doesn’t worry if the same option is specified twice; your code has to deal with that if it matters. (It’s easy to let the last specified value take effect.)

    Modern style is to avoid option letters before mandatory arguments. I’m not wholly in favour of the change; it means that the ordering of the arguments becomes critical in a way that using option letters to indicate what follows does not. Without option letters, you’d write: ./user.sh username filename, but with option letters, you can write either of these and expect it to work:

    ./user.sh -c username -f filename
    ./user.sh -f filename -c username
    

    Note that the onus is on you to worry about extra arguments too. You’ll typically use:

    shift $(($OPTIND - 1))
    

    to remove the processed arguments, and you can then do:

    case "$#" in
    (0) : No extra arguments - OK;;
    (*) echo "$0: Too many arguments" >&2; exit 1;;
    esac
    

    And variations on that theme. Note that the error report is sent to standard error, not to standard output — the >&2 redirection sends standard output (file descriptor 1) to standard error (file descriptor 2) instead.

    To avoid ambiguity, I’d code your usage function a little differently:

    usage()
    {
        {
        echo "Usage: user.sh -c username -f filename"
        echo "   -c username    Name of user to connect as"
        echo "   -f filename    SSH public key file"
        echo ""
        } >&2
    }
    

    The inner braces do I/O redirection en masse, without starting a subshell. That can be useful when you need to send a number of echo commands to the same place. I’ve also presented the detail information a little differently, so that a user isn’t confused into thinking that ‘SSH public key’ is three arguments to follow the -f. If there were any pure-option flags, they’d be followed by blanks:

        echo "   -V             Print version information and exit"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to write a script which will edit multiple XML files, I
Is Delphi have any ability to write script of IDE actions? I would like
I would like to write a function that creates a plot of a set
I would like to write an if statement that would continue to repeat a
I would like to write a query that returns a single date value, that
I would like to write a cross-platform function in C++ that contains system calls.
I would like to write an if statement that will do something base on
For a JQuery plugin that I am trying to write I would like to
I would like to write an abstract base class class func_double_double_t : public unary_function<double,
I would like to write a League Fixture generator in python, but I can't.

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.