help-bash
[Top][All Lists]
Advanced

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

Re: Use `find' "-printf" predicate inside bash script


From: Andreas Kusalananda Kähäri
Subject: Re: Use `find' "-printf" predicate inside bash script
Date: Tue, 19 Oct 2021 23:19:53 +0200

On Tue, Oct 19, 2021 at 07:40:46PM +0000, JB wrote:
> ‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
> 
> > Date: Mon, 18 Oct 2021 16:39:27 -0400
> > From: Greg Wooledge greg@wooledge.org
> > To: help-bash@gnu.org
> > Subject: Re: Use `find' "-printf" predicate inside bash script
> >
> > On Mon, Oct 18, 2021 at 08:24:58PM +0000, JB wrote:
> >
> > >         #!/bin/bash
> > >         args_find='-type f -mtime -1 -delete -printf "deleted file: 
> > > %f\\n"'
> > >         find /tmp/ $args_find
> > >
> >
> > https://mywiki.wooledge.org/BashFAQ/050
> >
> > tl;dr: put the args in an array, not a string variable. Use an array 
> > expansion, not an unquoted string variable expansion.
> 
> Had tried that already but it failed because $args_find[@] wasn't quoted. 
> Didn't test quoting because $args_find[*] broke `find', so I figured 
> double-quoting $args_find[@] would as well. This works:
> 
> find /tmp/ "$args_find[@]"

Note that this is not the correct syntax for expanding the array
args_find.  Also, your message reads as if the complete solution was to
use "$args_find[@]" whereas using the array correctly is only half the
answer.  The other half is to actually create the array.

        args_find=( -type f -mtime -1 -delete -printf 'deleted file: %f\n' )
        find /tmp "${args_find[@]}"


-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.



reply via email to

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