Unix run a script with -help option -


i have below script expected work when user invokes sh <scriptname> <propertyfile> work when provide @ dollar prompt. however, having 2 issues script.

  1. if provide 1 argument, ie if - sh <scriptname>, see below error -

    my-llt-utvsg$ sh temp.sh usage temp.sh 
  2. when -help, see below error -

    my-llt-utvsg$ sh tmp.sh -help -help not exist 

what doing wrong? can please advise? software developer needs shell scripting, please go easy on me ;)

#!/bin/bash file="system.properties" file=$1 if [ ! -f $file ];    echo "$file not exist" exit fi usage () {   echo "usage $0 $file"    exit } if [ "$#" -ne 1 ]   usage fi if [ "$1" = "-help" ] ; echo "" echo '############ properties ############    ' echo "" echo 'blah.' exit 

the reason

if [ "$1" = "-help" ] ; 

check not working checks $1 or first argument.

try instead:

for var in "$@"     if [ "$var" = "-help" ] ;         echo ""         echo '############ properties ############    '         echo ""         echo 'blah.'     fi done 

which loop on each argument , run if of them -help.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -