Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Need Help With My First Borne Shell Program Post 57695 by zazzybob on Thursday 4th of November 2004 05:44:33 PM
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..
 

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
escape(1)							Mail Avenger 0.8.3							 escape(1)

NAME
escape - escape shell special characters in a string SYNOPSIS
escape string DESCRIPTION
escape prepends a "" character to all shell special characters in string, making it safe to compose a shell command with the result. EXAMPLES
The following is a contrived example showing how one can unintentionally end up executing the contents of a string: $ var='; echo gotcha!' $ eval echo hi $var hi gotcha! $ Using escape, one can avoid executing the contents of $var: $ eval echo hi `escape "$var"` hi ; echo gotcha! $ A less contrived example is passing arguments to Mail Avenger bodytest commands containing possibly unsafe environment variables. For example, you might write a hypothetical reject_bcc script to reject mail not explicitly addressed to the recipient: #!/bin/sh formail -x to -x cc -x resent-to -x resent-cc | fgrep "$1" > /dev/null && exit 0 echo "<$1>.. address does not accept blind carbon copies" exit 100 To invoke this script, passing it the recipient address as an argument, you would need to put the following in your Mail Avenger rcpt script: bodytest reject_bcc `escape "$RECIPIENT"` SEE ALSO
avenger(1), The Mail Avenger home page: <http://www.mailavenger.org/>. BUGS
escape is designed for the Bourne shell, which is what Mail Avenger scripts use. escape might or might not work with other shells. AUTHOR
David Mazieres Mail Avenger 0.8.3 2012-04-05 escape(1)
All times are GMT -4. The time now is 11:26 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy