Forum Thread: Bash Script

I am making a bash script to find a file/directory. I am begining bash and i picked this as a start to scripting. My script is not changing directories to root, it is but it isn't. I googled my problem and tried making a function like someone mentioned but it did not work for me.

I got rid of some comments for easier reading

#!/bin/bash
#Find files and directories

#VARIABLES
#==================
User=$USER #variable for the enviromental variable $USER (Who you are)
PresDir=$PWD #Variable for the enviromental variable $PWD (Present working directory)
Root=/ #Variable for root directory
#==================

clear #Clears screen providing a clean slate to work on

echo "
===================================================
Hello $User, enter a file to search for.
==================================================="

read WANTEDFILE

sudo find $Root $WANTEDFILE | grep $WANTEDFILE

$PresDir #Print Our Pres Directory

sleep 2 #Sleep 2 seconds before continuing commands

clear #Clears the screen presenting the food. . . file on a silver platter

ls -l $WANTEDFILE #Long lists the WANTEDFILE

exit 0

13 Responses

Did it run at all? Or did it just show and vanish in a half a second?
What is $WANTEDFILE BTW?

Exactly what do you want this script to do? Just find a file or folder?

Yes it runs and searches for what i type into the terminal after the "Hello $User" statement, read takes what i type and stores it into $WANTEDFILE which is a variable that i defined after the read command.

I run it, and it waits for me to type the file i want to find after the "Hello $User" statement, it then comes up with the following errors:

IptableChart
/home/kalfy/Documents/IptableChart
find: `IptableChart': No such file or directory
./InteractiveSearch: line 21: /home/kalfy/BashScripts/BashScriptProto: Is a directory

It must change directories because i execute it in BashScriptProto, and it shows the path to IptableChart. But the $PWD shows that ./InteractiveSearch line 21 is were we are after it executes the commands just before we list the file.

My goal of this script is to present the user with a welcoming message, and to take whatever they type as input and have the script find that file on the computer then print it out in long list format. I was wanting it to find both directories and files.

I tried making root a string like Cracker | Hacker mentioned but it did not work. i tried both single and double quotes

Cool I'll work this out for ya and post it back in a few minutes.. Maybe it's what you want, maybe I may have to tweak it some..

Thank you for the help. I will be working on it tomorrow so i may get it, may need to read more into bash scripting instead.

Take a look. It may be what you kinda wanted.

sudo find $Root $WANTEDFILE | grep $WANTEDFILE

Is that grep needed? Aren't you searching for the file that you want to find, and then showing only things pertaining to that file? Well wouldn't that work in itself instead of needing to grep out the file?

I will examine and build/change the script as i can. I don't need the present directory part, i just put it in to see if CD had changed directories to the root directory. If i make any significant changes, which after seeing all you done is doubtful that my changes will be significant i'll post the final one.

No the grep is not needed, for some reason i came to the thought it would stop the find command from posting all the directories it goes through which it doesn't. Find was probably posting the directories it couldn't access without sudo when i come to think of it (permission denied).

#!/bin/bash
#Find files and directories

#VARIABLES
#==================
User=$USER #variable for the enviromental variable $USER (Who you are)
Root=/ #Variable for root directory
#==================

clear #Clears screen providing a clean slate to work on

echo "
===================================================
Hello $User, enter a file to search for.
==================================================="

read WANTEDFILE

clear && echo -e "\n\n\tThis is not the file you are searching for...\n\n\t" && sudo find $Root -iname $WANTEDFILE -exec ls -l {} \;

exit 0

Why is "Root" a variable? Isn't the root directory always the same?

Hi OTW, yes its not needed. i just left it in there because it was in the original script.

Of course a "find / (...)" would do the same.

Yeah I just re worte it the way he wanted. I saw no use for it either, swapped out his find for the correct one. find / -f -name blah blah blah

Share Your Thoughts

  • Hot
  • Active