Quick question: how to call a script function from "find"?

classic Classic list List threaded Threaded
59 messages Options
123
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Quick question: how to call a script function from "find"?

Carlos E. R.-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



Hi,

I have this script:

#!/bin/bash

function doall()
{
   NOMBRE="$*"
   echo $NOMBRE
}

find /home/cer/Fusion/Videos/ -type d -exec doall '{}' \;



But I get this error message:

find: ‘doall’: No such file or directory
find: ‘doall’: No such file or directory
find: ‘doall’: No such file or directory



It is not recognising that "doall" is a function in the same script;
instead it tries to call an external command.

How can I do this?

(Yes, I did try looking 'man', did not seem to find whatever I need to
find).

Of course, I can write it up in two scripts, but seems ugly.

- --
Cheers

  Carlos E. R.
  (from 42.2 x86_64 "Malachite" at Telcontar)


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iEUEARECAAYFAlk/G0cACgkQtTMYHG2NR9Wf8wCfe7LZDeVf2kuPme7BxzV1Ky+7
ttgAl267YI6l92xtDlFO6QE/78vks+4=
=fVjk
-----END PGP SIGNATURE-----
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Bernhard Voelker
On 06/13/2017 12:52 AM, Carlos E. R. wrote:

> I have this script:
>
> #!/bin/bash
>
> function doall()
> {
>   NOMBRE="$*"
>   echo $NOMBRE
> }
>
> find /home/cer/Fusion/Videos/ -type d -exec doall '{}' \;
>
>
>
> But I get this error message:
>
> find: ‘doall’: No such file or directory
> find: ‘doall’: No such file or directory
> find: ‘doall’: No such file or directory

This is because doall is a shell function defined in the outer shell
by default only.

> It is not recognising that "doall" is a function in the same script; instead it tries to call an external command.
>
> How can I do this?

Well, you could export the function definition to child shell processes

  $ export -f doall

and the find command would look like this:

  find /home/cer/Fusion/Videos/ -type d -exec sh -c 'doall "{}"' \;

However, this would launch one shell process per file, and I don't
think that is what you would like to have.

>
> (Yes, I did try looking 'man', did not seem to find whatever I need to find).

man bash - search for "export -f"

> Of course, I can write it up in two scripts, but seems ugly.

Depends - whatever it is doing.
You could launch a shell, but define the shell function inside it:

  find /home/cer/Fusion/Videos/ -type d -exec sh -c '\
    doall() {
      NOMBRE="$@"
      echo "$NOMBRE"
    }
    doall "$@"
    ' '{}' +

Of course, quoting is a bit more tricky. ;-)
To set how it works, you could add "set -x" somewhere in the shell code.

For further improvements, we'd need to know what 'doall()' is really
doing - it seems you have stripped it down to something reproducible.

Have fun,
Berny


--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Carlos E. R.-2
On 2017-06-13 02:01, Bernhard Voelker wrote:

> On 06/13/2017 12:52 AM, Carlos E. R. wrote:
>> I have this script:
>>
>> #!/bin/bash
>>
>> function doall()
>> {
>>   NOMBRE="$*"
>>   echo $NOMBRE
>> }
>>
>> find /home/cer/Fusion/Videos/ -type d -exec doall '{}' \;
>>
>>
>>
>> But I get this error message:
>>
>> find: ‘doall’: No such file or directory
>> find: ‘doall’: No such file or directory
>> find: ‘doall’: No such file or directory
>
> This is because doall is a shell function defined in the outer shell
> by default only.
>
>> It is not recognising that "doall" is a function in the same script; instead it tries to call an external command.
>>
>> How can I do this?
>
> Well, you could export the function definition to child shell processes
>
>   $ export -f doall
>
> and the find command would look like this:
>
>   find /home/cer/Fusion/Videos/ -type d -exec sh -c 'doall "{}"' \;
>
> However, this would launch one shell process per file, and I don't
> think that is what you would like to have.
Huh.

I've never had to do anything like that in any script. It is "find" which is special.


>> (Yes, I did try looking 'man', did not seem to find whatever I need to find).
>
> man bash - search for "export -f"

Well, of course, "man find" would say nothing about that...



>> Of course, I can write it up in two scripts, but seems ugly.
>
> Depends - whatever it is doing.
> You could launch a shell, but define the shell function inside it:
>
>   find /home/cer/Fusion/Videos/ -type d -exec sh -c '\
>     doall() {
>       NOMBRE="$@"
>       echo "$NOMBRE"
>     }
>     doall "$@"
>     ' '{}' +
>
> Of course, quoting is a bit more tricky. ;-)
> To set how it works, you could add "set -x" somewhere in the shell code.
>
> For further improvements, we'd need to know what 'doall()' is really
> doing - it seems you have stripped it down to something reproducible.
No, I haven't. doall is not written yet. I go by steps. The script only has what you see.


The intention was to convert:

find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer:cer '{}' \;
find /home/cer/Fusion/Videos/ -type d -exec chmod u+r+w+x,g+w+x,o+r-w-x,+t '{}' \;
find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer-g:cer '{}' \;
#setfacl -m u:wwwrun:rx  /data/waterhoard/Fusion.uno/Videos

to a single "find". And calling a function in the same script seemed to me the clearest way of doing it.


--
Cheers / Saludos,

                Carlos E. R.
                (from 42.2 x86_64 "Malachite" at Telcontar)


signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Bernhard Voelker
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 06/13/2017 05:38 AM, Carlos E. R. wrote:
> The intention was to convert:
>
> find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer:cer '{}' \; find /home/cer/Fusion/Videos/ -type d -exec
> chmod u+r+w+x,g+w+x,o+r-w-x,+t '{}' \; find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer-g:cer '{}' \;
> #setfacl -m u:wwwrun:rx  /data/waterhoard/Fusion.uno/Videos

I'm not sure why you would want to chown to 'cer' and later to 'cer-g'.
This looks redundant to me.

> to a single "find". And calling a function in the same script seemed to me the clearest way of doing it.

He, he - what about script recursion? ;-)

  #!/bin/bash

  if [ "$1" = '__do_action__' ]; then

    shift 1 \
      && sudo chown cer:cer "$@" \
      && chmod u+r+w+x,g+w+x,o+r-w-x,+t "$@" \
      && sudo chown cer-g:cer "$@"
    exit $?

  fi

  find /home/cer/Fusion/Videos/ -type d -exec "$0" __do_action__ '{}' +

(The above is untested, as I don't have users 'cer', 'cer-g', etc.,
 but it should work.)

Have a nice day,
Berny

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBAgAGBQJZP4tfAAoJEEZQLveWkXGVdR4H+gKBstuUyzB7cjBQY00yKX0z
akOUWzSXSdWI88SY+sYHqwkp90kCckfVVAuWxNUg4+s5S2mWJfU1eVH2gLX6KV4A
9UVCu4APUVoys/YgPEeoenhwPbHviQjlH+1kmpGlHvko8qQ4O82nrLgJLFCH//7y
kCm0nBFIF4Qmhp8u4dQSMliRwg9rJ4yoGPGI6RzvKsH9lqeLQ5LihWDPHWZjb3sK
rLjsJlot59+3sYl4R4uKdcAGl9I0sbvnUpayA9jCvSFh4Zp2gpQsftZXpRZ76WkS
HfOjpZVpMUy8KoBkybyROfTvKI3bbON1y7h5H8CSN4a5M449p2Wkb1xtr2ZB9o0=
=aFVu
-----END PGP SIGNATURE-----

