LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-16-2001, 10:41 PM   #1
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Rep: Reputation: 30
Cool shell scripts


hullo ppl!
it's been a long time since i've been here...

anyways, need some help on a simple shell script i'm working on...

basically this is what the script does:
1.) get 2 parameters (error message and exit if less than or more than 2) from the user
2.) first parameter is to get a directory (error message and exit if first parameter is not a directory)
3.) second parameter is to get filesize (error message and exit if second parameter is not a number)
4.) output is to print all the files in the stated directory(parameter one) that have a file size smaller than or equal to the 2nd parameter.

I have completed 1.), 2.) and 3.). but 4.) baffles me... i can do this program in C (ie. i know the logic) but i think i need more familiarity with the commands used in Linux to write scripts.

in any case, can i have some help here please? thanks in advance!
 
Old 10-16-2001, 10:46 PM   #2
isajera
Senior Member
 
Registered: Jun 2001
Location: San Antonio, TX
Distribution: distro? what's a distro?
Posts: 1,635

Rep: Reputation: 45
can you post the shell script you have so far? it's a lot easier to work with code than to try to explain how to change it...
 
Old 10-16-2001, 10:59 PM   #3
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Original Poster
Rep: Reputation: 30
ok here's the script:

##########################################
# error checking for number of parameters
##########################################

# if number of parameters exceed 2, display error message
if test $# -gt 2
then
echo
echo ======
echo ERROR!
echo ======
echo Too many parameters!
echo Please enter only 2 parameters after the script command
echo
exit 1
fi

# if number of parameters less than 2, display error message
if test $# -lt 2
then
echo
echo ======
echo ERROR!
echo ======
echo Too few parameters!
echo Please enter the name of the directory as your 1st parameter
echo and the file size you would like to request as your 2nd parameter
echo

exit 1
fi


if test -d $1 # if the first parameter is a directory
then
echo
echo == Directory passed successfully ==
echo

########################################
########################################
# code for checking file size and displaying the list of files in
# directory $1 with file size smaller than the 2nd parameter
########################################
########################################

else
echo
echo ======
echo ERROR!
echo ======
echo First parameter passed is not a directory
echo Please type the name of a directory as your first parameter
echo
fi


exit 0
 
Old 10-17-2001, 02:24 AM   #4
isajera
Senior Member
 
Registered: Jun 2001
Location: San Antonio, TX
Distribution: distro? what's a distro?
Posts: 1,635

Rep: Reputation: 45
add this to the end of the script:

for n in ~/$1/*.*
do
size=$(filesize $n)
if [ $size -lt $2]; then
echo $n
fi
done

-that's pretty much it. you might want to add some additional error checking... make sure $n isn't a directory... make sure $size exists before the -lt statement. but it should be functional.
 
Old 10-17-2001, 04:19 AM   #5
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Original Poster
Rep: Reputation: 30
thank you!
i donno if it works yet, i'll go and try it this evening when i get home...
thanks for the help! appreciate it much!
i'll add in the error checks as well...
 
Old 10-20-2001, 12:23 AM   #6
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Original Poster
Rep: Reputation: 30
Unhappy didn't work

hey there... your script didn't work... you have a some stuff in the middle of your script... like () and ;
these symbols were flagged as errors when i tried to run the script.

help would be really appreciated...
 
Old 10-20-2001, 12:29 AM   #7
isajera
Senior Member
 
Registered: Jun 2001
Location: San Antonio, TX
Distribution: distro? what's a distro?
Posts: 1,635

Rep: Reputation: 45
well... ran it again on my computer, under bash and sh, and it worked fine... so i don't know exactly where the problem is.


i placed my segment just before the

exit 0

but after the last fi - is that where you added it in?

Last edited by isajera; 10-20-2001 at 12:34 AM.
 
Old 10-20-2001, 05:57 AM   #8
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Original Poster
Rep: Reputation: 30
baffled

hiya...

yea i tried putting it at the back of the script just before 'exit 0' and after the last 'fi'...

by the way, is 'filesize' your own utility? coz i can't seem to find it on the man pages... and it flags me an error on that too...

the script is still giving me errors. the full error messages are:

filesize: command not found
[: missing ']'

thanks for your help... pardon me for takin up your time.
 
Old 10-22-2001, 05:47 PM   #9
isajera
Senior Member
 
Registered: Jun 2001
Location: San Antonio, TX
Distribution: distro? what's a distro?
Posts: 1,635

Rep: Reputation: 45
no... it's not my own util - it came with my distro. it's a separate program tho. shoulda seen that coming...

ummm... the only thing i can think of is to have the script parse an ls -l to get the filesize. i'm honestly not sure how to do that outside of sed or awk tho.
 
Old 10-23-2001, 10:53 AM   #10
gui10
Member
 
Registered: Mar 2001
Distribution: enigma, slack8
Posts: 677

Original Poster
Rep: Reputation: 30
thanks for all the help... yea it's puzzling the hell out of me, i've tried all sorts of ways to solve this one (shows my incompetence now... lol).
sorry for not informing u earlier that all i am allowed to use are the basic bash commands. i didn't see that one coming either
how we live in a world of preassumptions

once again, thanks... and if anyone can figure this one out, you're most welcome to try... would appreciate the help as well

meanwhile, i'll be working on it...
 
Old 10-28-2001, 02:46 AM   #11
shmoo28
LQ Newbie
 
Registered: Oct 2001
Location: LA,CA
Distribution: Slackware 7.1, if you can call what I do with it "use"
Posts: 7

Rep: Reputation: 0
Found this at http://moria.org/lg/issue19/lg_tips19.html#track

#!/bin/bash
# This script monitors the size of the files given
# on the command line.
while :
do
clear
for i in $@; do
echo File $i has the size `ls -l $i | tr -s " " | cut -f 5 -d " "` bytes;
done
sleep 1
done


I think this basically parses the output from the ls -l as isajera suggested. I haven't tried it, but I skimmed the man pages for tr and cut and it seems reasonable to me. Good luck!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Need help with shell scripts thorney Linux - Newbie 4 11-27-2005 11:18 PM
shell scripts rocketgo Linux - General 10 12-01-2003 05:20 AM
Shell Scripts benwy Linux - Software 1 06-09-2003 02:58 AM
Shell scripts j0hn0n1 Linux - Software 1 01-03-2003 03:11 PM
shell scripts nautilus_1987 Linux - General 3 08-30-2002 03:12 AM


All times are GMT -5. The time now is 05:51 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration