Solved

how to define arg in bash

Posted on 2008-10-20
10
465 Views
Last Modified: 2010-04-21
Please tell me what is wrong with this script. I want to define what is passed to +mtime and -mtime command line. So it would run like:

script.sh 10 20

Then it would list anything older than 10 days (+mtime) and shorter than 60 days.

find . -type f \( -mtime +$args 1 -o -mtime +$ARGS[1] \) -printf "%Tw %p\n" | \
grep -v '^0' | cut -c3- | xargs ls -l
0
Comment
Question by:linuxpig
10 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 22764331
find . -type f -mtime +$1-mtime -$2
0
 
LVL 40

Expert Comment

by:omarfarid
ID: 22764400
shell refer to arguments as positional arguments and they are accessed by names like $1 $2 $3 ...
0
 

Author Comment

by:linuxpig
ID: 22764684
Looking at the find command itself, with the two mtimes, will it keep anything within 14 days and a sunday and remove everything else is the mtimes are 14 and 60 as below?

find . -type f \( -mtime +14 -o -mtime +60 \) -printf "%Tw %p\n" | grep -v '^0' | cut -c3- | xargs rm -rf
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22764712

#!/bin/bash

find . -type f \( -mtime +$1 -o -mtime +$2 \) -printf "%Tw %p\n"|grep -v "^0" | cut -c3- | exec ls -l

Open in new window

0
 

Author Comment

by:linuxpig
ID: 22766389
Thanks tintin,

But that will do what i want it to do right? Using the two mtimes will  keep anything within 14 days and a sunday and remove everything else is the mtimes are 14 and 60 on command line as below?

scomescript.sh 14 60
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 48

Expert Comment

by:Tintin
ID: 22770125
linuxpig.

That's correct.
0
 

Author Comment

by:linuxpig
ID: 22772354
If i want to define the arguments below to be entered command line, will what i have below work?
I want to be able to type the command as:

somescript.sh 10 60 Sun

Where 10 is the first mtime, 60 is the second, Sun is the day to define in the grep -v '^$3'

Sun=0
Mon=1
Tues=2
Wed=3
Thurs=4
Fri=5
Sat=6

find /local/backup/uni* -type f \( -mtime +$1 -o -mtime +$2 \) -printf "%Tw %p\n" | grep -v '^$3' | cut -c3- | xargs ls -la
0
 
LVL 48

Accepted Solution

by:
Tintin earned 50 total points
ID: 22772916
I noticed that the mtime logic is incorrect.  It needs to be

-mtime +$1 -a -mtime -$2

To add the 3rd parameter, you can do
find /local/backup/uni* -type f \( -mtime +$1 -a -mtime -$2 \) -printf "%Ta %p\n" | grep -v "^$3" | cut -c5-  | xargs ls -l

Open in new window

0
 

Author Closing Comment

by:linuxpig
ID: 31508141
Actually -a wont work, -o will, ive tested it already and i also tried the grep you listed, it doesnt do anything but return a 0
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22773121
I beg to differ with your assessment.

If you call your script with

somescript.sh 10 60 Sun

then the logic you have is files that have been modified greater than 10 days ago OR modified greater than 60 days ago will be listed.  The 2nd -mtime is made redundant in this case.

Also, I can not see anyway possible, you would be getting the grep to return 0.  Can you please show an example where that behaviour occurs.

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now