Taula de continguts

getopts

buen tutorial: https://wiki.bash-hackers.org/howto/getopts_tutorial

uso

/via: https://stackoverflow.com/questions/16483119/an-example-of-how-to-use-getopts-in-bash

ejemplos

primer ejemplo
#!/bin/bash
 
usage() { echo "Usage: $0 [-s <45|90>] [-p <string>]" 1>&2; exit 1; }
 
while getopts ":s:p:" o; do
    case "${o}" in
        s)
            s=${OPTARG}
            ((s == 45 || s == 90)) || usage
            ;;
        p)
            p=${OPTARG}
            ;;
        *)
            usage
            ;;
    esac
done
shift $((OPTIND-1))
 
if [ -z "${s}" ] || [ -z "${p}" ]; then
    usage
fi
 
echo "s = ${s}"
echo "p = ${p}"
#!/bin/bash
 
while getopts ":a:" opt; do
  case $opt in
    a)
      echo "-a was triggered, Parameter: $OPTARG" >&2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done