1

Im just wondering if there is any way to say 'if chmod does not change permissions of the file, then ...'

if ! chmod ${1} ${2}  #if chmod did not excecute correctly
then
echo "${0}:ERROR: ${2} has not been changed." 1>&2
exit 6 #exiting due to failure. status 6
fi

I have tried this and several other ways but I can only add to the current setting rather than change it...

i.e if i start with -rwxr-xr-x i change it to -rwxrwxrwx

nothing else seems to work

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 2
    @EricLeschinski, please don't suggest the ABS as a reference -- it's the w3schools of bash, full of outdated suggestions and bad-practice examples. The bash-hackers wiki is a far more reputable source; its page on exit status is [here](http://wiki.bash-hackers.org/dict/terms/exit_status). Similarly, the [BashGuide's Tests and Conditionals section](http://mywiki.wooledge.org/BashGuide/TestsAndConditionals) covers exit status in detail. – Charles Duffy Dec 02 '16 at 01:30
  • What's going into `${1}`? The problem here may be what you're feeding `chmod`. Perhaps it's a valid command, just not doing what you expect. :) – ghoti Dec 02 '16 at 01:35
  • What do you mean by "did not execute correctly", or "failed"? If the permissions were already what you want them to be, does that mean it failed? If the operating-system syscall succeeds (meaning that the filesystem has reported back that it successfully executed the change requested), but the underlying filesystem can't represent the permissions you want (and so implements the change in a manner that makes read-back different from what you specified), does *that* count as a failure? Be very, **very** specific about what the scenario is -- in general, `chmod` will already report a failure. – Charles Duffy Dec 02 '16 at 01:35
  • ...that is, `chmod` already will return with an exit status of `1` and thus perform the content inside your `if` if the operating system reports back that it was unable to perform the desired change. But if you're on a filesystem that doesn't support the UNIX permissions model, then the operating system may do the closest thing it *can* to the desired change, and report success if *that* operation encountered no failures. – Charles Duffy Dec 02 '16 at 01:36
  • 1
    On a different point, `${1}` is exactly the same as `$1`, but `"$1"` is different (and more correct) than either. If you're going to write extra characters in the interests of making your code less broken, consider using extra characters *that actually make your code less broken*. (See [the http://shellcheck.net/ page for the relevant error](https://github.com/koalaman/shellcheck/wiki/SC2086)). – Charles Duffy Dec 02 '16 at 01:39
  • 1
    ravingbadgers, I wrote [another answer that explains chmod usage](http://stackoverflow.com/a/29070153/1072112) that might contain helpful tips for you. Still, please either [complete this question with an MCVE](http://stackoverflow.com/help/mcve) or delete it. It's unanswerable in its current form. – ghoti Dec 02 '16 at 01:46

1 Answers1

1

Your command is correct. But it only detects when the chmod can not operate (e.g. when you don't own the file, when the FS is read-only, etc.).

If you also want to detect that the chmod didn't change anything, try this:

if ! res=$(chmod -c -- "${1}" "${2}") || [ -z "$res" ]
then
    echo "${0}:ERROR: ${2} has not been changed." 1>&2
    exit 6 #exiting due to failure. status 6
fi

The -c flag is specific to GNU chmod. Here is the description:

-c, --changes
    like verbose but report only when a change is made

-v, --verbose
    output a diagnostic for every file processed
xhienne
  • 5,738
  • 1
  • 15
  • 34
  • `-c` isn't specified in [POSIX `chmod`](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/chmod.html), nor is it available in the BSD version. Is it a GNUism? – Charles Duffy Dec 02 '16 at 01:33
  • @Charles Duffy Probably. Here is the description for -c / --changes : like verbose but report only when a change is made. – xhienne Dec 02 '16 at 01:40
  • 1
    Not sure what operating system you're using, but I don't see a `-c` option on chmod in OS X or FreeBSD. If you're going to offer vendor- or platform-specific answers to a non-platform-specific question, please include the limitations you're working with in your answer. – ghoti Dec 02 '16 at 01:49
  • @Charles It outputs to stdout. – xhienne Dec 02 '16 at 02:10
  • @ghoti Done. Hope this is ok. – xhienne Dec 02 '16 at 02:11