Sudo function

I have my .bash_profile with this function for instance:

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:

$ sudo -s
[password]
$ source ~/.bash_profile
$ foo

That will work, but I'd rather have a fast way of doing it.
Any tips?

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.

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.

You could also source $HOME/.bash_profile and run foo using the -c arg to bash eg:

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::

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:

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!!

The shell won't expand variables (like $1) when enclosed in single quotes.

How should I proceed, then?

Thanks.

Move the closing single quote just before $1, or use double quotes.

Amazing. Finally got it working!

Only bad thing is I must use quotes if I'm passing any options to my custom functions but hey, much better than before!

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

Example function:

cache(){
	local OPTIND
	while getopts :sdc opt
		do
			case $opt in
				s) 	echo User Cache:
					peso -d0 -r /Users/****/Library/Caches/com.Huyyyk.client/ | awk '{print $1}'
					;;
					
				d) 	echo System Cache:
					peso -d0 -r /Users/****/Library/Caches/.Huyyyk/ | awk '{print $1}'
					;;
					
				c) 	if [ `id -u` -ne 0 ]
						then	warning "YOU NEED TO BE ROOT IN ORDER TO CLEAR THE CACHE"
								break
					fi
					echo Cache:
					peso -d0 -r /Users/****/Library/Caches/.Huyyyk/ | awk '{print $1 $2 $3}'
					echo
					echo "ARE YOU SURE YOU WANT TO CLEAR THE CACHE?" 
					echo "(y/n)..."
					read -n1 resp
					echo
					echo
					if [ $resp = "y" ]
						then	rm -rf /Users/****/Library/Caches/.Huyyyk/*
								echo "CACHE HAS BEEN CLEARED"
								echo
						else 	echo ABORTED
								echo
					fi
					;;
					
				\?)	echo "Options:	-s: User cache"
					echo "		-d: System cache"
					echo "		-c: Clear cache"
					;;
			esac
		done
}

In order to clear cache as sudo I must type:

suddo "cache -c"

I need the quotes. As I said though, great improvement.

THANK YOU ALL!!! :slight_smile:

You only need those quotes if you use "$1". Using "$@" would not require them.

$ cat myScript
function suddo1 {
  echo "$0: $1"
}

function suddo2 {
  echo "$0: $@"
}


suddo1 cache -c
suddo2 cache -c

$ ./myScript
suddo1: cache
suddo2: cache -c

But it doesn't matter. It's good practice to use quotes :slight_smile:

Thanks Scott!!!

This didn't work though:

suddo(){
	sudo /bin/bash -c "source ~/.bash_profile ; $@"
}

Tried this other and worked. :slight_smile:

suddo(){
	a=$@
	sudo /bin/bash -c "source ~/.bash_profile ; $a"
}

In that case, perhaps you could try:

suddo(){
	sudo /bin/bash -c "source ~/.bash_profile ; $*"
}

But you would still need to use double quotes sometimes...

That does indeed work, thanks!

Does anybody know of somewhere I could look up all ways of using the $ ?

I mean:

$1, $2, $9
$!
$@
$*
$?
(I'm sure there's more...)

Try searching the internet for "shell special variables"

I would not use it like this, though, because the quoting works odd this way, and so it becomes less predictable what the outcome will be and it is a sudo command after all. I would prefer to use $@ and quotes around the command...

But with $@ I don't need to use the quotes really, it works without them.

---------- Post updated at 03:34 PM ---------- Previous update was at 03:33 PM ----------

:b:

$1 - $9       these variables are the positional parameters.

$0            the name of the command currently being executed.

$#            the number of positional arguments given to this
              invocation of the shell.

$?            the exit status of the last command executed is
              given as a decimal string.  When a command
              completes successfully, it returns the exit status
              of 0 (zero), otherwise it returns a non-zero exit
              status.

$$            the process number of this shell - useful for
              including in filenames, to make them unique.

$!            the process id of the last command run in
              the background.

$-            the current options supplied to this invocation
              of the shell.

$*            a string containing all the arguments to the
              shell, starting at $1.

$@            same as above, except when quoted.

Source.