PDA

View Full Version : [Script Shell] How can i check for sudo



geearf
August 4th, 2005, 11:24 AM
Hello,

I am doing a little install script for my work using shell, and I want to test wheter the script was started with or with sudo.
If it was without, and I need it I will put it in front of my command ..

But i cannot find a way to check for sudo ($USER only gives me the user).

Thanks for the tip

Sam
August 4th, 2005, 11:38 AM
#! /bin/sh

ROOT_UID="0"

#Check if run as root
if [ "$UID" -ne "$ROOT_UID" ] ; then
echo "You must be root to do that!"
exit 1
fi

geearf
August 4th, 2005, 04:42 PM
Hello,
I will try this right now,

Thanks a lot :)

geearf
August 4th, 2005, 05:41 PM
Thanks it works flawlessly for my needs :)

Would you also happen to know how can I store 'file /usr/bin/java' in a variable in my script ?


Thanks

Sam
August 4th, 2005, 10:30 PM
You need ` quotes to evaluate an expression.

#! /bin/sh

FILE_JAVA="`file /usr/bin/java`"

echo "$FILE_JAVA"

geearf
August 4th, 2005, 10:58 PM
Oh ok,

thank you very much for your help,

Do you happen to know a good website for learning shell script (so that I won't bother you too much :) ) ?

Sam
August 4th, 2005, 11:23 PM
Just Google for "bash tutorial (http://www.google.com/search?&q=bash%20tutorial)" and you'll find a lot of websites...

Here's some: Introduction to bash shell scripting (http://www.start-linux.com/articles/article_66.php), a good start
AWK, GRP, SED tutorial (http://linuxcourse.rutgers.edu/lessons/lesson9/shell_script_tutorial.html), useful for parsing command outputs
Introduction to Bash Programming pt. 1 (http://www.geocities.com/tipsforlinux/articles2/043.html), pt. 2 (http://www.geocities.com/tipsforlinux/articles2/044.html)

Ask if you have a question! Happy scripting :wink:

geearf
August 5th, 2005, 12:19 AM
Thanks,

I have looked at shell script and stuff like that, but I always found about the same stuff, that is why I asked here in fact :)

Thanks for your help,

Sam
August 5th, 2005, 12:58 AM
You're welcome!