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:
______________________________________________
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
moxxx68
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.
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....
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!
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)
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)
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)
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)
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)
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)
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)
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)
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)