Need Help With My First Borne Shell Program


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Need Help With My First Borne Shell Program
# 1  
Old 11-04-2004
Need Help With My First Borne Shell Program

I'm working on writing my very first borne shell program and I need some help. I think I'm pretty close to having this correct but I may be off. I think my actual program is coded correctly but the commands I use within it I think are what's throwing it off?


**Purpose of the program: To find all files in a directory that contain a given string.


This program runs with only 1 or 2 arguments. The first argument can only consist of upper/lower-case letters. This is the string that you'll be searching for within the directory. If only one command line argument is given you recursively search all files and subdirectories within your current working directory. You then output all files containing the search pattern.

If two arguments are supplied. The first one is still the same as above (the string you're searching for) but the second argument becomes the directory you want to search. Again, I need to output the files that contain the search pattern.

Obviously this involves checking the arguments to make sure they are valid. Argument 1 must be an upper/lower-case letter and argument 2 (if supplied) must be a relative/absolute pathname to an existing directory.


Here's what I have so far:
______________________________________________
Code:
#!/bin/sh

# Checks for valid number of command arguments
if [ $# -eq 0 -o  $# -gt 2 ]; then
        echo "Invalid number of arguments:  1 or 2 only!"
        exit 1
fi

case $# in

"1")  # Checks for validity of command argument 1
        echo $1 | egrep -si "^[a-z]+$"

        # Error message for invalid argument 1
        if [ $? -ne 0 ]; then
                echo "Invalid argument 1:  Letters only!"
                exit 2
        fi

        # Searches through all files in the current working                      
        # directory
        for i in `find . -type f -print`
        do
                # If current file is readable then search for the   
                # string
                if [ -r $i ]; then
                        grep -li "$1 " $i
                fi
        done

"2")  # Checks for validity of command argument 1
        echo $1 | egrep -si "^[a-z]+$"

        # Error message for invalid argument 1
        if [ $? -ne 0 ]; then
                echo "Invalid argument 1:  Letters only!"
                exit 2
        fi

        # Checks for validity of command argument 2
        find $2 -type d

        # Error message for invalid argument 2
        if [ $? -ne 0 ]; then
                echo "Invalid argument 2:  Directory path only!"
                exit 3
        fi

        # Searches through all files in the directory specified
        for i in `find .$2  -type f -print`
        do
                # If current file is readable then search for the         
                # string
                if [ -r $i ]; then
                        grep -li "$1 " $i
                fi
        done;;
esac

exit 0

# 2  
Old 11-04-2004
MySQL according to the book so far!

Smilie i think you forgot :: after the first done.. the usual syntax for case
case in
1)
::
2)
::
esac
the script is basically a perfect example of how a script should be written according to the books that I have read.. I would go into more detail but I had the most fun figuring out my first shell scripts doing it by myself.. unless I was really 100% stuck.. I would say try and figure out the mistake if there is one by yourself and keep in mind that this is a good script
have fun
moxxx68Smilie
# 3  
Old 11-04-2004
I've changed a couple of bits and it now appears to be working as you'd want it.

Rather than using a find to test if $2 is a directory, I've used the test command ([). I've also slightly simplified your other finds, and edited the second find (you had .$2).

You can see i've redirected the output of the egreps to /dev/null to stop them echoing to the screen - but that's of course up to you.

I've removed the space following $1 in the grep. If you want to search for a "word", you could use \<$1\>.

I've also added the missing ;; as moxx suggested.

Code:
#!/bin/sh

# Checks for valid number of command arguments
if [ $# -eq 0 -o  $# -gt 2 ]; then
        echo "Invalid number of arguments:  1 or 2 only!"
        exit 1
fi

case $# in

"1")  # Checks for validity of command argument 1
        echo $1 | egrep -si "^[a-z]+$" >/dev/null 2>&1

        # Error message for invalid argument 1
        if [ $? -ne 0 ]; then
                echo "Invalid argument 1:  Letters only!"
                exit 2
        fi

        # Searches through all files in the current working
        # directory
        for i in `find . -print`
        do
                # If current file is readable then search for the
                # string
                if [ -r $i ]; then
                        grep -li "$1" $i
                fi
        done;;

