Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Bash script parameters

farenda 2015-08-31 0

Problem:

How to process Bash script parameters? There are two ways – using while-case combo or with Bash getopts as we show in the following example.

Solution:

Basically there are two ways to process Bash script parameters:

  1. Using Bash case statement inside while loop.
  2. Using Bash getopts built-in command.

In the post Bash case statement we’ve shown how to process the parameters with while-loop case-statement combo. In this post we’re going to show how to use getopts to do that.

#!/bin/bash

function usage() {
    echo "$0 demonstrates Bash getopts command usage"
    exit 1
}

function version() {
    echo "$0 - version: 0.0.1"
    exit 1
}

OPTIND=1
while getopts "d:o:hv" opt; do
    case "$opt" in
        d) TARGET_DIR="$OPTARG";;
        o) OPTIONS="$OPTARG";;
        v) version;;
        h) usage;;
    esac
done

echo "Options: ${OPTIONS}"
echo "Directory: ${TARGET_DIR}"

getopts takes a string of chars, which will be script options. Options that require arguments are followed by “:” (colon). Else just option char is needed. Read option is put in given variable (here opt) and its argument (if any) lands in OPTARG variable. OPTIND is an index of currently processed option and has to be reset everytime getopts is called.

And here’s result of processing a couple of script parameters:

$> ./getopts.sh
Options:
Directory:
$> ./getopts.sh -h
./getopts.sh demonstrates Bash getopts command usage
$> ./getopts.sh -i
./getopts.sh: illegal option -- i
Options:
Directory:
$> ./getopts.sh -o
./getopts.sh: option requires an argument -- o
Options:
Directory:
$> ./getopts.sh -o 42
Options: 42
Directory:
$> ./getopts.sh -o 42 -d /tmp
Options: 42
Directory: /tmp
$> ./getopts.sh -v
./getopts.sh - version: 0.0.1

As you can see Bash getopts automatically handles missing option arguments or illegal options.

If short options are enough, then go ahead with Bash getopts, but when you need more sophisticated parsing, like long, multi-word options, then use while case combo.

Share with the World!
Categories Bash Tags bash
Previous: Spring inject null value
Next: Java Formattable example

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored