1

I am designing a script to install a lot of my common applications but I was wondering if there is a way to add a -t tag to go a different part of a script so -t called the tutorial and nothing just lists help for it?

2

Yes, but you would have to do some command line parsing.

This is without using getopt:

case "$1" in
  -t) do_tutorial
      exit 0 ;;
  -i) do_install "$2"
      exit 0 ;;
  -h) do_usage
      exit 0 ;;
  *)  do_usage >&2
      exit 1 ;;
esac

This is just an example but it will take the first thing after the script name on the command line ($1, the flag) and depending on its value it will do different things. Then you would have functions that implement the different behaviours that you need for supporting each flag on the command line, like e.g.

do_usage () {
  cat <<USAGE_END
Usage:  script [ -i "component" | -h | -t ]

Options:

  -i "component"    Install "component"
  -h                Show this help text
  -t                Show tutorial

USAGE_END
}

Look into getopt or Bash's getopts for how to do more complicated command line parsing.

One simple example of getopts usage: https://unix.stackexchange.com/a/292242/116858

  • I have read over this many times could you make one with only one variable and explain what each parts do? I am very new to this and I find it very Confusing – 9291Sam May 11 '17 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.