Solved

how to define arg in bash

Posted on 2008-10-20
10
478 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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