FedoraForum.org - Fedora Support Forums and Community
Results 1 to 9 of 9
  1. #1
    Join Date
    May 2011
    Location
    Confoederatio Helvetica (Swissh)
    Age
    38
    Posts
    4,277

    [bash] sudo sub?

    Heyas

    Is it possible to run a function of a script with sudo?
    sudo -i - function... doesnt seem to work properly.

    The way of the issue:
    PHP Code:
    # | Virtual Machine:    ::    Virtual Box Tutorial: http://www.if-not-true-then-false.com/2010/install-virtualbox-with-yum-on-fedora-centos-red-hat-rhel/
        
    vboxanyway() {
                    
    cd "/etc/yum.repos.d/"
                    
    seawget "Virtual Box Repo\t"         "http://download.virtualbox.org/virtualbox/rpm/fedora/virtualbox.repo"
                    
    yum update
                    seayum 
    "Dependency packag VBox\t"        "gcc kernel-devel kernel-headers"
                    
    seayum "Virtual Box 4.1\t\t"            "virtualbox-4.1"
                    
    sudo usermod --G vboxusers $USER
                    
    }
        if [ 
    isROOT ] ;then # usualy not installed/ran as root
            
    vboxanyway
        
    else
            
    sudo -i vboxanyway
        fi 
    Or do i need to execute that script which provides the function with sudo first?
    Would that suffice?

    tia sea
    EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
    Video Handler Script (VHS) (mass re-encode videos, screenrecorder, console music/webradio player, ...) :: http://forums.fedoraforum.org/showthread.php?t=299182
    Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161

  2. #2
    Join Date
    Jul 2009
    Location
    England, UK
    Posts
    969

    Re: [bash] sudo sub?

    I think the problem is that when you use "sudo -i" it starts a new login shell (as root), which does not have the function vboxanyway defined.

    Personally, I would make the script halt if run as a non-root user (with a useful error message), and remember to run the whole script with sudo.

    Otherwise perhaps you could re-write slightly so that your script does the test for being run as root very early on, and re-loads itself using sudo; I mean having a line something like
    Code:
    [ "$EUID" -ne 0 ] ||  exec sudo "$0" "$@"

  3. #3
    Join Date
    May 2011
    Location
    Confoederatio Helvetica (Swissh)
    Age
    38
    Posts
    4,277

    Re: [bash] sudo sub?

    Problem is, i dont want to run the complete script as root.
    The above code is just a segment, and the rest of the script is running fine as 'normal' user.

    Basicly ROOT is just required to save the repo to that path.
    I run vboxanyway just out of lazyness, and it looks cleaner.

    If i understood you correctly, if i'd leave out the -i, i could pass the subroutine to sudo, as that shell session knows that subroutine already?
    EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
    Video Handler Script (VHS) (mass re-encode videos, screenrecorder, console music/webradio player, ...) :: http://forums.fedoraforum.org/showthread.php?t=299182
    Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161

  4. #4
    Join Date
    Jul 2009
    Location
    England, UK
    Posts
    969

    Re: [bash] sudo sub?

    Quote Originally Posted by sea
    If i understood you correctly, if i'd leave out the -i, i could pass the subroutine to sudo, as that shell session knows that subroutine already?
    I'm afraid that won't work, no: then sudo will try to run vboxanyway as a command (not as a shell function). For example, watch what happens when I define a new function and try to use sudo interactively:
    Code:
    $ wibble() { echo This is wibble ; }
    $ wibble
    This is wibble
    $ sudo wibble
    [sudo] password for root: 
    sudo: wibble: command not found
    $
    Why not instead make the function vboxanyway use sudo for the commands where it is needed? (I notice you do this in some places.)

  5. #5
    Join Date
    May 2011
    Location
    Confoederatio Helvetica (Swissh)
    Age
    38
    Posts
    4,277

    Re: [bash] sudo sub?

    Quote Originally Posted by marriedto51
    (I notice you do this in some places.)
    Because that line was copy-pasted

    Guess i have to make root getting these subs as well then.

    Thank you.
    EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
    Video Handler Script (VHS) (mass re-encode videos, screenrecorder, console music/webradio player, ...) :: http://forums.fedoraforum.org/showthread.php?t=299182
    Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161

  6. #6
    Join Date
    Jul 2011
    Location
    Birmingham, UK
    Age
    35
    Posts
    2,761

    Re: [bash] sudo sub?

    You can do "export -f funcname" to add the function's code to the environment so that a child shell can run it, but I can't find a way to make sudo keep the function. "su -c" works though:

    Code:
    [gareth@gareth-desktop ~]$ say_hi()
    > {
    > echo Hi there!
    > }
    [gareth@gareth-desktop ~]$ say_hi
    Hi there!
    [gareth@gareth-desktop ~]$ su -c "say_hi"
    Password: 
    bash: say_hi: command not found
    [gareth@gareth-desktop ~]$ export -f say_hi
    [gareth@gareth-desktop ~]$ su -c "say_hi"
    Password: 
    Hi there!
    [gareth@gareth-desktop ~]$
    You are probably better off just using sudo for the specific commands that require it however, as you already appear to be doing for usermod in your function.

    Gareth

  7. #7
    Join Date
    May 2011
    Location
    Confoederatio Helvetica (Swissh)
    Age
    38
    Posts
    4,277

    Re: [bash] sudo sub?

    Hmm i might like this workaround, thank you.
    Going to test it with sudo.
    EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
    Video Handler Script (VHS) (mass re-encode videos, screenrecorder, console music/webradio player, ...) :: http://forums.fedoraforum.org/showthread.php?t=299182
    Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161

  8. #8
    Join Date
    Jul 2011
    Location
    Birmingham, UK
    Age
    35
    Posts
    2,761

    Re: [bash] sudo sub?

    Good luck, I couldn't make sudo keep the function in the environment, even when it kept normal env vars (-E option).

    Gareth

  9. #9
    Join Date
    May 2011
    Location
    Confoederatio Helvetica (Swissh)
    Age
    38
    Posts
    4,277

    Re: [bash] sudo sub?

    Yea just figured, oh well su does just as well
    Thank you very much.
    EFI Cheatsheet :: http://forums.fedoraforum.org/showthread.php?t=298546
    Video Handler Script (VHS) (mass re-encode videos, screenrecorder, console music/webradio player, ...) :: http://forums.fedoraforum.org/showthread.php?t=299182
    Windows 8+ & Fedora 20+ Dualboot :: http://forums.fedoraforum.org/showthread.php?t=298161

Similar Threads

  1. sudo
    By mlocicero in forum Using Fedora
    Replies: 4
    Last Post: 17th January 2011, 03:21 PM
  2. Cannot open /var/db/sudo after sudo package upgrade
    By Replicant10000 in forum Security and Privacy
    Replies: 1
    Last Post: 16th September 2010, 01:25 PM
  3. [SOLVED]
    How to Run (not Run in Terminal) bash script with sudo in it?
    By premudriy in forum Using Fedora
    Replies: 2
    Last Post: 6th July 2010, 06:56 PM
  4. Using Sudo
    By adrianelliott in forum Using Fedora
    Replies: 4
    Last Post: 17th July 2007, 02:22 AM
  5. Get bash completion working on fedora 3 (bash 3.0)
    By madcat in forum Guides & Solutions (Not For Questions)
    Replies: 2
    Last Post: 27th November 2004, 12:05 PM

Posting Permissions

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