Results 1 to 10 of 14
i have a boot up vscanner i have been scripting and one of the commands i use is a find and remove command, but i want to know if there ...
- 05-09-2007 #1Just Joined!
- Join Date
- Nov 2006
- Posts
- 90
script for find command
i have a boot up vscanner i have been scripting and one of the commands i use is a find and remove command, but i want to know if there is a way to make a script that can be called like /script/command -var that would then remove -var with: find / -iname "var" -exec rm -rv {} \; but the -var could change just by changing the name w/o editing the script. any ideas?
- 05-09-2007 #2Linux Enthusiast
- Join Date
- Aug 2006
- Posts
- 631
Something like:
RegardsCode:#!/bin/bash if [ $# −ne 1 ] then echo "Usage: `basename $0` file-name" exit 1 fi find / -iname "$1" -exec rm -rv {} \;
- 05-11-2007 #3Just Joined!
- Join Date
- Nov 2006
- Posts
- 90
thanks for the help!
- 05-14-2007 #4Just Joined!
- Join Date
- Nov 2006
- Posts
- 90
hey, it said it is expecting a binary on line 3. any ideas why and how to fix?
- 05-14-2007 #5
When I copied the script to test it myself, the "-" from "-ne" vanished. Add it back.
"-ne" is a binary operator that means "is numerical argument 1 not equal to numerical argument 2?" (as opposed to string arguments).DISTRO=Arch
Registered Linux User #388732
- 05-14-2007 #6Just Joined!
- Join Date
- Nov 2006
- Posts
- 90
ok i got it, works beautiful. bye bye windows viruses . thanks!
(heh, id get rid of windows too if the customer would allow it!)
- 05-14-2007 #7Just Joined!
- Join Date
- Nov 2006
- Posts
- 90
ok what if i need to give it a starting location too? like:
"/run/script location file", because i am getting a weird error like the drive is failing trying to start off of /
device i/o error
bad bread(block 51
failed
or something similar.
the computer has two ntfs partitions on ntfs-3g but one is in an extended partition if any of this matters.
- 05-14-2007 #8
The first argument to find is the starting directory. It will take the subtree that starts there and traverse it.
You could specify that directory by using another commandline parameter to the script. This one, not surprisingly, would be called $2.
This is the best resource for Bash scripting that I have ever found:
http://www.tldp.org/LDP/abs/html/
You might read this section, under "positional parameters":
http://www.tldp.org/LDP/abs/html/othertypesv.htmlDISTRO=Arch
Registered Linux User #388732
- 05-14-2007 #9Just Joined!
- Join Date
- Nov 2006
- Posts
- 90
um... learning, but still really not good and i only have internet @ work. show me an example with this one so i can learn based on what changes?
- 05-15-2007 #10
Alrighty. Well, as you will see in find's man page, the first argument to find is the subtree to start at. So the question here is "how are we going to specify this?" This is done using commandline arguments: we can pass the script arguments, and it will use them. We are already doing this: $1 is the first parameter. $2 is the second. So the new script would be:
What have we done? Well $# is the number of commandline arguments. We are requiring 2. We have also replaced '/' with '$2' in the call to find. This is because $2 will have the starting directory stored in it.Code:#!/bin/bash if [ $# −ne 2 ] then echo "Usage: `basename $0` file-name starting-directory" exit 1 fi find "$2" -iname "$1" -exec rm -rv {} \;
Does this all make sense?DISTRO=Arch
Registered Linux User #388732


Reply With Quote