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.
if provide 1 argument, ie if -
sh <scriptname>
, see below error -my-llt-utvsg$ sh temp.sh usage temp.sh
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
Post a Comment