2

I am new to Unix shell scripting and would like some help with writing small script.

I have defined a following synopsis for my script:

install.sh [-h|-a path|[-k path][-f path][-d path][-e path]]

ie user can request some help (-h), install all to a specified place (-a path), or install one or more of a components (-k, -f, -d -e) to a appropriate paths. If there is no arguments, the help should be shown.

Thanks in advance.

Levon
  • 138,105
  • 33
  • 200
  • 191
user1256821
  • 1,158
  • 3
  • 15
  • 35

1 Answers1

5

You can use getopts to parse a command line with bash. Here is an example taken from Bash/Parsing command line arguments using getopts (obviously you'd have to adjust the options to your needs).

#!/bin/bash

#Set a default value for the $cell variable
cell="test"

#Check to see if at least one argument was specified
if [ $# -lt 1 ] ; then
   echo "You must specify at least 1 argument."
   exit 1
fi

#Process the arguments
while getopts c:hin: opt
do
   case "$opt" in
      c) cell=$OPTARG;;
      h) usage;;
      i) info="yes"
      n) name=$OPTARG;;
      \?) usage;;
   esac
done

Related SO question How do I parse command line arguments in bash?

For more information search for getopts on this man page for bash.

Community
  • 1
  • 1
Levon
  • 138,105
  • 33
  • 200
  • 191