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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:07:12+00:00 2026-06-14T01:07:12+00:00

My shell script takes in a set of input variables some are optional. The

  • 0

My shell script takes in a set of input variables some are optional. The input should follow the following signature.

myscript.sh var1 var2 [-x var3] [-y var4]`.

The -x and -y are optional inputs and these options can occur at any location (between var1 & var 2 or at the start) but var3 will always preceed var4. Also var1 will preceed var 2.

The script I have does a lot of if else checks to cover all the probabilities.
Something like this

if [ $1 == "-x" ]; then
## Then check if $3 == "-y" 
## Assign values to VAR1 , VAR2, VAR3, VAR4 
## else check for other possibilities
fi

I was wondering if there was a better way of doing this and assigning the values to variables?

  • 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-14T01:07:13+00:00Added an answer on June 14, 2026 at 1:07 am

    with a test to ensure that optionals arguments, if provided, are provided only once.

    #!/bin/bash
    
    die() {
        echo >&2 $@
        exit 1
    }
    
    declare -a vars_1_2
    var1='' var2='' var3='' var4=''
    while [ "$1" ];do
        case $1 in
            -x ) [ "$var3" ] && die "Argument '-x' provided twice!"
                 shift
                 var3=$1
                 shift
                 ;;
            -y ) [ "$var4" ] && die "Argument '-y' provided twice!" 
                 shift
                 var4=$1
                 shift
                 ;;
            * ) vars_1_2+=($1) ; shift ;;
        esac
    done
    [ ${#vars_1_2[@]} -ne 2 ] && \
        die "Needed exactly 2 positional arguments. Got ${#vars_1_2[@]}."
    var1=${vars_1_2[0]}
    var2=${vars_1_2[1]}
    set | grep ^var
    

    make:

    $ ./parsargs.sh 12 33 -x 45 -y 67 -x 2
    Argument '-x' provided twice!
    
    $ ./parsargs.sh 12 -x 45 -y 67 
    Needed exactly 2 positional arguments. Got 1.
    
    $ ./parsargs.sh 12 34 -x 45 -y 67 
    var1=12
    var2=34
    var3=45
    var4=67
    vars_1_2=([0]="12" [1]="34")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

<?php // Execute a shell script $dump = shell_exec('bigfile.sh'); // This script takes some
The following shell script takes a list of arguments, turns Unix paths into WINE/Windows
I am writing a shell script that takes file paths as input. For this
I wrote a short shell script that takes in parameters, checks them (because they're
I currently have a request to build a shell script to get some data
I have a shell script that takes one variable as an argument, the way
Working on a shell script that takes a machine name as an argument and
I am trying to write a bourne-shell script that takes a directory as a
I am trying to write a bourne-shell script that takes a string as a
I am trying to do the following task: write a shell script called changedir

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.