Execute a command with root user

i m logged in with user1 id.

i wish to execute the below as root user for which i tried several commands but all of them fail.

sudo 'cat /tmp/tmp.file >>/etc/logger'
Password:
sudo: cat /tmp/tmp.file >>/etc/logger: command not found

sudo cat /tmp/tmp.file >>/etc/logger
bash: /etc/logger: Permission denied

su - root -c 'cat /tmp/tmp.file >>/etc/logger'
Password:
su: Sorry

sudo -u root 'cat /tmp/tmp.file >>/etc/logger'
sudo: cat /tmp/tmp.file >>/etc/logger: command not found

uname -a
SunOS mymac 5.11 11.2 sun4v sparc sun4v

Can you please point out what am i doing wrong ?

1 Like

sudo is not a shell. Feed it the string cat /tmp/tmp.file >> filename and it will look for a file named cat /tmp/tmp.file >> filename and complain that no such file exists.

Don't run script with sudo, use sudo to run scripts.

Do you mind explaining to me what is /etc/logger ?
Im missing something...

I m sorry but that was difficult for me to understand.

sudo is not a shell and therefore does not understand redirection ( >>/etc/logger ) and when you pass the command cat /tmp/tmp.file >>/etc/logger to it in quotes, it sees >>/etc/logger as part of the command name. Instead what you could do is use sudo to run a shell who does this for you:

sudo bash -c 'cat /tmp/tmp.file >>/etc/logger'

Similar to what Corona688 suggested, if cat /tmp/tmp.file >>/etc/logger is part of a shell script, then you could use sudo /path/to/script to make it happen, since running a script will invoke a new shell that will run as root and that will interpret that shell script.

Additionally, if you run this:-

sudo cat /tmp/tmp.file >>/etc/logger

... what you are trying to do is to run cat with privileges so it can open whatever file out want, HOWEVER the >> is in your current, non-privileged shell so if you cannot open the file, then you will get the bash: /etc/logger: Permission denied seen in post 1

You haven't answered vbe's question about what /etc/logger is. Is this just a plain log file, or are you trying to write to the system logs? If it is the latter, overwriting/appending to it would be a bad thing. Usually /etc contains configuration information that you would not want to routinely overwrite/append. Depending on your actual desires, this is more likely to be a file in /var somewhere.

What are you actually trying to achieve?

Robin