Overclockers Australia Forums
OCAU News - Wiki - QuickLinks - Pix - Sponsors  

Go Back   Overclockers Australia Forums > Software Topics > Programming & Software Development

Notices


Sign up for a free OCAU account and this ad will go away!
Search our forums with Google:
Reply
 
Thread Tools
Old 23rd April 2012, 10:07 PM   #1
TinTin184 Thread Starter
Member
 
TinTin184's Avatar
 
Join Date: Aug 2009
Location: Omicron Persei 8
Posts: 110
Default bash script to only execute if it finds a file

ok I have tried searching google and couldn't find anything helpful so I'm gonna try the hive mind

I'm looking for a script to check a folder for .rar files and if it finds the files to execute a script (or vis versa doesn't really matter) I thought that this script below would work but for some reason the "$?" returns "0" if it finds a .rar file or not, there may just be something wrong with the script or I might have just made a mistake somewhere and I just can't see it. Any help or input is helpful and I'm more than happy to through this script out if there is a better option available.

Code:
for filename in *.rar
do
find $2 -name '*.rar' 
if [ "$?" -ne '*.rar' ]
then
echo norar >> $2/.error
else
#run the script
fi
done
exit 0
to explain $1 is a variable passed onto the script being run if it finds a .rar and $2 is the directory eg /home/user/folder/directory.to.be.checked
__________________
I AM ROOT HEAR ME RAWR
My Rig: AMD Phenom II X4 955 @ 3.2| 8.0GB 1066 DDR2| GA-MA790X-UD4P motherboard|HDDs 500gb raid 1 OS Drive, 1tb Movies, 1tb TV shows, 500gb Downloads, 1tb Games|Gigabyte 5850OC|1 24" Main Screen and 1 24" secondary
My on and off again blog tristan-dawson.com.au
TinTin184 is offline   Reply With Quote

Join OCAU to remove this ad!
Old 24th April 2012, 6:10 AM   #2
bugayev
Whammy!
 
bugayev's Avatar
 
Join Date: May 2003
Location: Melbourne
Posts: 3,983
Default

find $directory_to_look_in -name -'*.rar' -exec "$script_to_execute" {}\;

Dont' understand why you need a script for this.
__________________
Administrator: Apple Desktop Hardware/Software, Graphics and Programming, Digital Art, iOS Devices, For Sale (PC and Non-PC), Price Check and What/Where Should I Buy?
mercury: 11" Macbook Air, i5, 4Gb RAM, 128Gb SSD
lunchbox: Mac mini, i5 Dual Core, 4Gb RAM, 500Gb HDD
bugayev is offline   Reply With Quote
Old 24th April 2012, 9:41 AM   #3
TinTin184 Thread Starter
Member
 
TinTin184's Avatar
 
Join Date: Aug 2009
Location: Omicron Persei 8
Posts: 110
Default

Quote:
Originally Posted by bugayev View Post
find $directory_to_look_in -name -'*.rar' -exec "$script_to_execute" {}\;

Dont' understand why you need a script for this.
I had thought about that but I need the 2 outputs one for failing to find the file and one for finding it as the fail condition writes to a file in the directory which is then picked up by a web interface that shows me the files that where passed the checks and those that failed. That is why I need the script, its automated and goes off without me entering any commands, all I get is the end result once the script has run

But thanks for the idea anyway
__________________
I AM ROOT HEAR ME RAWR
My Rig: AMD Phenom II X4 955 @ 3.2| 8.0GB 1066 DDR2| GA-MA790X-UD4P motherboard|HDDs 500gb raid 1 OS Drive, 1tb Movies, 1tb TV shows, 500gb Downloads, 1tb Games|Gigabyte 5850OC|1 24" Main Screen and 1 24" secondary
My on and off again blog tristan-dawson.com.au
TinTin184 is offline   Reply With Quote
Old 24th April 2012, 10:37 AM   #4
tr3nton
Member
 
Join Date: Oct 2009
Posts: 1,245
Default

Quote:
Originally Posted by TinTin184 View Post
I thought that this script below would work but for some reason the "$?" returns "0" if it finds a .rar file or not,
$? returns the exit code of the last run program.

http://linuxcommand.org/wss0150.php
__________________
Microsoft gives you windows, linux gives you the whole house

Last edited by tr3nton; 24th April 2012 at 10:40 AM.
tr3nton is offline   Reply With Quote
Old 24th April 2012, 8:30 PM   #5
bugayev
Whammy!
 
bugayev's Avatar
 
Join Date: May 2003
Location: Melbourne
Posts: 3,983
Default

Describe what you want to do a bit more - I am still convinced that unless it's enormously complex, you can do it in a one liner.
__________________
Administrator: Apple Desktop Hardware/Software, Graphics and Programming, Digital Art, iOS Devices, For Sale (PC and Non-PC), Price Check and What/Where Should I Buy?
mercury: 11" Macbook Air, i5, 4Gb RAM, 128Gb SSD
lunchbox: Mac mini, i5 Dual Core, 4Gb RAM, 500Gb HDD
bugayev is offline   Reply With Quote
Reply

Bookmarks

Sign up for a free OCAU account and this ad will go away!

Thread Tools

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 On
HTML code is Off

Forum Jump


All times are GMT +10. The time now is 2:08 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd. -
OCAU is not responsible for the content of individual messages posted by others.
Other content copyright Overclockers Australia.
OCAU is hosted by Internode!