Difference between revisions of "Bash script template"

From Teknologisk videncenter
Jump to: navigation, search
m (Created page with "Below is a script template with options. <source lang=bash> #!/bin/bash # # FILNAME - SHORT DESCRIPTION # # (c) 2012 - DONALD DUCK <dd@disney.org> - all rights reserved # # THIS ...")
 
m
Line 24: Line 24:
 
N=1    # Default set
 
N=1    # Default set
 
UNIT=""
 
UNIT=""
 +
TEXT="Distance between A and B"
  
 
usage() {
 
usage() {
Line 31: Line 32:
 
         echo "  -r    r flag"
 
         echo "  -r    r flag"
 
         echo "  -c    c flag with value"
 
         echo "  -c    c flag with value"
         echo "  -n    if c flag ...."
+
         echo "  -n    n flag - unset"
 
         echo "  -t    t flag with text"
 
         echo "  -t    t flag with text"
 
         echo "  -h    help"
 
         echo "  -h    help"
 
         echo ""
 
         echo ""
 
         echo "  Description line1"
 
         echo "  Description line1"
     echo "      Description line1"
+
     echo "      Description line2"
 
     exit 1
 
     exit 1
 
}
 
}
Line 42: Line 43:
 
# collect options and override default
 
# collect options and override default
  
while getopts 'c:mkrr:n' CMDLINEARG
+
while getopts 'ct:mkr:n' CMDLINEARG
 
do
 
do
 
         case "${CMDLINEARG}" in
 
         case "${CMDLINEARG}" in
 
         c)
 
         c)
 
                 COUNT=${OPTARG}
 
                 COUNT=${OPTARG}
 +
                ;;
 +
        t)
 +
                TEXT=${OPTARG}
 
                 ;;
 
                 ;;
 
         m|k)
 
         m|k)
Line 66: Line 70:
 
echo -e "Settings"
 
echo -e "Settings"
 
echo -e "COUNT = $COUNT"
 
echo -e "COUNT = $COUNT"
 +
echo -e "TEXT  = $TEXT"
 
echo -e "UNIT  = $UNIT"
 
echo -e "UNIT  = $UNIT"
 
echo -e "R    = $R"
 
echo -e "R    = $R"
 
echo -e "N    = $N"
 
echo -e "N    = $N"
 
echo -e "Done..."
 
echo -e "Done..."
 
+
</source>
 +
Running the script - example
 +
<source lang=bash>
 +
heth@mars:~/bin$ ./bash_template -t "Hyde" -n -m -c 67
 +
Settings
 +
COUNT =
 +
TEXT  = Hyde
 +
UNIT  = m
 +
R    =
 +
N    =
 +
Done...
 
</source>
 
</source>
 
[[Category:bash]][[category:Linux]]
 
[[Category:bash]][[category:Linux]]

Revision as of 07:15, 2 May 2012

Below is a script template with options.

#!/bin/bash
#
# FILNAME - SHORT DESCRIPTION
#
# (c) 2012 - DONALD DUCK <dd@disney.org> - all rights reserved
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.

# default settings
COUNT=0
unset R # Default unset
N=1     # Default set
UNIT=""
TEXT="Distance between A and B"

usage() {
        echo "usage: $0 [-mkrh] [-c count -n] [-t text]"
        echo "  -m    m flag fx. report result in miles"
        echo "  -k    k flag fx. report result in km"
        echo "  -r    r flag"
        echo "  -c    c flag with value"
        echo "  -n    n flag - unset"
        echo "  -t    t flag with text"
        echo "  -h    help"
        echo ""
        echo "  Description line1"
    echo "      Description line2"
    exit 1
}

# collect options and override default

while getopts 'ct:mkr:n' CMDLINEARG
do
        case "${CMDLINEARG}" in
        c)
                COUNT=${OPTARG}
                ;;
        t)
                TEXT=${OPTARG}
                ;;
        m|k)
                [ ! -z "${UNIT}" ] && echo '-m and -k are mutually exclusive. Select miles or km.'\
                        && usage
                UNIT="${CMDLINEARG}"
                ;;
        r)
                R=${CMDLINEARG}
                ;;
        n)
                N=${OPTARG}
                ;;
        h|*)
                usage
                ;;
        esac
done
echo -e "Settings"
echo -e "COUNT = $COUNT"
echo -e "TEXT  = $TEXT"
echo -e "UNIT  = $UNIT"
echo -e "R     = $R"
echo -e "N     = $N"
echo -e "Done..."

Running the script - example

heth@mars:~/bin$ ./bash_template -t "Hyde" -n -m -c 67
Settings
COUNT =
TEXT  = Hyde
UNIT  = m
R     =
N     =
Done...