help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] help


From: John McKown
Subject: Re: [Help-bash] help
Date: Sun, 6 Mar 2016 16:08:30 -0600

Oh, pooie. I got a typo. Where you see "lt" in the if [ $# lt 2 ], it
should be -lt: if [ $# -lt 2 ]. Silly fingers of mine.

On Sun, Mar 6, 2016 at 4:06 PM, John McKown <address@hidden>
wrote:

> On Sun, Mar 6, 2016 at 9:22 AM, Val Krem <address@hidden> wrote:
>
>> Hi all,
>>
>>
>> I am using this more often in several places.
>>
>> find . -type f -mtime +10 -name "*.txt"
>>
>> So I want to put it in my .bashsrc as (util.sh)
>>
>
> ​I don't understand this. "util.sh" would be a different file from
> ".bashsrc" (I guess you meant ".bashrc"). Do you want a shell script (a
> file) or a BASH function?
>
>
>>
>>
>> #!/bin/bash
>> find . -type f -mtime +10 -name "*.txt"
>>
>> So when I call this script (util.sh) anywhere
>>
>> I want to have three agreements ($1, $2 and $3).
>>
>> $1  takes the time ( +10 or +150 or +200)
>> $2  should take the file name ( "*.txt",  "*.csv", etc)
>> $3  an action such as (the default should list, if there is an argument
>> such as head or tail or even  -delete the delete  those files )
>>
>>
>> Any help how to do this?
>>
>>
> ​You've got it all correct. original command: find . -​type f -mtime +10
> -name "*.txt"
>
> you want the time in $1, so it becomes: find . -type f -mtime $1 -name
> '*.txt"
>
> you want the file name in $2, so it becomes: find . -type f -mtime $1
> -name "$2" #in " just in case of blanks, et al
>
> you want an options portion, like -delete, in $3 and it becomes: find .
> -type f -mtine $1 -name "$2" $3
>
> The simpliest shell script (somewhere on your ${PATH})
>
> #!/bin/bash
> if [ $# lt 2 ]; then
>    echo "Sorry, I need at least two parameters:"
>    echo "The first is the time."
>    echo "The second is the name pattern"
>    exit 1
> fi
> find . -mtime $1 -name "$2" $3
>
> ​Note that the above can actually use mulitple optional parameters, but it
> looks weird:
>
> util.sh +10 "*.txt" "-executable -delete" # delete *.txt files over 10
> days which are marked executable
>
> This works because $3 ("-executable -delete"), when intepreted without
> being enclosed in " marks will expand to two parameter: "-executable" and
> "-delete". Which explains why I usually enclose things in quotes!
>
> If you want the more conventional, and "proper", script, you might do:
>
> #!/bin/bash
> ​if [ $# lt 2 ]; then
>    echo "Sorry, I need at least two parameters:"
>    echo "The first is the time."
>    echo "The second is the name pattern"
>    exit 1
> fi
> time=$1
> shift #move all parameters over 1, dropping $1
> filePattern="$2"
> shift #move all parameters over 1 again
> find . -mtime ${time} -name "${filePattern}" "$@"
>
> The only plus of this is that the invocation looks more "normal"
>
> util.sh +10 "*.txt" -executable -delete
>
> Note in the example the "$@" expands to "-executable" and "-delete" as you
> would want because the two "shift" commands have removed the +10 and
> "*.txt" from the list of parameters.
>
> Now, if you really want this "ability" to be defined in ~/.bashrc, then
> you'll need to define it as a function. Fairly simple to do, with just a
> slight change. Put the following towards the end of ~/.bashrc
>
> function util.sh() {
> f [ $# lt 2 ]; then
>    echo "Sorry, I need at least two parameters:"
>    echo "The first is the time."
>    echo "The second is the name pattern"
>    return 1 #NOTICE! not _exit_ but _return_ for a function
> fi
> time=$1
> shift #move all parameters over 1, dropping $1
> filePattern="$2"
> shift #move all parameters over 1 again
> find . -mtime ${time} -name "${filePattern}" "$@"
> }
>
> ​This will look unusual to most BASH programmers because BASH functions
> generally don't end with a ".sh". I.e. they'd use "util" as the name
> instead of "util.sh". But you may call it want you want, so long as it is a
> syntactically correct name.​
>
>
> --
> A fail-safe circuit will destroy others. -- Klipstein
>
> Maranatha! <><
> John McKown
>



-- 
A fail-safe circuit will destroy others. -- Klipstein

Maranatha! <><
John McKown


reply via email to

[Prev in Thread] Current Thread [Next in Thread]