--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Bernhard Voelker
In reply to this post by Carlos E. R.-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 06/13/2017 05:38 AM, Carlos E. R. wrote:
> The intention was to convert:
>
> find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer:cer '{}' \; find /home/cer/Fusion/Videos/ -type d -exec
> chmod u+r+w+x,g+w+x,o+r-w-x,+t '{}' \; find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer-g:cer '{}' \;
> #setfacl -m u:wwwrun:rx  /data/waterhoard/Fusion.uno/Videos
>
> to a single "find". And calling a function in the same script seemed to me the clearest way of doing it.

Another way:
this only work if you stick to the single-argument "\;" (instead of "+"),
because then find(1) will replace every occurrence of '{}' in the
command to be executed:

  find /home/cer/Fusion/Videos/ -type d -exec \
    sh -c "
      sudo chown cer:cer '{}' &&
      chmod u+r+w+x,g+w+x,o+r-w-x,+t '{}' &&
      sudo chown cer-g:cer '{}'
      " \;


Yet another way: use multiple -exec arguments to one find(1):

  find /home/cer/Fusion/Videos/ -type d          \
    -exec sudo chown cer:cer '{}' \;             \
    -exec chmod u+r+w+x,g+w+x,o+r-w-x,+t '{}' \; \
    -exec sudo chown cer-g:cer '{}' \;

Have fun,
Berny

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQEcBAEBAgAGBQJZP45QAAoJEEZQLveWkXGV6BcH/3a3acTjze3l0iDdCHmL5Xz2
ElneKZQyzcePdx9RJaEsr8YbuY3sVT5q85c5VKsbZ0KhGYeiAeLvhW2/AFRxdALR
4e5N3f8McOJJJn+BrMqwhoIWaZ6q7gfZfNQFsgybTWRz0l5QfEosAJNgACkPbVX2
Y8AoR7OioM6wVzSAikqwVPcKcT4H52EL9uxEcTGYLQC64Tain5xBFzUss1VP46zW
cup2iQCwrLXTva2++S5ouWm1CYDSSQwfPaXQoCL4jBtKCWwdWU6tLcVP0aeggQYY
3TRRX2rVNM3NMyI0i6rl7fii4ebnV8KKotKbXQBXIyBx0aGWC5DcSooNQiWkylE=
=bLR6
-----END PGP SIGNATURE-----

--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Per Jessen
In reply to this post by Carlos E. R.-2
Carlos E. R. wrote:

> The intention was to convert:
>
> find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer:cer '{}' \;
> find /home/cer/Fusion/Videos/ -type d -exec chmod
> u+r+w+x,g+w+x,o+r-w-x,+t '{}' \;
> find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer-g:cer '{}'
\;
> #setfacl -m u:wwwrun:rx  /data/waterhoard/Fusion.uno/Videos
>
> to a single "find". And calling a function in the same script seemed
> to me the clearest way of doing it.

I usually tend towards 'xargs' because it can be parallelised, but in
this case, I would probably do this:

find /home/cer/Fusion/Videos/ -type d |\
while read d;
do
    sudo chown cer:cer $d
    chmod u+r+w+x,g+w+x,o+r-w-x,+t $d
    sudo chown cer-g:cer $d
    and-whatever-else-you-want.
done



--
Per Jessen, Zürich (21.4°C)
http://www.dns24.ch/ - free dynamic DNS, made in Switzerland.


--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

L A Walsh
In reply to this post by Carlos E. R.-2
Carlos E. R. wrote:
> find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer-g:cer '{}' \;
> #setfacl -m u:wwwrun:rx  /data/waterhoard/Fusion.uno/Videos
>  
First two questions:
is 'cer-g' a user? What are you wanting 'change-owner' to do
to the file?

Is #setfacl a comment?

You seem to be trying to set things on the directories, but
not setting access on anything in those directories  Is that right?



--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Carlos E. R.-2
In reply to this post by Bernhard Voelker
On 2017-06-13 08:51, Bernhard Voelker wrote:
> On 06/13/2017 05:38 AM, Carlos E. R. wrote:
>> The intention was to convert:
>
>> find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer:cer '{}' \; find /home/cer/Fusion/Videos/ -type d -exec
>> chmod u+r+w+x,g+w+x,o+r-w-x,+t '{}' \; find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer-g:cer '{}' \;
>> #setfacl -m u:wwwrun:rx  /data/waterhoard/Fusion.uno/Videos
>
> I'm not sure why you would want to chown to 'cer' and later to 'cer-g'.
> This looks redundant to me.

Because on a second run of the script, the chmod would error on
directories already owned by "cer-g". Just a hack. The correct method
would be to check the permissions first, and change only if "incorrect".

Easier coding as compared to faster run - the time to do the correct
code doesn't seem worth it ;-)

And then I would have to investigate if find can do a search on all that
criteria. Probably so.

>> to a single "find". And calling a function in the same script seemed to me the clearest way of doing it.
>
> He, he - what about script recursion? ;-)

What? :-O

>   #!/bin/bash
>
>   if [ "$1" = '__do_action__' ]; then
>
>     shift 1 \
>       && sudo chown cer:cer "$@" \
>       && chmod u+r+w+x,g+w+x,o+r-w-x,+t "$@" \
>       && sudo chown cer-g:cer "$@"
>     exit $?
>
>   fi
>
>   find /home/cer/Fusion/Videos/ -type d -exec "$0" __do_action__ '{}' +
>
> (The above is untested, as I don't have users 'cer', 'cer-g', etc.,
>  but it should work.)
Oh :-O

Interesting!

$@?  I don't know what that one does. [searching...]

       @      Expands  to the positional parameters, starting from one.
When the expansion occurs within double quotes, each parameter expands
to a separate word. That is, "$@" is equivalent to "$1" "$2" ...  If the
double-quoted expansion occurs within a word, the expansion of the
first  parameter  is  joined with  the beginning part of the original
word, and the expansion of the last parameter is joined with the last
part of the original word.  When there are no positional parameters,
"$@" and $@ expand to nothing (i.e., they are removed).


I don't quite see the difference with $* :-?


--
Cheers / Saludos,

                Carlos E. R.
                (from 42.2 x86_64 "Malachite" at Telcontar)


signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Carlos E. R.-2
In reply to this post by Bernhard Voelker
On 2017-06-13 09:03, Bernhard Voelker wrote:

> On 06/13/2017 05:38 AM, Carlos E. R. wrote:
>> The intention was to convert:
>
>> find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer:cer '{}' \; find /home/cer/Fusion/Videos/ -type d -exec
>> chmod u+r+w+x,g+w+x,o+r-w-x,+t '{}' \; find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer-g:cer '{}' \;
>> #setfacl -m u:wwwrun:rx  /data/waterhoard/Fusion.uno/Videos
>
>> to a single "find". And calling a function in the same script seemed to me the clearest way of doing it.
>
> Another way:
> this only work if you stick to the single-argument "\;" (instead of "+"),
> because then find(1) will replace every occurrence of '{}' in the
> command to be executed:
>
>   find /home/cer/Fusion/Videos/ -type d -exec \
>     sh -c "
>       sudo chown cer:cer '{}' &&
>       chmod u+r+w+x,g+w+x,o+r-w-x,+t '{}' &&
>       sudo chown cer-g:cer '{}'
>       " \;
This seems nice. I suppose it doesn't mater that the directories found
have spaces?

>
>
> Yet another way: use multiple -exec arguments to one find(1):
>
>   find /home/cer/Fusion/Videos/ -type d          \
>     -exec sudo chown cer:cer '{}' \;             \
>     -exec chmod u+r+w+x,g+w+x,o+r-w-x,+t '{}' \; \
>     -exec sudo chown cer-g:cer '{}' \;

I like this one best. :-)

--
Cheers / Saludos,

                Carlos E. R.
                (from 42.2 x86_64 "Malachite" at Telcontar)


signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Per Jessen
In reply to this post by Carlos E. R.-2
Carlos E. R. wrote:

> $@?  I don't know what that one does. [searching...]
>
>        @      Expands  to the positional parameters, starting from
>        one.
> When the expansion occurs within double quotes, each parameter expands
> to a separate word. That is, "$@" is equivalent to "$1" "$2" ...  If
> the double-quoted expansion occurs within a word, the expansion of the
> first  parameter  is  joined with  the beginning part of the original
> word, and the expansion of the last parameter is joined with the last
> part of the original word.  When there are no positional parameters,
> "$@" and $@ expand to nothing (i.e., they are removed).
>
>
> I don't quite see the difference with $* :-?

$* is a single string/argument, $@ is an array.



--
Per Jessen, Zürich (24.6°C)
http://www.dns24.ch/ - your free DNS host, made in Switzerland.


--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Carlos E. R.-2
In reply to this post by Per Jessen
On 2017-06-13 09:19, Per Jessen wrote:
> Carlos E. R. wrote:


>> to a single "find". And calling a function in the same script seemed
>> to me the clearest way of doing it.
>
> I usually tend towards 'xargs' because it can be parallelised, but in
> this case, I would probably do this:
>
> find /home/cer/Fusion/Videos/ -type d |\
> while read d;
> do
>     sudo chown cer:cer $d
>     chmod u+r+w+x,g+w+x,o+r-w-x,+t $d
>     sudo chown cer-g:cer $d
>     and-whatever-else-you-want.
> done
Ah, nice :-)

--
Cheers / Saludos,

                Carlos E. R.
                (from 42.2 x86_64 "Malachite" at Telcontar)


signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Carlos E. R.-2
In reply to this post by L A Walsh
On 2017-06-13 12:07, L A Walsh wrote:
> Carlos E. R. wrote:
>> find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer-g:cer '{}' \;
>> #setfacl -m u:wwwrun:rx  /data/waterhoard/Fusion.uno/Videos
>>  
> First two questions:
> is 'cer-g' a user? What are you wanting 'change-owner' to do
> to the file?

That comes from another thread :-)

The directories are changed to belong to another user, and the
"restricted deletion" flag is set. The files that belong to 'cer-g' can
not be then deleted by 'cer' by accident.

The alternative was to set the attribute "i" on files, which is a very
good solution; unfortunately, it does not show on "ls -l".

>
> Is #setfacl a comment?

Yes. It is a future command to add; first I have to add it to sudoers.

As the directory is not world readable, apache can not read it. So I'll
add an ACL for it. I already tested this on one directory.


> You seem to be trying to set things on the directories, but
> not setting access on anything in those directories  Is that right?

Correct.

The files are set from inside 'mc' midnight commander, using the context
menu under "F2": I mark the files I want to change, then call it.

That way I control which files I change or revert, and which I don't.

--
Cheers / Saludos,

                Carlos E. R.
                (from 42.2 x86_64 "Malachite" at Telcontar)


signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Carlos E. R.-2
In reply to this post by Per Jessen
On 2017-06-13 13:42, Per Jessen wrote:

> Carlos E. R. wrote:
>
>> $@?  I don't know what that one does. [searching...]
>>
>>        @      Expands  to the positional parameters, starting from
>>        one.
>> When the expansion occurs within double quotes, each parameter expands
>> to a separate word. That is, "$@" is equivalent to "$1" "$2" ...  If
>> the double-quoted expansion occurs within a word, the expansion of the
>> first  parameter  is  joined with  the beginning part of the original
>> word, and the expansion of the last parameter is joined with the last
>> part of the original word.  When there are no positional parameters,
>> "$@" and $@ expand to nothing (i.e., they are removed).
>>
>>
>> I don't quite see the difference with $* :-?
>
> $* is a single string/argument, $@ is an array.
Ah.

But in this case, both would result in generating the proper command
line to the chmod commands, nor?

Actually, a single argument may be the right thing, being a directory
name with spaces. I may get a name with two spaces!

Those names are come from TV serials names, so not under my control.
Another application generates those directories.


--
Cheers / Saludos,

                Carlos E. R.
                (from 42.2 x86_64 "Malachite" at Telcontar)


signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Vojtěch Zeisek-2
In reply to this post by Per Jessen
Dne úterý 13. června 2017 9:19:25 CEST, Per Jessen napsal(a):

> Carlos E. R. wrote:
> I usually tend towards 'xargs' because it can be parallelised, but in
> this case, I would probably do this:
>
> find /home/cer/Fusion/Videos/ -type d |\
> while read d;
> do
>     sudo chown cer:cer $d
>     chmod u+r+w+x,g+w+x,o+r-w-x,+t $d
>     sudo chown cer-g:cer $d
>     and-whatever-else-you-want.
> done
find /home/cer/Fusion/Videos/ -type d | parallel "sudo... && chmod... &&..."
This is my favorite style. :-)

--
Vojtěch Zeisek
https://trapa.cz/

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Bernhard Voelker
In reply to this post by Carlos E. R.-2
On 06/13/2017 01:55 PM, Carlos E. R. wrote:

> On 2017-06-13 13:42, Per Jessen wrote:
>> Carlos E. R. wrote:
>>
>>> $@?  I don't know what that one does. [searching...]
>>>
>>>        @      Expands  to the positional parameters, starting from
>>>        one.
>>> When the expansion occurs within double quotes, each parameter expands
>>> to a separate word. That is, "$@" is equivalent to "$1" "$2" ...  If
>>> the double-quoted expansion occurs within a word, the expansion of the
>>> first  parameter  is  joined with  the beginning part of the original
>>> word, and the expansion of the last parameter is joined with the last
>>> part of the original word.  When there are no positional parameters,
>>> "$@" and $@ expand to nothing (i.e., they are removed).
>>>
>>>
>>> I don't quite see the difference with $* :-?
>>
>> $* is a single string/argument, $@ is an array.
>
> Ah.
>
> But in this case, both would result in generating the proper command
> line to the chmod commands, nor?
>
> Actually, a single argument may be the right thing, being a directory
> name with spaces. I may get a name with two spaces!

Well, that's exactly the problem: if the script gets another argument
and that may also have spaces, then the array variant "$@" will work
better.

Thus, I'm almost never using "$*".

And BTW: it's always a good idea to use double quotes around "$@",
because otherwise the called program/snippet will have the same problem
again.

Have a nice day,
Berny

--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Bernhard Voelker
In reply to this post by Carlos E. R.-2
On 06/13/2017 01:41 PM, Carlos E. R. wrote:
> On 2017-06-13 09:03, Bernhard Voelker wrote:
>>   find /home/cer/Fusion/Videos/ -type d -exec \
>>     sh -c "
>>       sudo chown cer:cer '{}' &&
>>       chmod u+r+w+x,g+w+x,o+r-w-x,+t '{}' &&
>>       sudo chown cer-g:cer '{}'
>>       " \;
> This seems nice. I suppose it doesn't mater that the directories found
> have spaces?

No, as long as you put quotes around '{}'. ;-)

Have a nice day,
Berny


--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Bernhard Voelker
In reply to this post by Carlos E. R.-2
On 06/13/2017 01:42 PM, Carlos E. R. wrote:

> On 2017-06-13 09:19, Per Jessen wrote:
>> Carlos E. R. wrote:
>
>
>>> to a single "find". And calling a function in the same script seemed
>>> to me the clearest way of doing it.
>>
>> I usually tend towards 'xargs' because it can be parallelised, but in
>> this case, I would probably do this:
>>
>> find /home/cer/Fusion/Videos/ -type d |\
>> while read d;
>> do
>>     sudo chown cer:cer $d
>>     chmod u+r+w+x,g+w+x,o+r-w-x,+t $d
>>     sudo chown cer-g:cer $d
>>     and-whatever-else-you-want.
>> done
>
> Ah, nice :-)

yes, as long as you use quotes around "$d", and as long as there
are no even more weird characters in the directory names like newlines.

Have a nice day,
Berny

--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Carlos E. R.-2
In reply to this post by Bernhard Voelker
On 2017-06-13 13:59, Bernhard Voelker wrote:
> On 06/13/2017 01:55 PM, Carlos E. R. wrote:
>> On 2017-06-13 13:42, Per Jessen wrote:
>>> Carlos E. R. wrote:


>>>> I don't quite see the difference with $* :-?
>>>
>>> $* is a single string/argument, $@ is an array.
>>
>> Ah.
>>
>> But in this case, both would result in generating the proper command
>> line to the chmod commands, nor?
>>
>> Actually, a single argument may be the right thing, being a directory
>> name with spaces. I may get a name with two spaces!
>
> Well, that's exactly the problem: if the script gets another argument
> and that may also have spaces, then the array variant "$@" will work
> better.
>
> Thus, I'm almost never using "$*".
>
> And BTW: it's always a good idea to use double quotes around "$@",
> because otherwise the called program/snippet will have the same problem
> again.
Ok, noted in my notes :-)


--
Cheers / Saludos,

                Carlos E. R.
                (from 42.2 x86_64 "Malachite" at Telcontar)


signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

Carlos E. R.-2
In reply to this post by Bernhard Voelker
On 2017-06-13 14:02, Bernhard Voelker wrote:

> On 06/13/2017 01:42 PM, Carlos E. R. wrote:
>> On 2017-06-13 09:19, Per Jessen wrote:
>>> Carlos E. R. wrote:
>>
>>
>>>> to a single "find". And calling a function in the same script seemed
>>>> to me the clearest way of doing it.
>>>
>>> I usually tend towards 'xargs' because it can be parallelised, but in
>>> this case, I would probably do this:
>>>
>>> find /home/cer/Fusion/Videos/ -type d |\
>>> while read d;
>>> do
>>>     sudo chown cer:cer $d
>>>     chmod u+r+w+x,g+w+x,o+r-w-x,+t $d
>>>     sudo chown cer-g:cer $d
>>>     and-whatever-else-you-want.
>>> done
>>
>> Ah, nice :-)
>
> yes, as long as you use quotes around "$d", and as long as there
> are no even more weird characters in the directory names like newlines.
I wouldn't put it past the TV industry ;-)

--
Cheers / Saludos,

                Carlos E. R.
                (from 42.2 x86_64 "Malachite" at Telcontar)


signature.asc (188 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Quick question: how to call a script function from "find"?

L A Walsh
In reply to this post by Carlos E. R.-2
Carlos E. R. wrote:

> On 2017-06-13 08:51, Bernhard Voelker wrote:
>  
>> On 06/13/2017 05:38 AM, Carlos E. R. wrote:
>>    
>>> The intention was to convert:
>>>      
>>> find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer:cer '{}' \; find /home/cer/Fusion/Videos/ -type d -exec
>>> chmod u+r+w+x,g+w+x,o+r-w-x,+t '{}' \; find /home/cer/Fusion/Videos/ -type d -exec sudo chown cer-g:cer '{}' \;
>>> #setfacl -m u:wwwrun:rx  /data/waterhoard/Fusion.uno/Videos
>>>      
>> I'm not sure why you would want to chown to 'cer' and later to 'cer-g'.
>> This looks redundant to me.
>>    
>
> Because on a second run of the script, the chmod would error on
> directories already owned by "cer-g".
not if you are running the script as root (i.e. just run chown cer-g:cer )

Are you sure you want to give 'other' read+no execute?
Maybe you want the other way around?



--
To unsubscribe, e-mail: [hidden email]
To contact the owner, e-mail: [hidden email]

123
Loading...