Solved

how to define arg in bash

Posted on 2008-10-20
10
468 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
weird behavior after adding color to bash prompt 4 73
Basic linux/ awk Question 5 43
Parse DNS log 3 53
Linux "time" command output redirection 16 121
Utilizing an array to gracefully append to a list of EmailAddresses
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

867 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

21 Experts available now in Live!

Get 1:1 Help Now