[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

how to define arg in bash

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
linuxpig
Asked:
linuxpig
1 Solution
 
ozoCommented:
find . -type f -mtime +$1-mtime -$2
0
 
omarfaridCommented:
shell refer to arguments as positional arguments and they are accessed by names like $1 $2 $3 ...
0
 
linuxpigAuthor Commented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
TintinCommented:

#!/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
 
linuxpigAuthor Commented:
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
 
TintinCommented:
linuxpig.

That's correct.
0
 
linuxpigAuthor Commented:
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
 
TintinCommented:
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
 
linuxpigAuthor Commented:
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
 
TintinCommented:
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

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now