LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-23-2009, 01:42 PM   #1
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 123

Rep: Reputation: 16
insert a line or a file to /etc


I am trying to create a script to add a line to the /etc/exports file. I am root user and can do it from cli but when I put an echo 'test' >> /etc/testfile or echo 'add line' >> /etc/exports in a script I get a 'permission denied'. What do I need to do to make this work within a script?

Thank you.
 
Old 07-23-2009, 01:59 PM   #2
bsdunix
Senior Member
 
Registered: May 2006
Distribution: Caldera, CTOS, Debian, FreeBSD, Mac OS X, Mandrake, Minix, OpenBSD, Slackware, SuSE
Posts: 1,757

Rep: Reputation: 80
Is the executable bit set on the script?

http://unixhelp.ed.ac.uk/scrpt/scrpt1.2.html
 
Old 07-23-2009, 02:02 PM   #3
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 123

Original Poster
Rep: Reputation: 16
Yes. the script does execute. I tested it by just doing an echo 'hello' and that executed fine. Thanks.
 
Old 07-23-2009, 02:03 PM   #4
repo
LQ 5k Club
 
Registered: May 2001
Location: Belgium
Distribution: Arch
Posts: 8,527

Rep: Reputation: 898Reputation: 898Reputation: 898Reputation: 898Reputation: 898Reputation: 898Reputation: 898
Quote:
I get a 'permission denied
Where do you get the permission denied?
Can you post the error messages ?
 
Old 07-23-2009, 02:10 PM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Code:
Peter@dv9710us:~ $ sudo touch /etc/testfile
[sudo] password for Peter:                 
Peter@dv9710us:~ $ sudo echo Test > /etc/testfile
bash: /etc/testfile: Permission denied           
Peter@dv9710us:~ $ ls -l /etc/testfile           
-rw-r--r--. 1 root root 0 2009-07-23 10:50 /etc/testfile
<edit>
Note that, whilst the SELinux context is set (the . in -rw-r--r--.), I have SELinux set to "permissive" mode, so that shouldn't be a problem.
</edit>

<edit2>
On the other hand . . .
Code:
Peter@dv9710us:~ $ su -
Password:              
[root@dv9710us ~]# echo Test > /etc/testfile 
[root@dv9710us ~]# ls -l /etc/testfile*
-rw-r--r--. 1 root root 5 2009-07-23 11:18 /etc/testfile
[root@dv9710us ~]# cat /etc/testfile                    
Test
</edit2>

Last edited by PTrenholme; 07-23-2009 at 02:23 PM.
 
Old 07-23-2009, 02:17 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
j-me and PTrenholme the same person? :}

Anyway: the problem is that you echo as root, but the redirect happens
to the sudo-part.

What you need to do is:
Code:
sudo sh -c "echo hello > /etc/testfile"
 
Old 07-23-2009, 02:19 PM   #7
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 123

Original Poster
Rep: Reputation: 16
Here is what I get:

cp: cannot create regular file `/etc/exports-j-me': Permission denied

script:
cp /etc/exports .
echo 'testing' >> exports
cp -f exports /etc/exports-j-me

I execute from a working directory.
make a copy of the exports file
insert a line at bottom of the file.
copy the changed file back over.
the first two steps work. the last step is where I receive an error.
 
Old 07-23-2009, 02:28 PM   #8
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 123

Original Poster
Rep: Reputation: 16
i'll be damned, it works! That you oh wise one!

Thanks to all for their quick response.
 
Old 07-23-2009, 02:34 PM   #9
j-me
Member
 
Registered: Jan 2003
Location: des moines, ia
Distribution: suse RH
Posts: 123

Original Poster
Rep: Reputation: 16
can you explain what the "sudo sh -c does? execute within a shell? I remember reading something about this.
Thanks.
 
Old 07-23-2009, 02:47 PM   #10
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
I'm far from wise, I'm afraid ... but I can read, and
occasionally understand what I take in ;}

After a decade or so of Linux use it seemed "obvious" that the
re-direct was not part of what's run under sudo (otherwise a
permission problem wouldn't have arisen). How to wrap that all
up would have taken me a few minutes of thought, instead I looked
at the sudo man-page and searched for re-direct.

From the sudo man-page
Code:
       To make a usage listing of the directories in the /home partition.
       Note that this runs the commands in a sub-shell to make the cd and file
       redirection work.

        $ sudo sh -c "cd /home ; du -s * | sort -rn > USAGE"


Cheers,
Tink
 
  


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
using sed to insert line into file and overwrite the current file jadeddog Programming 3 06-11-2009 08:14 PM
vi trick for insert text in between line ufmale Programming 3 05-13-2008 04:01 AM
insert a line into a file: facing problem RudraB Programming 7 03-25-2008 08:43 PM
shell script: insert line in a file noir911 Programming 6 02-04-2008 11:42 PM
Script to insert string in first line of a file minil Programming 13 01-03-2006 12:56 AM


All times are GMT -5. The time now is 12:26 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration