New to shellscripting....need some help!

I have 3 shell scripts below.... I need to write a command line parser
for these 3 utilities.

basically the parser will accept ANY number of commands in this syntax

-r = right -f = findtext -c = count so you do something like

mu.s -r 3 4 5 -f hello goodbye.txt -c *.c -h 5 4 3

right accepts 3 numbers, findtext accepts a key to find, and the file to look in and count accepts any # of file names.....

if a bad command (-h in my example) is given, trash that command and its arguments, and move on to the next valid argument.

Some of the script is off memory. Im not at the computer with the scripts on it.... but they all do work....

Findtext.s

#! /bin/sh

grep -n $1 $2

right.s

#! /bin/sh
##This script I simplified to pseudo code...  I have the correct math ##syntax
##in the file and it works.... I just can't remember it offhand! :(
a=$1*$1
b=$2*$2
c=$3*$3
ab=a+b
ac=a+c
bc=b+c

*insert pythagorean checks here*

count.s

#! /bin/sh
for file in $*; do
        if [ -f "$file" ]; then
        {
                index=`expr $index + 1`
                chars=`wc -c $file | cut -d' ' -f1`
                words=`wc -w $file | cut -d' ' -f1`
                lines=`wc -l $file | cut -d' ' -f1`

                echo "$file data:"

                echo "$chars characters"
                echo "$words word"
  	        echo "$lines lines"
  	        echo " "
         }
         else
        {
                echo "$file does not exist"
        }
        fi

done

There are the 3 scripts. They all do what they should, but I don't know how to do this multi-function setup at all! I did it in C already, so I have somewhat of an idea of what to do, but.....:confused: :frowning: Ive never done shell scripting before...

Thanks for reading/helping! :slight_smile:

simple example of getopts - a lot like what you should use in C see: man 3 getopt

while getopts 2>/dev/null r c
do
# only accept -r as a valid command otherwise display help.
  case $c in
    r) find . -type f | xargs ls -la |cut -c35-45,59-120 \
       | sort -r -n  ; exit 0 ;;
   \?) echo "\nUsage: $0 [-r]
       list the byte count and name of files in current dir sorted by size
       -r recursively lists all of the subdirectories as well" >&2
        exit 2;;
  esac
done

THanks for the reply......! but erm....

I literally just started shell scripting when I started this... so alot of what you posted is confusing me.... can you elaborate some? Im worried that since i used things like $1 and $2, its going to go haywire when its done w/ the multi utility. What if it needs to use $5 and $6 instead?....!

My C version was a while loop with an if/else chain inside it...I see thats whats happening here also :slight_smile:

Let me pop on a unix server and check out the getopt manual... Im not at my linux computer right now...

thanks again ! :slight_smile:

EDIT:
hmm im sort of seeing how this works.. based off the manual, and google!

When I get back to my Linux machine later... ill give it a whirl... but will it be something like

while getopts r:f:c: o
do case "$o" in
r) right.s ;;
f) findtext.s;;
c) count.s;;
?) help like message
esac
done

??

Im not seeing how I will get proper arguments passed to the scripts this way though....

Put your code in [ c o d e ] blocks -see the FAQ.

while getopts r:f:c: o
do case "$o" in
  r) 
    myvariable="right.s"
    anothervariable="0"
    i put more code here
    even more code  here;;
  f) myvariable=findtext.s;;
  c) myvariable=count.s;;
  ?) *help like message*
esac
done

You can put any number of lines in each "case" thingy. Just end the case block with ;;

ah jeeze my bad I thought I did the code blocks....

anyway, I am confused on how to handle the variables with each case in the getopts command....

like, i do

 grep -n $1 $2 

for findtext.

how do I translate that to the f) section of getops?

f)grep -n $1 $2
?....or are there arbitrary index counters I am not keeping track of where I can do something like 
grep -n $x $x+1

?

thanks a ton!

EDIT: $OPTIND and $OPTIND+1

is that the right variable to be dealing with??

Oh the excitement I think im getting somewhere.... if only i could test this out right now!

With this getopts, I am making some nice progress.... but is there a way to dereference $OPTIND? It is the index of the argument, as opposed to the argument...

as soon as I figure this out I believe it might work! :slight_smile: