LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-08-2008, 12:18 PM   #1
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Rep: Reputation: 30
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

Last edited by snowman81; 05-08-2008 at 12:19 PM.
 
Old 05-08-2008, 01:09 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,774

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
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'
 
Old 05-08-2008, 01:28 PM   #3
snowman81
Member
 
Registered: Aug 2006
Location: Michigan
Distribution: Ubuntu
Posts: 282

Original Poster
Rep: Reputation: 30
Yup, that was my problem, thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The Ultimate Sudo FAQ — To Sudo Or Not To Sudo? LXer Syndicated Linux News 13 04-13-2013 01:36 AM
php script and sudo jaymoney Programming 7 07-02-2007 08:15 PM
sudo does not work with gpg-script Lotharster Linux - General 12 07-16-2006 03:03 PM
php script & sudo rblampain Programming 3 05-13-2006 08:03 PM
Using sudo to control script exececution wimnat Linux - Software 10 11-15-2005 03:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration