Results 1 to 5 of 5

Thread: Question about sudo and bash

  1. #1
    Join Date
    Jul 2005
    Beans
    34

    Question about sudo and bash

    Hi, in my ubuntu lucid box, I have a bash script to automatize the creation of a svn repository (this is not a svn question).

    In the script, I have these lines of code (among others):

    Code:
    # Create the repository
    sudo -u www-data svnadmin create --fs-type fsfs $PATH$1 2>&1
    
    # Create a pre-commit hook manually
    sudo -u www-data echo '#!/bin/sh
    REPOS="$1"
    TXN="$2"
    ...some more content' > $PATH$1/hooks/pre-commit
    Ok, the second sentence (sudo -u www-data echo ...) cannot complete because it shows a "permission denied" even though the $PATH$1/hooks is owned by www-data:www-data.

    Interesting thing is that if I remove the "sudo -u www-data" from the script, and I execute it "outside" as: sudo -u www-data ./createrepo nameofrepo, no more "permission denied" errors are showed.

    How can I achieve this "sudo -u www-data" inside the script?

    Thanks in advance.

    Cheers,
    Roland

  2. #2
    Join Date
    Sep 2007
    Location
    England
    Beans
    1,103

    Re: Question about sudo and bash

    The redirect is done by the shell, and not by the command

    Try something like this (note the escaped quotes)

    Code:
    sudo -u www-data sh -c 'echo \'#!/bin/sh
    REPOS="$1"
    TXN="$2"
    ...some more content\' > $PATH$1/hooks/pre-commit'

  3. #3
    Join Date
    Jul 2009
    Location
    London
    Beans
    1,480
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: Question about sudo and bash

    or an alternative is to use tee.

    Code:
    echo 'blah
    blah
    blah' | sudo -u www-data tee /path/to/file
    as amauk indicates, you don't need to be sudo'd to echo text (first part of your command), however you do need to be sudo'd to redirect that text onto a file owned by another user.

  4. #4
    Join Date
    Sep 2007
    Location
    England
    Beans
    1,103

    Re: Question about sudo and bash

    yeah, use tee instead
    it's cleaner

  5. #5
    Join Date
    Jul 2005
    Beans
    34

    Re: Question about sudo and bash

    Hi there.
    Sorry for the delay; was on some vacation days.

    Thanks a lot for your replies. Will do that with the tee command (I didn't know about that command).

    Thanks a lot for your help!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •