LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-06-2003, 02:26 AM   #1
sharathkv
Member
 
Registered: Jul 2003
Distribution: HP-UX
Posts: 35

Rep: Reputation: 15
Search for a file using shell script


Hi,

I am new to shell scripts and wrote a very basic script as follows:



#bash script
#Script to search for a file given its filename as a parameter.

if($1 == 0)
print ' No arguments specified ';
exit;
else
find / '!' -type d -name $1 -print0

# End of Script



However I am getting errors when I try to execute the above
script.

Also I am getting "permission denied" message on lots of
folders.

How can I write a script that truly searches the entire drive for
the filename specified without any access problems?


Any help is appreciated.
Thanks
Sharath
 
Old 08-06-2003, 04:50 AM   #2
arobic
Member
 
Registered: Jul 2003
Location: Geneva, Switzerland
Distribution: Debian 3.1, SLC3 (based on RHEL)
Posts: 84

Rep: Reputation: 15
>#bash script
instead of this, you should put:
#!/bin/sh (for bash shell)
>#Script to search for a file given its filename as a parameter.
>
>if($1 == 0)
if [$1 == 0]; then
>print ' No arguments specified ';
echo "No arguments specified"
>exit;
exit 0
>else
>find / '!' -type d -name $1 -print0
>
exit 0
># End of Script
This should work (I hope!)
You can also look at this web page for bash scripts:
http://theory.uwinnipeg.ca/UNIXhelp/...hell_help.html

and for your permission denied problems, you can only avoid these problems by being root while executing your script, or change the permissions on all folders! There is no way out!

Hope this help! I am not an expert myself, but I am trying hard!

Cheers
Andree
 
Old 08-06-2003, 05:05 AM   #3
jharris
Senior Member
 
Registered: May 2001
Location: Bristol, UK
Distribution: Slackware, Fedora, RHES
Posts: 2,243

Rep: Reputation: 47
You can loose the error message if you wish by redirecting STDERR to null in your shell script. So you would use find / '!' -type d -name $1 -print0 2> /dev/null

You say that your script will search for files yeah? Uhm... No... You appear to be searching for directories (-type d) if you want to look for just files then you want -type f

cheers

Jamie...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 12:13 AM
Script to search and replace in text file - kinda... jeffreybluml Programming 45 11-07-2004 05:37 PM
Search and replace text in file using shell script? matthurne Linux - Software 2 11-02-2004 10:11 AM
Help with file search and destroy script flippy Programming 5 07-25-2003 04:29 PM
Shell-script: search item in any order macone Programming 3 08-13-2002 10:44 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration