LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   New to Shell Script (https://www.linuxquestions.org/questions/linux-newbie-8/new-to-shell-script-4175431893/)

kevorski 10-12-2012 01:24 PM

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.

shivaa 10-12-2012 01:35 PM

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..

JaseP 10-12-2012 01:38 PM

See;
http://www.freeos.com/guides/lsst/

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

For starters...

TB0ne 10-12-2012 04:34 PM

Quote:

Originally Posted by kevorski (Post 4804143)
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/

grail 10-12-2012 10:58 PM

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

http://mywiki.wooledge.org/TitleIndex

David the H. 10-13-2012 12:13 PM

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

kevorski 10-14-2012 01:30 PM

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.

TB0ne 10-14-2012 01:37 PM

Quote:

Originally Posted by kevorski (Post 4805548)
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.


All times are GMT -5. The time now is 03:56 PM.