New to shellscripting....need some help!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting New to shellscripting....need some help!
# 1  
Old 03-04-2008
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

Code:
#! /bin/sh

grep -n $1 $2

right.s
Code:
#! /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

Code:
#! /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.....Smilie Smilie Ive never done shell scripting before...

Thanks for reading/helping! Smilie
# 2  
Old 03-04-2008
simple example of getopts - a lot like what you should use in C see: man 3 getopt
Code:
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

# 3  
Old 03-04-2008
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 Smilie

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

thanks again ! Smilie

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....

Last edited by TurboArkhan; 03-04-2008 at 12:37 PM..
# 4  
Old 03-04-2008
Put your code in [ c o d e ] blocks -see the FAQ.

Code:
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 ;;
# 5  
Old 03-04-2008
Tools

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
Code:
 grep -n $1 $2

for findtext.

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

Code:
f)grep -n $1 $2

?....or are there arbitrary index counters I am not keeping track of where I can do something like
Code:
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!

Last edited by TurboArkhan; 03-04-2008 at 03:03 PM.. Reason: forgot codeblocks because im an idiot.
# 6  
Old 03-04-2008
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! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shellscripting -z option

Can someone explain the -z option in shellscripting. I did this. When I was in both ksh and bash it echoed back hi. #!/bin/ksh if then echo hi fi When I echoed $BASH_VERSION in bash it gave a version. When I echoed $BASH_VERSION in ksh it was blank. I thought -z was... (4 Replies)
Discussion started by: cokedude
4 Replies

2. Shell Programming and Scripting

small error in shellscripting -archival part

Hi All, I have run the below script but getting one small error. please help me to solve this. ERROR: tar: Error exit delayed from previous errors CODE: #! /bin/bash CEP=/home/user01/exercise/CEP ARCH=/home/user01/exercise/archive LOG=/home/user01/exercise/logs... (3 Replies)
Discussion started by: aish11
3 Replies

3. Shell Programming and Scripting

shellscripting to edit xml file

I have an xml file where every line which has the word CDATA in it follows this pattern (line number) <word1><!]></>I need only these lines editing so that the end result is that (line number) <word1><!]></word1>so it copies the first bit to the end. Anyone know how I can do this I am... (10 Replies)
Discussion started by: legolad
10 Replies

4. Shell Programming and Scripting

Timeout in shellscripting

#!/bin/sh for ip in $(cat /root/Desktop/ftp.txt) do HOST=$ip USER='bob' PASS='bob' ftp -n $HOST <<EOF user bob bob EOF echo "$ip" done the Above code i want to use check and verify login works on multiple ftp servers on my network. However the ftp servers are dynamic in setup... (5 Replies)
Discussion started by: Noledge
5 Replies

5. Shell Programming and Scripting

csv file to excel file with shellscripting

Hi all, Please help me out How to write a shell script which converts abc.csv file into abc.xls example file abc.csv AP_RQ_ID,TXN_IER,AP_RS_RESP_CD,TXN_CNT,TOTAL_AMT 60000002,ACTIVATION,00,490,14657.52 60000002,ACTIVATION,08,1,100 This needs to be converted into excel format using a... (3 Replies)
Discussion started by: dudd9
3 Replies

6. Shell Programming and Scripting

New to shellscripting error: ./emailnotifications.sh: line 43: [: FH: integer expression expected

Hi , I'm a beginner in unix shell scripting need help in rectifying an error Source file :test.txt with Header ------ ----- Trailer ex: FH201010250030170000000000000000 abc def jke abr ded etf FE2 I was validating whether the header begin... (2 Replies)
Discussion started by: dudd9
2 Replies

7. Shell Programming and Scripting

Running SQLPLUS from Shellscripting ...

Hi guys, I am facing issues while triggering sqlplus with "/" option I can connect to database with USER ID and password. but, if i just specify "/" option since it is "externally identiifed user" it doesn't work Any suggestions? mail me @ Removed. Regards, Giri (2 Replies)
Discussion started by: chittari
2 Replies

8. Shell Programming and Scripting

shellscripting question

I have been “commissioned” to write a shellscript to automate Oracle cold refeshes.. right now we perform about 20 refresh each 300gb in size which takes up our time. my goal is to write a shellscript which can be executed by a user on serverA to refresh any of the 7 databases on ServerA. I am... (4 Replies)
Discussion started by: jigarlakhani
4 Replies
Login or Register to Ask a Question