This is part of Ansible automation where I have to run a single chmod
command and read the permission from the users. Thus,
chmod -R $fileperm <filename>
However, at times the user may not provide any fileperm
thus $fileperm
is empty in which case the file permissions should remain unchanged however the above command fails.
I do not wish to have two chmod
commands having different when
conditions.
Can you please suggest how can I accommodate whether the user provides or does not provide permissions in the same command if possible? I am currently using Ansible's command
module to execute chmod
.
when
. Which automation language are you using? If it's the shell, you can use a compact command like[ -z "$fileperm" ] || chmod $fileperm <filename>
. The part after||
is only executed if the preceding test is false, i.e.$fileperm
is non-empty.[ -z "$fileperm" ] ||
considering my current code iscommand: chmod -R $fileperm <filename>
u+
(add nothing extra for owner) seems to be a valid symbolic permission string for (GNUs implementation of)chmod
, so I guess you could use that as a default?