LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Using sudo in script (https://www.linuxquestions.org/questions/programming-9/using-sudo-in-script-640856/)

snowman81 05-08-2008 12:18 PM

Using sudo in script
 
I have a simple script that echos a line into my fstab file. It's probably not safe but I just using it for testing. Anyway, the second line works but for some reason the third always says permission denied even though I know it is running as root. Any ideas?

Code:

#!/bin/bash

echo "*******" | sudo -S mkdir /mnt/publicshare

sudo echo "//macx/swu /mnt/publicshare smbfs user,rw,noauto,username=swu" >> /etc/fstab


ntubski 05-08-2008 01:09 PM

In your script only the echo is run with root privileges, but the current shell is the one that writes to the file. What you need to do is run a subshell with sudo:
Code:

sudo bash -c 'echo "//macx/swu /mnt/publicshare smbfs user,rw,noauto,username=swu" >> /etc/fstab'

snowman81 05-08-2008 01:28 PM

Yup, that was my problem, thanks.


All times are GMT -5. The time now is 08:28 PM.