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.

0

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.