Tool that changes permissions, and then changes them back
I am looking for a tool that changes permissions, and then changes them back to a st开发者_开发百科ate before.
Why?
Well..., with GNU stat
#!/bin/sh
orig_mode=$(stat -c %a "$2")
chmod "$1" "$2"
chmod "$orig_mode" "$2"
Here we go again, with *BSD stat this time:
#!/bin/sh
orig_mode=$(stat -f "%OMp%OLp" "$2")
chmod "$1" "$2"
chmod "$orig_mode" "$2"
With other versions of stat it would depend on whether/how output formatting is supported.
Display a file's permissions with stat's "-c,--format" option combined with the "%a Access rights in octal" sequence. File permissions can be changed with chmod.
For example, save the original permissions of a file with stat.
$ OLD_PERM=$(stat -c"%a" $FILENAME)
$ echo $OLD_PERM
644
Change the permissions
$ chmod 600 $FILENAME
$ stat -c"%a" $FILENAME
600
And finally restore permissions back to the original value.
$ chmod $OLD_PERM $FILENAME
$ stat -c"%a" $FILENAME
644
精彩评论