2

I have a bash script in which I want to check if a flag with value was passed to it and then use the value in a variable. Something like this (pseudocode):

file.sh -c 1.0.0

inside file.sh :

#!/bin/bash

get flag:
if flag 'c' then 
curl c
else 
curl 'something else'

Whats the most optimal way to do the above?

codeforester
  • 34,080
  • 14
  • 96
  • 122
Scooby
  • 3,105
  • 7
  • 40
  • 79

1 Answers1

3

try the following

#!/bin/bash


while getopts ":c" opt; do
 case $opt in
 c)
    echo "-c was triggered!" >&2
    ;;
 \?)
    echo "Invalid option: -$OPTARG" >&2
    ;;
 esac
done
Tharanga Abeyseela
  • 3,154
  • 3
  • 30
  • 42