"2")  # Checks for validity of command argument 1
        echo $1 | egrep -si "^[a-z]+$" >/dev/null 2>&1

        # Error message for invalid argument 1
        if [ $? -ne 0 ]; then
                echo "Invalid argument 1:  Letters only!"
                exit 2
        fi

        # Checks for validity of command argument 2
        if [ ! -d "$2" ]; then
           echo "Invalid argument 2:  Directory path only!"
           exit 3
        fi

        # Searches through all files in the directory specified
        for i in `find $2 -print`
        do
                # If current file is readable then search for the
                # string
                if [ -r $i ]; then
                        grep -li "$1" $i
                fi
        done;;
esac

exit 0

You are writing a very nice script, I hope I've helped you out a bit! Keep up the good work.

EDIT: Re-read your original post and put the recursion in....

Cheers
ZB

Last edited by zazzybob; 11-04-2004 at 06:52 PM..
# 4  
Old 11-04-2004
I wanted to say thanks to both of you. After reading moxxx68's post I stuck with it and got things working correctly. Zazzybob, I just looked over your post and mine looks really similar to yours (including dumping the standard errors to /dev/null which we just learned today and was added to the requirements). I did take some of your suggestions and made some changes to make things a little neater. Again, I appreciate the help from both of you in making my first script a success!Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

C shell program

1. I've have to write a shell program that accepts Ctrl+T (in linux os in c language) and should print out the current time and date to the screen. I've written the following code but i've to type ^T individual rather than pressing ctrl+T(^T) to get the output. : 2. How do i make the shell... (2 Replies)
Discussion started by: zorro_phu
2 Replies

2. Shell Programming and Scripting

I am getting strange message when run borne shell script

I have a code: if then#{ process daily files for file in *_${Today}*.csv *_${Today}*.txt do if || then echo "This file will be processed in separate script" continue fi if ;then ... (2 Replies)
Discussion started by: digioleg54
2 Replies

3. Shell Programming and Scripting

Shell Program , need help!!

Hi all, I am trying to get a file from an ftp server and i have the list of files which needs to be get from the ftp server. grep unix_prg*.* log.txt > log1.txt log1.txt (which has the list of files) 06-29-09 00:00AM 3550258 unix_prg090629 06-28-09 07:00PM ... (7 Replies)
Discussion started by: raghav1982
7 Replies

4. UNIX for Dummies Questions & Answers

borne shell script

I need anwser to this qestion! I havewirte a script that provides line numbered contents of a file which must make use of the following control structures files Command line arguments I am a complete new commer to unix and bourne shell scripting can any one help (1 Reply)
Discussion started by: migg-21
1 Replies

5. Shell Programming and Scripting

very first shell program.

in the beginners book i have it gives an exercise to try. saying to make a script that examines the time. it should keep examining every second or so and say some sort of message. Can anyone help me get going. Thanks (3 Replies)
Discussion started by: bebop1111116
3 Replies

6. Shell Programming and Scripting

Documenattion on Borne And C shell programming

Hi experts, I am new bee in unix programming. How to differenciate a Borne and C shell programming. Can i write a C shell syntax in Borne again shell. Please send me the good links on Borne and Cshell programming . Any help in this regard will be highly appreciated. Regards, Azaz. (3 Replies)
Discussion started by: azazalis
3 Replies

7. Shell Programming and Scripting

UNIX Find command in Borne Shell

I need to perform two separate commands as part of the -exec section of a find command. Is this possible? (1 Reply)
Discussion started by: marshaferry
1 Replies

8. Shell Programming and Scripting

Can there be multi-dimensional variable arrays in borne shell?

Hello - I've serached the web but can't find much on array script variables (except that C-shell variables are arrays!) I'm trying to form a 2-D string array: (this is what I want, but in java) String list = { {"one", "two"}, {"three"} }; I know this is a 1-D string array shell... (4 Replies)
Discussion started by: jparker
4 Replies

9. Shell Programming and Scripting

Help me with this Shell Program

Now, am in a very tight situation here. I really dont expect anyone to understand but please, try your best. am trying to right a program that goes back to the previous entry to correct a mistake. heres what am trying to do. i write a program like this Name : James Holgston... (1 Reply)
Discussion started by: TRUEST
1 Replies

10. UNIX for Dummies Questions & Answers

Modifying from borne shell to C shell

Just want to know a few conversion tricks. in Borne Shell, I have the line: if test -s testmap then ... fi ## testmap is a filename and I wanna test whether it exists ## then do whatever How can I convert that to C Shell? I've tried: if (test -s testmap) then ... endif but it... (3 Replies)
Discussion started by: zenkisoft
3 Replies
Login or Register to Ask a Question