File "Ageing" Shell Script

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions File "Ageing" Shell Script
# 1  
Old 05-29-2012
File "Ageing" Shell Script

Thank you very much for viewing my post and offering your suggestions & help. I really appreciate it. ~Simba

1. The problem statement, all variables and given/known data:

File "Ageing" Shell Script

Part A) Write a shell script that will count the number of files in your account that were last modified 5 or more days ago. When you run the shell script, the results should look something like this:

%ageing
There are 19 files modified 5 or more days ago.
%

Part B) Modify your script (or re-write it if you wish) to allow the user to choose what the script should use for the last modified day. Example:

%ageing 15
There are 4 files modified 15 or more days ago.

Part C) Modify the script to get it to count back up to "X" number of days and list the files that were modified for each day. Example:

%ageing -3
Files modified 1 day ago
./a.txt
./b.txt

Files modified 2 days ago
./2days.txt
./dir1/test _file.txt
./dir2/test _file2.txt

Files modified 3 days ago
(none)

Note: (you may have to test the results environment variable to
get this to work)

Ageing should still work like it did in Part B if you type this command (note, it is still 3 or more days):

%ageing 3
The number of files modified 3 or more days ago is 4.



2. Relevant commands, code, scripts, algorithms:

I have determined that it is best to use the "Find" command (as a basis) to solve this problem. The script can be written in either bash, bourne shell, or csh, but bourne or bash is best if possible.


3. The attempts at a solution (include all code and scripts):

So far all I have determined is to use the find command. I am new to unix.

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Boston University, Boston, MA, Prof McBee, Unix2201

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 05-29-2012
For Part A:
Code:
man find
       -mmin n
              File's data was last modified n minutes ago.

       -mtime n
              File's data was last modified n*24 hours ago.  See the comments for -atime to understand how rounding affects the interpretation of file  modification
              times.

For Part B:
One alternative is to use positional parameters.

For Part C:
You can enclose the whole logic in an infinite while loop to exit under certain condition. Something like this:
Code:
while true
do
    if [ "$input_value" == "exit" ]
    then
        break
    fi
    <logic>
    <logic>
done

# 3  
Old 05-30-2012
Quote:
Originally Posted by balajesuri
Something like this:
Code:
while true
do
    if [ "$input_value" == "exit" ]
    then
        break
    fi
    <logic>
    <logic>
done

While this will surely work as desired wouldn't it be easier to include the test in the loop already?
Code:
while [ "$input_value" != "exit" ] ; do
    <logic>
    <logic>
done

@Simba-DMB
You might want to read this post for a little introduction on how to use find. Try a few find commands to see the various effects. Once you understand how to use find read the man page of it (issue man find at the command line) to get more clauses to use.

I hope this helps.

bakunin
# 4  
Old 05-30-2012
@bakunin: Sure it'd be easier and less typing. Simba-DMB claims to be new to unix and I didn't want to burden him already with things that are not-so-obvious to a newbie :-)

In my humble opinion,
Code:
while true
do
    if [ "$input_value" == "exit" ]
    then
        break
    fi
    <logic>
done

is more easily understood than

Code:
while [ "$input_value" != "exit" ] ; do
    <logic>
done

# 5  
Old 05-30-2012
Quote:
Originally Posted by balajesuri
In my humble opinion,
[...]
is more easily understood than
hmm.. I came to systems administration via programming and the concept of loop-until/while-some-condition was natural for me. This construct exists in every programming language i know (well, with the exception of APL2 ;-) ).

Still, you might be right. People without a programming background understand your version more easily maybe.

@SimbaDMB: you see, most things you do in Unix can be achieved in a number of different ways. Unix is - you will learn to love this - not only about achieving a certain goal, but doing so in (a certain) style. [philosophical mode on]In fact Unix is as much a culture - a way of life, even - as it is an operating system.[/philosophical mode off].

bakunin
# 6  
Old 05-31-2012
Quote:
Originally Posted by balajesuri
@bakunin: Sure it'd be easier and less typing. Simba-DMB claims to be new to unix and I didn't want to burden him already with things that are not-so-obvious to a newbie :-)

In my humble opinion,
Code:
while true
do
    if [ "$input_value" == "exit" ]
    then
        break
    fi
    <logic>
done

is more easily understood than

Code:
while [ "$input_value" != "exit" ] ; do
    <logic>
