Sudo function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sudo function
# 1  
Old 01-13-2013
Sudo function

I have my .bash_profile with this function for instance:
Code:
foo(){
   if [ `id -u` -ne 0 ]
      then   echo "RUN COMMAND AS ROOT"
      else   echo "F-O-O"
   fi
}

If I launch terminal (MAC OSX) and run foo it'll output RUN COMMAND AS ROOT as expected.

BUT.

If I run sudo foo output is sudo: foo: command not found.

I can always:
Code:
$ sudo -s
[password]
$ source ~/.bash_profile
$ foo

That will work, but I'd rather have a fast way of doing it.
Any tips?
# 2  
Old 01-13-2013
sudo is not psychic, hence it does not know what aliases and functions you have in your shell.

sudo can only run things that are files. It cannot run aliases and functions.

Why can't this "foo" run sudo itself?
# 3  
Old 01-13-2013
Quote:
Originally Posted by Corona688
sudo is not psychic, hence it does not know what aliases and functions you have in your shell.

sudo can only run things that are files. It cannot run aliases and functions.

Why can't this "foo" run sudo itself?
I must assume then there's no possible way of doing this?

Thanks.
# 4  
Old 01-13-2013
Quote:
Originally Posted by dasx
I must assume then there's no possible way of doing this?
sudo cannot run shell functions and aliases. These are shell-builtins, unavailable to anything else outside the shell.

You could put 'foo' into a script file, and have sudo run that.
# 5  
Old 01-13-2013
You could also source $HOME/.bash_profile and run foo using the -c arg to bash eg:

Code:
sudo /bin/bash -c "source $HOME/.bash_profile ; foo"

I'm unsure if the .bash_profile you refer to is root's or the user's if you want root's profile try::

Code:
sudo /bin/bash -c 'source ~/.bash_profile ; foo'

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 01-14-2013
Quote:
Originally Posted by Chubler_XL
You could also source $HOME/.bash_profile and run foo using the -c arg to bash eg:

Code:
sudo /bin/bash -c "source $HOME/.bash_profile ; foo"

I'm unsure if the .bash_profile you refer to is root's or the user's if you want root's profile try::

Code:
sudo /bin/bash -c 'source ~/.bash_profile ; foo'

That does actually work! Thanks!
I was trying to set a function or an alias to do the most part of the writing for me (because if not, this still is faster: sudo -s; source .bash_profile; foo).

Tried this but didn't work:

Code:
suddo(){
	sudo /bin/bash -c 'source ~/.bash_profile ; $1'
}

Doesn't give any error nor it dosn't work.
Do you come up with something?


Thanks!!
# 7  
Old 01-14-2013
The shell won't expand variables (like $1) when enclosed in single quotes.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sudo has no access to exported bash function

Hello. I am running leap 15.1 sudo behavior may differ from other distrib. I have a file with functions definition .... function_1 { echo "Hello world" } export -f function_1 This file is sourced by both /etc/bash.bashrc.local and profile.local. So the functions are... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

3. Shell Programming and Scripting

sudo: sorry, you must have a tty to run sudo

Hi, Have a need to run the below command as a "karuser" from a java class which will is running as "root" user. When we are trying to run the below command from java code getting the below error. Command: sudo -u karuser -s /bin/bash /bank/karunix/bin/build_cycles.sh Error: sudo: sorry,... (8 Replies)
Discussion started by: Satyak
8 Replies

4. Shell Programming and Scripting

sudo: sorry, you must have a tty to run sudo

Hi All, I running a unix command using sudo option inside shell script. Its working well. But in crontab the same command is not working and its throwing "sudo: sorry, you must have a tty to run sudo". I do not have root permission to add or change settings for my userid. I can not even ask... (9 Replies)
Discussion started by: Apple1221
9 Replies

5. Shell Programming and Scripting

ssh foo.com sudo command - Prompts for sudo password as visible text. Help?

I am writing a BASH script to update a webserver and then restart Apache. It looks basically like this: #!/bin/bash rsync /path/on/local/machine/ foo.com:path/on/remote/machine/ ssh foo.com sudo /etc/init.d/apache2 reloadrsync and ssh don't prompt for a password, because I have DSA encryption... (9 Replies)
Discussion started by: fluoborate
9 Replies

6. AIX

sudo log and sudo auditing

Sudo In AIX, how to find out what commands have been run after a user sudo to another user? for example, user sam run 'sudo -u robert ksh' then run some commands, how can I (as root) find what commands have been run? sudo.log only contains sudo event, no activity logging. (3 Replies)
Discussion started by: jalite19
3 Replies

7. Cybersecurity

sudo /bin/sh or sudo su -

we are looking at changing the way we get root on our network. in our current system if an admin needs root access he just gets the root password and uses an su. some of our staff have decided that a sudo to "/bin/sh" will be easer. some of our staff think a sudo to "su -" will be better. I... (0 Replies)
Discussion started by: robsonde
0 Replies

8. UNIX for Dummies Questions & Answers

Unable to use the Sudo command. "0509-130 Symbol resolution failed for sudo because:"

Hi! I'm very new to unix, so please keep that in mind with the level of language used if you choose to help :D Thanks! When attempting to use sudo on and AIX machine with oslevel 5.1.0.0, I get the following error: exec(): 0509-036 Cannot load program sudo because of the following errors:... (1 Reply)
Discussion started by: Chloe123
1 Replies

9. Shell Programming and Scripting

Passing global variable to a function which is called by another function

Hi , I have three funcions f1, f2 and f3 . f1 calls f2 and f2 calls f3 . I have a global variable "period" which i want to pass to f3 . Can i pass the variable directly in the definition of f3 ? Pls help . sars (4 Replies)
Discussion started by: sars
4 Replies
Login or Register to Ask a Question