Search code examples
bashshelldarwin

Subshells, permissions and following symlinks on OS X/Darwin: why do I get permission errors?


(Edit: There was a typo that caused the problem. Please see comments on existing answers)

I wanted to stat a file that symlinked to another file and thought that I could use subshells and readlink command substitution to do the job. For context, let me mention that this is on OS X 10.8.3 (Darwin).

But I ran into a permission denied error.

ujagtahb@ujags-Retina-MBP-5.local:~/Code_Pen/c_exp$(cd /usr/share/locale/; stat $(en_US/LC_COLLATE))
bash: en_US/LC_COLLATE: Permission denied
727393128 1449 crw--w---- 1 ujagtahb tty 268435496 0 "Aug 12 11:36:50 2013" "Aug 12 11:36:51 2013" "Aug 12 11:36:51 2013" "Aug 12 11:36:51 2013" 131072 0 0 (stdin)

I checked the output of the readlink command and sure enough, I didn't see anything wrong with it.

ujagtahb@ujags-Retina-MBP-5.local:~/Code_Pen/c_exp$readlink /usr/share/locale/en_US/LC_COLLATE
../la_LN.US-ASCII/LC_COLLATE

stating the file directly raised no error and produced the output I needed.

ujagtahb@ujags-Retina-MBP-5.local:~/Code_Pen/c_exp$stat /usr/share/locale/la_LN.US-ASCII/LC_COLLATE
16777218 284538 -r--r--r-- 1 root wheel 0 2086 "Aug 12 11:36:51 2013" "Jul 22 07:55:02 2012" "Jul 22 07:55:02 2012" "Jun 21 01:35:25 2012" 4096 0 0x20 /usr/share/locale/la_LN.US-ASCII/LC_COLLATE

What's causing the permission denied in one case but not the other?


Solution

  • This:

    (cd /usr/share/locale/; stat $(en_US/LC_COLLATE))
    

    means "open a subshell, cd to /usr/share/locale, run the command en_US/LC_COLLATE, and run stat on the output".

    But I think the command you want to run is readlink en_US/LC_COLLATE, not en_US/LC_COLLATE; so:

    (cd /usr/share/locale/; stat $(readlink en_US/LC_COLLATE))
    

    (I'm guessing this was just a typo?)