Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Need Help With My First Borne Shell Program Post 57645 by FuzzyNips on Thursday 4th of November 2004 12:21:44 AM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
ZGREP(1)						    BSD General Commands Manual 						  ZGREP(1)

NAME
zgrep, zegrep, zfgrep -- print lines matching a pattern in gzip-compressed files SYNOPSIS
zgrep [grep-flags] [--] pattern [files ...] zegrep [grep-flags] [--] pattern [file ...] zfgrep [grep-flags] [--] pattern [file ...] DESCRIPTION
zgrep runs grep(1) on files or stdin, if no files argument is given, after decompressing them with zcat(1). The grep-flags and pattern arguments are passed on to grep(1). If an -e flag is found in the grep-flags, zgrep will not look for a pattern argument. zegrep calls egrep(1), while zfgrep calls fgrep(1). EXIT STATUS
In case of missing arguments or missing pattern, 1 will be returned, otherwise 0. SEE ALSO
egrep(1), fgrep(1), grep(1), gzip(1), zcat(1) AUTHORS
Thomas Klausner <wiz@NetBSD.org> BSD
December 28, 2003 BSD
All times are GMT -4. The time now is 04:39 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy