LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-12-2012, 02:24 PM   #1
kevorski
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Rep: Reputation: Disabled
New to Shell Script


Hey guys, I'm new to this website and new to shell scripting. I'm currently in a C/Unix class learning, well C and Unix. Currently we have an assignment that we are working on:

Quote:
Write a shell script called maxFileSize.sh such that:
If the user provides a number as an argument ($1), that is the number of levels to traverse
The user may not provide a number as an argument that is greater than 4 or less than 0
If the user does not provide an argument, find the file in the current working directory that is the largest file and echo the long listing for the file to the user.
If the user does provide an argument, recurs all directories up to $1 levels and for each directory, provide the listing for the largest file in that directory.
I'm not asking for someone to do my homework for me. I'm trying to find some tips and direction on this assignment. Anything you guys can help me with would be great. Like I said I'm completely new to shell scripting and have some example .sh files. Any tutorials would be great also! Thanks in advance! Again I'm not looking for someone to do my homework for me I would just like some direction.
 
Old 10-12-2012, 02:35 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Sure, we're not goin to finish your homework
Apart joking, you should better go through some shell scripting stuff so you can make some basic understanding.
In order to accomplish your task, read usage of "find" command and about simple/advanced filers like, grep, cut, sed etc.
Further, file size can be found using "find" or "du" commands.
Let me know how else I can help you..
 
Old 10-12-2012, 02:38 PM   #3
JaseP
Senior Member
 
Registered: Jun 2002
Location: Eastern PA, USA
Distribution: K/Ubuntu 12.04/14.04, Scientific Linux 6.3/6.4, Android-x86, Pretty much all distros at one point...
Posts: 1,799

Rep: Reputation: 157Reputation: 157
See;
http://www.freeos.com/guides/lsst/

http://www.linux-tutorial.info/modul...tent&pageid=33

For starters...
 
Old 10-12-2012, 05:34 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,327

Rep: Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881
Quote:
Originally Posted by kevorski View Post
Hey guys, I'm new to this website and new to shell scripting. I'm currently in a C/Unix class learning, well C and Unix. Currently we have an assignment that we are working on:

I'm not asking for someone to do my homework for me. I'm trying to find some tips and direction on this assignment. Anything you guys can help me with would be great. Like I said I'm completely new to shell scripting and have some example .sh files. Any tutorials would be great also! Thanks in advance! Again I'm not looking for someone to do my homework for me I would just like some direction.
Thanks for putting it like this...as is frequently said here, we're always happy to help, but never happy to DO it with no effort put forth from the original posters side of things.

Let's go step by step:
Quote:
If the user provides a number as an argument ($1), that is the number of levels to traverse
Basic programming: if something can ONLY be a number, check to make sure it IS only a number before continuing. Error out if it's not. Since you know it's the first argument ($1). There are ways to easily do this in bash. This is ONLY ONE..read up on regex:
Code:
if ! [[ "$1" =~ ^[0-9]+$ ]] ; then
   exec >&2; echo "error: Not a number"; exit 1
fi
Quote:
The user may not provide a number as an argument that is greater than 4 or less than 0
You've tested to make sure it's a number between 0 and 9 above, so you know it's not a negative or a letter. Do a greater than/less than test here.
Quote:
If the user does not provide an argument, find the file in the current working directory that is the largest file and echo the long listing for the file to the user.
Again, a basic test is called for....check to see if $1 equals null. If so, check the current working directory.
Quote:
If the user does provide an argument, recurs all directories up to $1 levels and for each directory, provide the listing for the largest file in that directory.
The user put something between 0-4 in. Read the man page on the ls command...pay attention to the options...like the "-l", "-S", and "-h". Also read the man page on the head command (to get the first xx files), and the cut command (so you can extract just the file names).

http://tldp.org/LDP/abs/html/
 
Old 10-12-2012, 11:58 PM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
In addition to TB0ne's link I would add the following one to use along side it:

http://mywiki.wooledge.org/TitleIndex
 
Old 10-13-2012, 01:13 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
See faq #3 from the above for some tips on sorting the file list by various properties.

How can I find the latest (newest, earliest, oldest) file in a directory?
http://mywiki.wooledge.org/BashFAQ/003


Also pay special attention to the pitfalls section.


And as mentioned, you'll probably need to use find. Check out the -maxdepth option in particular.

http://mywiki.wooledge.org/UsingFind
http://www.grymoire.com/Unix/Find.html
 
Old 10-14-2012, 02:30 PM   #7
kevorski
LQ Newbie
 
Registered: Oct 2012
Posts: 2

Original Poster
Rep: Reputation: Disabled
Thanks for all the help fellas. I am reading over the stuff now, I will update you guys with all the stuff that I can. I'm thinking that I might have to skip this assignment due to lack of time....I'll let you guys know. If I could get someone to show me the results later tonight that would help. Not worried about the points just want to see the solution.
 
Old 10-14-2012, 02:37 PM   #8
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 18,327

Rep: Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881Reputation: 3881
Quote:
Originally Posted by kevorski View Post
Thanks for all the help fellas. I am reading over the stuff now, I will update you guys with all the stuff that I can. I'm thinking that I might have to skip this assignment due to lack of time....I'll let you guys know. If I could get someone to show me the results later tonight that would help. Not worried about the points just want to see the solution.
Well, you've been given alot of pointers. As said before, we will be glad to HELP...so post what you've written/tried so far, and where you're stuck.
 
  


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
How to pass command line arguments from one shell script to another shell script VijayaRaghavanLakshman Linux - Newbie 5 01-20-2012 10:12 PM
Executing a Shell script with 654 permissions inside another shell script. changusee2k Linux - Newbie 2 06-07-2011 08:58 PM
Shell script calling shell script - List of all nikunjbadjatya Programming 7 04-13-2011 07:27 PM
shell script problem, want to use shell script auto update IP~! singying304 Programming 4 11-29-2005 06:32 PM


All times are GMT -5. The time now is 05:07 PM.

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