tags:

views:

138

answers:

1

Hi,

I have a script which is running as root, it's supposed to store it's cache file (only modified by the script) at /var/lib/varlibfile. However the script does not seem to create the file, and my investigation led me to this:

$ sudo echo "something" >/var/lib/varlibfile
bash: /var/lib/varlibfile: Permission denied
$ sudo echo "something" >varlibfile
$ sudo cp varlibfile /var/lib/

In other words, I am able to create a file called varlibfile at /var/lib, but I'm not allowed to create it using redirection. Only file copy works. It seems very strange to me and I would like to use redirection directly to the intended file instead of creating it somewhere else and then copying it.

Some extra information:

$ ls -ld /var
drwxr-xr-x 14 root root 4096 2009-03-02 14:47 /var
$ ls -ld /var/lib
drwxr-xr-x 51 root root 4096 2009-05-16 19:25 /var/lib
$ ls -la /var/lib/ | head
total 204
drwxr-xr-x  51 root          root          4096 2009-05-16 19:25 .
drwxr-xr-x  14 root          root          4096 2009-03-02 14:47 ..
drwxr-xr-x   2 root          root          4096 2009-03-02 14:22 acpi-support
drwxr-xr-x   2 root          root          4096 2009-03-02 14:39 alsa
drwxr-xr-x   2 root          root          4096 2009-05-14 14:42 apparmor
drwxr-xr-x   6 root          root          4096 2009-05-14 14:42 apt
drwxr-xr-x   2 root          root          4096 2008-04-04 11:56 aptitude
drwxr-xr-x   2 root          root          4096 2008-07-02 12:58 aspell
drwxr-xr-x   2 avahi-autoipd avahi-autoipd 4096 2009-03-05 16:42 avahi-autoipd

Can anyone explain why this is happening? And how to solve the problem, ideally I want to write /var/lib/varfile directly without any previous creation at another location.

+10  A: 

What

$ sudo echo something >/var/lib/varlibfile

does, is pipe the output of "sudo echo something" to the file. So the file write operation is done as the current user, who doesn't have permission to write in /var/lib.

Try something like

$ sudo sh -c "echo something >/var/lib/varlibfile"

or

$ echo something | sudo tee /var/lib/varlibfile
dF
+1 for that. Some people really seem to forget that it's the shell doing the redirections, not the program they start :)
TheBonsai
Yes, like me! :-) At least me.knowledge++ so hopefully I'll remember it from now on.
DeletedAccount