help-bash
[Top][All Lists]
Advanced

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

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


From: JB
Subject: Use `find' "-printf" predicate inside bash script
Date: Tue, 19 Oct 2021 19:21:47 +0000

‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐

> Date: Mon, 18 Oct 2021 22:35:06 +0200
> From: Alex fxmbsw7 Ratchev fxmbsw7@gmail.com
> To: JB freebsdlists.admin@protonmail.com
> Cc: help-bash help-bash@gnu.org
> Subject: Re: Use `find' "-printf" predicate inside bash script
>
> fyi, it works only if written separatly cause one would need eval to make 
> bash parse the quotes u uses inside
>
> the better approach is array elements, one per arg
>
> find=( find -opt1 -opt2 sub2 )
> "${find[@]}"
>
> On Mon, Oct 18, 2021, 22:31 JB freebsdlists.admin@protonmail.com wrote:
>
> > This `find' command works on the command line:
> >
> >         find /tmp/ -type f -mtime -1 -delete -printf "deleted file: %f\\n"
> >
> > But it doesn't work inside this script:
> >
> >         #!/bin/bash
> >         args_find='-type f -mtime -1 -delete -printf "deleted file: %f\\n"'
> >         find /tmp/ $args_find
> >
> > It only works when written like this:
> >
> >         #!/bin/bash
> >         args_find="-type f -mtime -1 -delete -printf"
> >         args_print="deleted file: %f\\n"
> >         find /tmp/ $args_find "$args_print"
> >
> > I've tried using arrays with both "@" and "*", but same result.
> > I tried escaping the double-quotes, but `find' complains: warning: 
> > unrecognized escape` \"'
> > paths must precede expression: `file'
> >
> > What's the proper way to do this inside a variable which doesn't trip up 
> > "-printf"?
> >
> > Sent with ProtonMail Secure Email.
> --

I said in the original message I tried arrays but the command still failed. 
Adding double-quotes as you did solved it. Thanks.

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

Sent with ProtonMail Secure Email.



reply via email to

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