<?xml version="1.0" encoding="UTF-8"?><rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
> <channel><title>Comments on: How to: Check the bash shell script is being run by root or not</title> <atom:link href="http://www.cyberciti.biz/tips/shell-root-user-check-script.html/feed" rel="self" type="application/rss+xml" /><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html</link> <description>This is a Linux sys admin journal by Vivek about sys admin work, Linux tips &#38; tricks, hacks, news and more.</description> <lastBuildDate>Fri, 10 Feb 2012 20:37:43 +0000</lastBuildDate> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>By: Erik</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-173027</link> <dc:creator>Erik</dc:creator> <pubDate>Wed, 03 Aug 2011 19:26:31 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-173027</guid> <description>the problem with the last command is that $EUID is evaluated before sudo is executed.  To get the effect you are looking for you would need to do something like:
sudo bash -c &#039;echo $EUID&#039;</description> <content:encoded><![CDATA[<p>the problem with the last command is that $EUID is evaluated before sudo is executed.  To get the effect you are looking for you would need to do something like:</p><p> sudo bash -c &#8216;echo $EUID&#8217;</p> ]]></content:encoded> </item> <item><title>By: Kyle Fuller</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-172905</link> <dc:creator>Kyle Fuller</dc:creator> <pubDate>Sun, 31 Jul 2011 00:58:10 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-172905</guid> <description>A simpler one-liner:
(( EUID )) &amp;&amp; echo &#039;You need to be root.&#039; &amp;&amp; exit 1</description> <content:encoded><![CDATA[<p>A simpler one-liner:<br
/> (( EUID )) &amp;&amp; echo &#8216;You need to be root.&#8217; &amp;&amp; exit 1</p> ]]></content:encoded> </item> <item><title>By: @break</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-171127</link> <dc:creator>@break</dc:creator> <pubDate>Wed, 04 May 2011 07:53:35 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-171127</guid> <description>There are differences between shells and sub shells and also when you are running some commands directly in a shell and from a bash script.</description> <content:encoded><![CDATA[<p>There are differences between shells and sub shells and also when you are running some commands directly in a shell and from a bash script.</p> ]]></content:encoded> </item> <item><title>By: break</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-170616</link> <dc:creator>break</dc:creator> <pubDate>Thu, 14 Apr 2011 06:43:02 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-170616</guid> <description>Hello Everyone,
Firstly, great blog!
Secondly:
&lt;blockquote&gt;
break@breakdown:~$ cat test.sh
#!/bin/bash
echo $EUID
id -u
&lt;/blockquote&gt;
break@breakdown:~$ ./test.sh
1000
1000
break@breakdown:~$ sudo ./test.sh
0
0
break@breakdown:~$ echo $EUID
1000
break@breakdown:~$ sudo echo $EUID
1000
Vivek can you explain me why last command return my normal-user EUID ? Correct me if I wrong, but &quot;sudo ./test.sh&quot;  is executing the same command as last one (&quot;sudo echo $EUID&quot;) so why test.sh is returning root`s EUID ?
OS: Debian Squeeze</description> <content:encoded><![CDATA[<p>Hello Everyone,</p><p>Firstly, great blog!</p><p>Secondly:</p><blockquote><p> break@breakdown:~$ cat test.sh<br
/> #!/bin/bash</p><p>echo $EUID<br
/> id -u</p></blockquote><p>break@breakdown:~$ ./test.sh<br
/> 1000<br
/> 1000<br
/> break@breakdown:~$ sudo ./test.sh<br
/> 0<br
/> 0<br
/> break@breakdown:~$ echo $EUID<br
/> 1000<br
/> break@breakdown:~$ sudo echo $EUID<br
/> 1000</p><p>Vivek can you explain me why last command return my normal-user EUID ? Correct me if I wrong, but &#8220;sudo ./test.sh&#8221;  is executing the same command as last one (&#8220;sudo echo $EUID&#8221;) so why test.sh is returning root`s EUID ?</p><p>OS: Debian Squeeze</p> ]]></content:encoded> </item> <item><title>By: Andreas</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-159027</link> <dc:creator>Andreas</dc:creator> <pubDate>Thu, 19 Aug 2010 20:38:19 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-159027</guid> <description>Typo @ last example:
echo &quot;You must be a root user&quot; 2&gt;&amp;1
exit 1
has to be
echo &quot;You must be a root user&quot; 1&gt;&amp;2
exit 1
cause you want redirect echo&#039;s stdout to stderr (as it is also done in other examples).  :-)</description> <content:encoded><![CDATA[<p>Typo @ last example:</p><p> echo &#8220;You must be a root user&#8221; 2&gt;&amp;1<br
/> exit 1</p><p>has to be</p><p> echo &#8220;You must be a root user&#8221; 1&gt;&amp;2<br
/> exit 1</p><p>cause you want redirect echo&#8217;s stdout to stderr (as it is also done in other examples).  :-)</p> ]]></content:encoded> </item> <item><title>By: jaysunn</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-156001</link> <dc:creator>jaysunn</dc:creator> <pubDate>Thu, 13 May 2010 15:07:37 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-156001</guid> <description>Just used this in one of my scripts.
Thanks Vivek</description> <content:encoded><![CDATA[<p>Just used this in one of my scripts.<br
/> Thanks Vivek</p> ]]></content:encoded> </item> <item><title>By: Chris</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-154365</link> <dc:creator>Chris</dc:creator> <pubDate>Thu, 18 Mar 2010 23:00:22 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-154365</guid> <description>Big help, thank you!</description> <content:encoded><![CDATA[<p>Big help, thank you!</p> ]]></content:encoded> </item> <item><title>By: pierreact</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-154290</link> <dc:creator>pierreact</dc:creator> <pubDate>Mon, 15 Mar 2010 08:31:43 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-154290</guid> <description>An yet another bashism...</description> <content:encoded><![CDATA[<p>An yet another bashism&#8230;</p> ]]></content:encoded> </item> <item><title>By: lorenzo</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-153862</link> <dc:creator>lorenzo</dc:creator> <pubDate>Tue, 23 Feb 2010 11:58:05 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-153862</guid> <description>@hemanth
&#039;root&#039; username is not assured. It could be different. id=0 is what is used inside the kernel to identify the superuser (aka root)</description> <content:encoded><![CDATA[<p>@hemanth</p><p>&#8216;root&#8217; username is not assured. It could be different. id=0 is what is used inside the kernel to identify the superuser (aka root)</p> ]]></content:encoded> </item> <item><title>By: hemanth</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-152270</link> <dc:creator>hemanth</dc:creator> <pubDate>Mon, 14 Dec 2009 05:22:16 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-152270</guid> <description>I was using this : [[ `id -un` != root ]] &amp;&amp; echo You must be root &#124;&#124; mount /dev/sdb1 /mnt/disk2</description> <content:encoded><![CDATA[<p>I was using this : [[ `id -un` != root ]] &amp;&amp; echo You must be root || mount /dev/sdb1 /mnt/disk2</p> ]]></content:encoded> </item> <item><title>By: gurmad</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-151887</link> <dc:creator>gurmad</dc:creator> <pubDate>Fri, 27 Nov 2009 01:29:52 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-151887</guid> <description>Guys, please help..am a beginner to shell script and lecture gave this assingment:
1 Write a Shell script to automatically check that a specified user is logged in to the
computer.
The program should allow the person running the script to specify the name of the
user to be checked, the frequency in seconds at which the script should check. If a
checking frequency is not specified, it should default to 60 seconds
The script should check for the specified user and if found should output a message
with a “beep” or “bell” sound to the screen stating that the user is logged in. It should
also output a message to a log file stating that the user is logged in and specifying the
date and time.
If the user is not logged in the script should output a message without any “beep”
sound to the screen stating that the user is not logged in.
The script should allow a person running the script to specify the user name on the
command line and optionally, a check frequency in seconds and a third argument “q”
“q” . If “q” is present as a third argument, The script should omit any output to the
screen or “beep” sound but should log to a log file the message stating that the user is
logged in and giving the time.
The command to run the script would be as follows:
checklogin username 30 q where “checklogin” is the script name, “username” is the
name of the user, “30” is the optional frequency to check in seconds, and “q” is the
optional argument to suppress screen output.
The script should check that the number of arguments supplied is between 1 and 3 and
if it is not, should terminate with an error message showing the correct syntax for the
command.
.</description> <content:encoded><![CDATA[<p>Guys, please help..am a beginner to shell script and lecture gave this assingment:</p><p>1 Write a Shell script to automatically check that a specified user is logged in to the<br
/> computer.<br
/> The program should allow the person running the script to specify the name of the<br
/> user to be checked, the frequency in seconds at which the script should check. If a<br
/> checking frequency is not specified, it should default to 60 seconds<br
/> The script should check for the specified user and if found should output a message<br
/> with a “beep” or “bell” sound to the screen stating that the user is logged in. It should<br
/> also output a message to a log file stating that the user is logged in and specifying the<br
/> date and time.<br
/> If the user is not logged in the script should output a message without any “beep”<br
/> sound to the screen stating that the user is not logged in.<br
/> The script should allow a person running the script to specify the user name on the<br
/> command line and optionally, a check frequency in seconds and a third argument “q”<br
/> “q” . If “q” is present as a third argument, The script should omit any output to the<br
/> screen or “beep” sound but should log to a log file the message stating that the user is<br
/> logged in and giving the time.<br
/> The command to run the script would be as follows:<br
/> checklogin username 30 q where “checklogin” is the script name, “username” is the<br
/> name of the user, “30” is the optional frequency to check in seconds, and “q” is the<br
/> optional argument to suppress screen output.<br
/> The script should check that the number of arguments supplied is between 1 and 3 and<br
/> if it is not, should terminate with an error message showing the correct syntax for the<br
/> command.<br
/> .</p> ]]></content:encoded> </item> <item><title>By: Laurie</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-148131</link> <dc:creator>Laurie</dc:creator> <pubDate>Wed, 15 Apr 2009 15:14:42 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-148131</guid> <description>@Diana
You need to put your code into the global .bashrc file, /etc/bashrc. This will become the default .bashrc. Beware though, if you make a user specific bashrc then you need to import the global bashrc</description> <content:encoded><![CDATA[<p>@Diana</p><p>You need to put your code into the global .bashrc file, /etc/bashrc. This will become the default .bashrc. Beware though, if you make a user specific bashrc then you need to import the global bashrc</p> ]]></content:encoded> </item> <item><title>By: Arky</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-147598</link> <dc:creator>Arky</dc:creator> <pubDate>Mon, 09 Mar 2009 07:14:59 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-147598</guid> <description>@ terry
The script works as expected.</description> <content:encoded><![CDATA[<p>@ terry</p><p>The script works as expected.</p> ]]></content:encoded> </item> <item><title>By: Terry</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-144628</link> <dc:creator>Terry</dc:creator> <pubDate>Mon, 11 Aug 2008 21:55:32 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-144628</guid> <description>I think it&#039;s worth mentioning that $UID &amp; $EUID will not return the desired result if you use sudo to run  the script, id -u does however.</description> <content:encoded><![CDATA[<p>I think it&#8217;s worth mentioning that $UID &amp; $EUID will not return the desired result if you use sudo to run  the script, id -u does however.</p> ]]></content:encoded> </item> <item><title>By: Fabio</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-142377</link> <dc:creator>Fabio</dc:creator> <pubDate>Sun, 06 Jan 2008 14:07:05 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-142377</guid> <description>Hi,
if you want to redirect a message to stderr using echo you have to use &quot;1&gt;&amp;2&quot; instead of &quot;2&gt;&amp;1&quot;.
For example,
~ &gt;&gt;  f(){
&gt; echo &quot;to stdout&quot; 2&gt;&amp;1
&gt; echo &quot;to stderr&quot; 1&gt;&amp;2
&gt; }
~ &gt;&gt;  f 2&gt;/dev/null
to stdout #this is printed to stdout
~ &gt;&gt;
bye</description> <content:encoded><![CDATA[<p>Hi,<br
/> if you want to redirect a message to stderr using echo you have to use &#8220;1&gt;&amp;2&#8243; instead of &#8220;2&gt;&amp;1&#8243;.<br
/> For example,</p><p>~ &gt;&gt;  f(){<br
/> &gt; echo &#8220;to stdout&#8221; 2&gt;&amp;1<br
/> &gt; echo &#8220;to stderr&#8221; 1&gt;&amp;2<br
/> &gt; }<br
/> ~ &gt;&gt;  f 2&gt;/dev/null<br
/> to stdout #this is printed to stdout<br
/> ~ &gt;&gt;</p><p>bye</p> ]]></content:encoded> </item> <item><title>By: diana</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-141871</link> <dc:creator>diana</dc:creator> <pubDate>Fri, 16 Nov 2007 10:26:08 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-141871</guid> <description>may be it was the wrong place to post this.Please help me,
I would like to send mail for each success login.i tried with this,
and place this in /root/.bashrc
#!/bin/bash
echo `last $i &#124; head -1 &#124; awk &#039;{print $1&quot; &quot;$3&quot; &quot;$4&quot; &quot;$5&quot; &quot;$6&quot; &quot;$7}&#039;`&#124;while read output;
do
echo $output
ip=$(echo $output &#124; awk &#039;{print $2}&#039; )
on=$(echo $output &#124; awk &#039;{print $3&quot; &quot;$4&quot; &quot;$5}&#039; )
at=$(echo $output &#124; awk &#039;{print $6&#039;} )
echo &quot;User logged in from $ip on $on at $at&quot;&#124;mail -s &quot;Alert: user logged in to server $(hostname) from $ip&quot; ephrondiana@gmail.com
done
but its sending mail for root login only,i need to send mail for users login also.Please help me...</description> <content:encoded><![CDATA[<p>may be it was the wrong place to post this.Please help me,<br
/> I would like to send mail for each success login.i tried with this,<br
/> and place this in /root/.bashrc<br
/> #!/bin/bash<br
/> echo `last $i | head -1 | awk &#8216;{print $1&#8243; &#8220;$3&#8243; &#8220;$4&#8243; &#8220;$5&#8243; &#8220;$6&#8243; &#8220;$7}&#8217;`|while read output;<br
/> do<br
/> echo $output<br
/> ip=$(echo $output | awk &#8216;{print $2}&#8217; )<br
/> on=$(echo $output | awk &#8216;{print $3&#8243; &#8220;$4&#8243; &#8220;$5}&#8217; )<br
/> at=$(echo $output | awk &#8216;{print $6&#8242;} )<br
/> echo &#8220;User logged in from $ip on $on at $at&#8221;|mail -s &#8220;Alert: user logged in to server $(hostname) from $ip&#8221; <a
href="mailto:ephrondiana@gmail.com">ephrondiana@gmail.com</a><br
/> done</p><p>but its sending mail for root login only,i need to send mail for users login also.Please help me&#8230;</p> ]]></content:encoded> </item> <item><title>By: vivek</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-141832</link> <dc:creator>vivek</dc:creator> <pubDate>Mon, 12 Nov 2007 20:37:10 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-141832</guid> <description>Nice suggestion, the post has been updated.</description> <content:encoded><![CDATA[<p>Nice suggestion, the post has been updated.</p> ]]></content:encoded> </item> <item><title>By: Jeff Schroeder</title><link>http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-141831</link> <dc:creator>Jeff Schroeder</dc:creator> <pubDate>Mon, 12 Nov 2007 18:52:13 +0000</pubDate> <guid
isPermaLink="false">http://www.cyberciti.biz/tips/shell-root-user-check-script.html#comment-141831</guid> <description>Make error messages go to STDERR (Standard Error) like they do in other Unix programs. That is the best way to do things.
if [ &quot;$(id -u)&quot; != &quot;0&quot; ]; then
echo &quot;This script must be run as root&quot; 2&gt;&amp;1
exit 1
fi</description> <content:encoded><![CDATA[<p>Make error messages go to STDERR (Standard Error) like they do in other Unix programs. That is the best way to do things.</p><p>if [ "$(id -u)" != "0" ]; then<br
/> echo &#8220;This script must be run as root&#8221; 2&gt;&amp;1<br
/> exit 1<br
/> fi</p> ]]></content:encoded> </item> </channel> </rss>
