#!/bin/sh loop=0 while [ $# -ne 0 ] do echo $1 shift done
!#/bin/sh # tr_case # convert files to either upper or lower case FILES="" TRCASE="" EXT="" OPT=no # gets called when a conversion fails error_msg() { _FILENAME=$1 echo "`basename $0`: Error the conversion failed on $_FILENAME" } if [ $# -eq 0 ] then echo "For more info try `basename $0` --help" exit 1 fi while [ $# -gt 0 ] do case $1 in #set the variables based on what option was used -u)TRCASE=upper EXT=".UC" OPT=yes shift ;; -l)TRCASE=lower EXT=".LC" OPT=yes shift ;; -help) echo "convert a file(s) to uppercase from lowercase" echo "convert a file(s) from lowercase to uppercase" echo "will convert all characters according to the sepcified command option." echo "where option is" echo "-l Convert to lowercase" echo "-u Convert to uppercase" echo "The original file(s) is not touched. A new file(s)" echo "will be created with either a .UC or .LC extension" echo "usage: $0 -[l|u] file [file..]" exit 0 ;; -*) echo "usage: `basename $0` -[l|u] file [file..]" exit 1 ;; *) # collect the files to process if [ -f $1 ] then # add the filenames to a variable list FILES=$FILES" "$1 else echo "`basename $0` : Error cannot find the file $1" fi shift ;; esac done if [ "$OPT" = "no" ] then echo "`basename $0`: Error you need to specify an option. No action taken" echo "`basename` --help" exit 1 fi # now read in all the file(s) # use the variable LOOP, I just love the word LOOP for LOOP in $FILES do case $TRCASE in lower) cat $LOOP|tr "[a-z]" "[A-Z]" >$LOOP$EXT if [ $? != 0 ] then error_msg $LOOP else echo "Converted file called $LOOP$EXT" fi ;; upper) cat $LOOP|tr "[A-Z]" "[a-z]" > $LOOP$EXT if [ $? != 0 ] then error_msg $LOOP else echo "Converted file called $LOOP$EXT" fi ;; esac done
#!/bin/sh #getopts1 #set the vars ALL=false HELP=false FILE=false VERBROSE=false while getopts ahfgv OPTION do case $OPTION in a)ALL=true echo "ALL is $ALL" ;; h)HELP=true echo "HELP is $HELP" ;; f)FILE=true echo "FILE is $FILE" ;; v)VERBOSE=true echo "VERROSE is $VERROSE" ;; esac done
/home/l/g/tomotoboy/getopts >getopts1 -a ALL is true /home/l/g/tomotoboy/getopts >getopts1 -v VERROSE is /home/l/g/tomotoboy/getopts >getopts1 -v -a -h VERROSE is ALL is true HELP is true
while getopts :ahfgvc: OPTION
#!/bin/sh #getopts1 #set the vars ALL=false HELP=false FILE=false VERBROSE=false COPIES=0 #the value for the -c option is set to zero while getopts :ahfgvc: OPTION do case $OPTION in a)ALL=true echo "ALL is $ALL" ;; h)HELP=true echo "HELP is $HELP" ;; f)FILE=true echo "FILE is $FILE" ;; v)VERBOSE=true echo "VERROSE is $VERROSE" ;; c)COPIES=$OPTARG echo "COPIES is $COPIES" ;; \?) #usage stagement echo "`basename $0` -[a h f v] -[c value] file" >&2 ;; esac done
/home/l/g/tomotoboy/getopts >chmod 755 getopts2 /home/l/g/tomotoboy/getopts >getopts2 -c hello COPIES is hello /home/l/g/tomotoboy/getopts >getopts2 -h -c hello HELP is true COPIES is hello
Powered by: BlogJava Copyright © Gavin.lee