done

I disagree. The 'true' makes no sense unless you understand you could put a condition there -- and if you could put a condition there, why not do so?
# 7  
Old 05-31-2012
Bug Here is what I have so far...

First, thanks balajesuri and bakunin. Your posts were a big help!

What I am posting below encompasses parts A, B and C. I still need a little help with part A.
-------------------------------------------------------------------------------
Code:
#!/bin/sh

PastXDays () {
	find . -mtime -$1
	}
	
if [ "$1" = "" ]; then
	echo "These are the files modified within the past 5 days:"  
	PastXDays 5
else
	if [ "$1" -ge 0 ]; then
		echo "These are the files modified within the past $1 days:" 
		PastXDays $1
	else
                neg2pos=`expr $1 - $1 - $1`
		while [ $neg2pos != 0 ] ; do
       		        echo "These are the files modified" $neg2pos "days ago:"
       		        PastXDays $neg2pos
       		        echo ""
       		        neg2pos=`expr $neg2pos - 1`
		done
	fi
fi
-------------------------------------------------------------------

Part A.

The script is called without an argument. It calls the function PastXDays with an argument of 5, giving you the files that were modified in the past 5 days. This is the only part I have incomplete as I need this to be a count rather than a list of files (e.g. the output should be something like this "There are 19 files modified 5 or more days ago.")

If you can offer suggestions to figure this out, I would much appreciate it.

Part B:

The script is called with a positive integer

Code:
if [ "$1" -ge 0 ]; then
	echo "These are the files modified within the past $1 days:" 
	PastXDays $1

It calls the PastXDays function with the positive integer, giving you an output like this:

These are the files modified within the past 3 days:
.
./-1]
./.DS_Store
./525AF2D3F89B4FE6D814B61372989F68B7C94795.torrent
./=
./=0]
./find.txt
./Pilot Guides (Lonely planet)-Nepal.DC.DIVX.avi
./UnixPartA.sh
./UnixPartB.sh
./UnixPartC.sh

Part C:

The script is called with a negative integer.

Code:
else
        neg2pos=`expr $1 - $1 - $1`
	while [ $neg2pos != 0 ] ; do
                echo "These are the files modified" $neg2pos "days ago:"
       		PastXDays $neg2pos
       		echo ""
       		neg2pos=`expr $neg2pos - 1`
        done

I create a variable to reverse the sign of the negative argument. I then run through a loop which runs the function, then decreases the count, and the loop is ran until the count reaches 0.


Thanks again for all your help so far.

Last edited by SimbaDMB; 05-31-2012 at 03:08 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Send an email if "No such file or directory" in the shell script program log in EBS concur

Hi All, I have the below code(.sh) and need to send an email. #!/bin/bash cp /u02/xxc_incoming/TEST*.dat /u02/xxc_archive_incoming/AMER7764_ARPP_2/ cat /u02/xxc_incoming/TEST*.dat > /u02/xxc_incoming/XXC_TEST.dat rm /u02/xxc_incoming/TEST*.dat cd $XXC_TOP/bin sqlldr userid=apps/<pwd> ... (12 Replies)
Discussion started by: Mist123
12 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

6. UNIX for Advanced & Expert Users

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

7. UNIX for Dummies Questions & Answers

Command Character size limit in the "sh" and "bourne" shell

Hi!!.. I would like to know what is maximum character size for a command in the "sh" or "bourne" shell? Thanks in advance.. Roshan. (1 Reply)
Discussion started by: Roshan1286
1 Replies

8. AIX

"too big" and "not enough memory" errors in shell script

Hi, This is odd, however here goes. There are several shell scripts that run in our production environment AIX 595 LPAR m/c, which has sufficient memory 14GB (physical memory) and horsepower 5CPUs. However from time to time we get the following errors in these shell scripts. The time when these... (11 Replies)
Discussion started by: jerardfjay
11 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

10. UNIX for Dummies Questions & Answers

No utpmx entry: you must exec "login" from lowest level "shell"

Hi I have installed solaris 10 on an intel machine. Logged in as root. In CDE, i open terminal session, type login alex (normal user account) and password and i get this message No utpmx entry: you must exec "login" from lowest level "shell" :confused: What i want is: open various... (0 Replies)
Discussion started by: peterpan
0 Replies
Login or Register to Ask a Question