User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,438 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,650 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 9777 | Replies: 7
Reply
Join Date: Jul 2004
Location: Oklahoma City, OK
Posts: 15
Reputation: tccummings is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
tccummings tccummings is offline Offline
Newbie Poster

Solution Bash shell screipt help

  #1  
Jul 16th, 2004
Hi everyone. I am fairly new to Linux and I am taking a UNIX course. I have been asked to write a shell script and I am having a little trouble. I was wondering if anyone could help me. Here are my instructions --

create a script called command find_big.sh that recursively finds file equals or exceed X bytes and outputs the names to stdout. The shell shall take 2 arguments: 1 the location to start looking; 2 the size to equal or exceed. Example bash find_big.sh /lib 20000.
---Hint you shell should begin
#!/bin/bash
#validate there are 2 arguments passed
#validate argument 1 exists and display an error if it does not
#validate argument 2 is a number and display error if no


And this is all I've come up with so far.

#!/bin/bash
#validate there are 2 arguments passed
#validate argument 1 exists and display an error if it does not
#validate argument 2 is a number and display error if not
directory=$1
size=$5
ls -l $1 | sort -n -k 5 | awk '{if($5>=size){print $9$5}}'

My if statement in awk is obviously not working. The if statements confuse me some (I have yet to make one work). I think I am trying to make this harder than it is. What I have so far will list any directory sorted by file size, but I'm not sure how to pull our the files that are greater than or equal to my second argument and just list them. I was hoping to make the listing part of this work first then go back and insert the argument validations. I've just gotten myself more confused though. I seem to learn better through reverse engineering for some reason.

If anyone could help, I'd be grateful.

Thanks,

Tim Cummings
tim.cummings@cox.net
Oklahoma City
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jul 2004
Location: Oklahoma City, OK
Posts: 15
Reputation: tccummings is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
tccummings tccummings is offline Offline
Newbie Poster

Help Re: Bash shell screipt help

  #2  
Jul 16th, 2004
Ok, I've gotten myself further now, but I'm still a little stuck. I have this done so far:

#!/bin/bash
#validate there are 2 arguments passed
#validate argument 1 exists and display an error if it does not
#validate argument 2 is a number and display error if not
directory=$1
size=$2
ls -l -R $1 | sort -n -k 5 | awk '{print $9$5}'
if test -d $1; then directory=$1; else echo "Directory invalid"
fi
if test $size -ge 0; then size=$2; else echo "Specify a file size"
fi


So basically I list the directory and if either argument is not there the script displays an error at the end, but how can I pull the file size out of the listing and only display those files with the specified sizes from argument 2?
Reply With Quote  
Join Date: Mar 2004
Posts: 1,514
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Rep Power: 10
Solved Threads: 49
Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: Bash shell screipt help

  #3  
Jul 17th, 2004
Hello,

grep or sed might have some functionality you are looking for. Also consider ls -al instead of ls -l because you will miss anything in a hidden directory (a .directory) if you are just in the -l mode.

This is a doozie of a project. Too sleepy to think right now. Grr.

Christian
Reply With Quote  
Join Date: Jul 2004
Location: Oklahoma City, OK
Posts: 15
Reputation: tccummings is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
tccummings tccummings is offline Offline
Newbie Poster

Re: Bash shell script help

  #4  
Jul 17th, 2004
Thanks for your input. I think I'm dancing all around it. I've put grep in between my sort and awk commands, but I can't get the format down. I think it should be something like grep -r if $size>=$2, but that somehow screws up my second argument because then I get the error "grep: 5000: No such file or diretory" when I type ./find_big.sh /boot 5000 at the command line. I've tried switching it around to be $2>=$size and a few other things. I think this statement is all I lack from making this script successful, but I just can't make my brain see it. This is due by Sunday and I think I'm starting to panic a little. I don't usually take this long in figuring something out. I'm just frustrated with this, but at the same timeI want to learn this because I reallize how important programming can be. We move on to Perl next week and I'm afraid if I don't get this I might miss out on something in Perl.
Reply With Quote  
Join Date: Jul 2004
Location: Oklahoma City, OK
Posts: 15
Reputation: tccummings is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
tccummings tccummings is offline Offline
Newbie Poster

Solution Re: Bash shell screipt help

  #5  
Jul 18th, 2004
I got it figured out another (simpler) way, although I would still like help figuring my original way out. What I ended up with was:

#!/bin/bash
#validate there are 2 arguments passed
#validate argument 1 exists and display an error if it does not
#validate argument 2 is a number and display error if not
directory=$1
size=$2
find $1 -size +$2c
if test -d $1; then directory=$1; else echo "Directory invalid"
fi
if test $size -ge 0; then size=$2; else echo "Specify a file size"
fi
:mrgreen:Tim Cummings
Reply With Quote  
Join Date: Mar 2004
Posts: 1,514
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Rep Power: 10
Solved Threads: 49
Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: Bash shell screipt help

  #6  
Jul 19th, 2004
Hello Tim,

Great work on solving it. Yes, find would work too. I should have thought of that. Damn.

Great work!

Christian
Reply With Quote  
Join Date: Jul 2004
Location: Oklahoma City, OK
Posts: 15
Reputation: tccummings is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
tccummings tccummings is offline Offline
Newbie Poster

Solution Re: Bash shell screipt help

  #7  
Jul 19th, 2004
#!/bin/bash
#validate there are 2 arguments passed
#validate argument 1 exists and display an error if it does not
#validate argument 2 is a number and display error if not
directory=$1
size=$2
if test -d $1; then directory=$1; else echo "Directory invalid"
fi
if test $size -ge 0; then size=$2; else echo "Specify a file size"
fi
find $1 -size $2c -o $2c



I forgot to add the part that makes it list the exact size and anything greater.
I also switched the test to happen first to validate my arguments before running find.
:mrgreen:Tim Cummings
Reply With Quote  
Join Date: Jul 2004
Location: Oklahoma City, OK
Posts: 15
Reputation: tccummings is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
tccummings tccummings is offline Offline
Newbie Poster

Re: Bash shell screipt help

  #8  
Jul 19th, 2004
Originally Posted by kc0arf
Hello Tim,

Great work on solving it. Yes, find would work too. I should have thought of that. Damn.

Great work!

Christian


Thanks Christian. I beat myself over the head repeatedly when I saw the easier solution. Man I don't know why I always make this harder than it needs to be.
:o
:mrgreen:Tim Cummings
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 1:40 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC