Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Given that it is <code>bash</code>, you can do:</p> <pre><code> printf "%.4o\n" $(( $(stat -f '0%Lp' "$file") &amp; ~$(umask) )) </code></pre> <p>at least, on Mac OS X, where <code>stat -f '%Lp'</code> means 'print the user, group, other permissions in octal'. If you're on Linux, you'll need to translate that to the GNU <code>stat</code> syntax, which looks like it probably is:</p> <pre><code>printf "%.4o\n" $(( $(stat --printf '%a' "$file") &amp; ~$(umask) )) </code></pre> <p>The spaces after <code>$((</code> and before <code>))</code> are not strictly needed in the sense that the shell knows what to do even if they are missing; likewise the spaces around the <code>&amp;</code>. OTOH, if anyone else has to read the code, you probably want those spaces there.</p> <hr> <p>Test code:</p> <pre><code>file="./mode-file" trap "rm -f $file; exit 1" 0 1 2 3 13 15 cp /dev/null "$file" for mode in 777 755 644 640 444 440 400 do chmod $mode $file ls -l $file for umask in 002 022 027 033 037 077 177 do umask $umask printf "$mode &amp; ~$umask = %.4o\n" $(( $(stat -f '0%Lp' "$file") &amp; ~$(umask) )) done done rm -f $file trap 0 </code></pre> <p>Example output:</p> <pre><code>-rwxrwxrwx 1 jleffler staff 0 Nov 11 20:57 ./mode-file 777 &amp; ~002 = 0775 777 &amp; ~022 = 0755 777 &amp; ~027 = 0750 777 &amp; ~033 = 0744 777 &amp; ~037 = 0740 777 &amp; ~077 = 0700 777 &amp; ~177 = 0600 -rwxr-xr-x 1 jleffler staff 0 Nov 11 20:57 ./mode-file 755 &amp; ~002 = 0755 755 &amp; ~022 = 0755 755 &amp; ~027 = 0750 755 &amp; ~033 = 0744 755 &amp; ~037 = 0740 755 &amp; ~077 = 0700 755 &amp; ~177 = 0600 -rw-r--r-- 1 jleffler staff 0 Nov 11 20:57 ./mode-file 644 &amp; ~002 = 0644 644 &amp; ~022 = 0644 644 &amp; ~027 = 0640 644 &amp; ~033 = 0644 644 &amp; ~037 = 0640 644 &amp; ~077 = 0600 644 &amp; ~177 = 0600 -rw-r----- 1 jleffler staff 0 Nov 11 20:57 ./mode-file 640 &amp; ~002 = 0640 640 &amp; ~022 = 0640 640 &amp; ~027 = 0640 640 &amp; ~033 = 0640 640 &amp; ~037 = 0640 640 &amp; ~077 = 0600 640 &amp; ~177 = 0600 -r--r--r-- 1 jleffler staff 0 Nov 11 20:57 ./mode-file 444 &amp; ~002 = 0444 444 &amp; ~022 = 0444 444 &amp; ~027 = 0440 444 &amp; ~033 = 0444 444 &amp; ~037 = 0440 444 &amp; ~077 = 0400 444 &amp; ~177 = 0400 -r--r----- 1 jleffler staff 0 Nov 11 20:57 ./mode-file 440 &amp; ~002 = 0440 440 &amp; ~022 = 0440 440 &amp; ~027 = 0440 440 &amp; ~033 = 0440 440 &amp; ~037 = 0440 440 &amp; ~077 = 0400 440 &amp; ~177 = 0400 -r-------- 1 jleffler staff 0 Nov 11 20:57 ./mode-file 400 &amp; ~002 = 0400 400 &amp; ~022 = 0400 400 &amp; ~027 = 0400 400 &amp; ~033 = 0400 400 &amp; ~037 = 0400 400 &amp; ~077 = 0400 400 &amp; ~177 = 0400 </code></pre> <p>NB: Tested on Mac OS X 10.9 (Mavericks). If you are using a GNU/Linux platform, you must change the <code>stat</code> command to use the notation that works on your machine.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload