[CLUE-Tech] shell programming
Dave Price
davep at kinaole.org
Wed Dec 11 03:26:36 MST 2002
On Wed, Dec 11, 2002 at 09:48:32AM -0100, Marcin wrote:
>
> Hello,
>
> I don't understand something.
> I've written something like this:
>
> #! /bin/bash
> if [ $user="root" ]
> then
> echo "Hello root"
> else
> echo "You aren't a root"
> fi
>
> I'm loged in as user e.g. Orion and if I run this script I get "Hello
> root", but I'm not a root only user Orion.
> If I correct second line (add sign "!") :
> if [ ! $user="root" ]
> then my script works OK and when I run it I get "You aren't a root"
> Why this script works OK only with the sign "!" ?
> But if I now do command "su root" and I became a root and run this
> script as root it again doesn't work OK :-( I get "You aren't a root"
> Do someone know this and could explain me?
> How I should write this script?
> I use RH 7.3 and bash shell.
>
Marcin,
(reaches for O'Reilly's bash book)
A couple of things ... $USER and $user are not the same - the
environment variable is case sensitive.
You need spaces between the $VAR, the condition, and the test value (see
below... also, (you had this right) - spaces around the []'s are required
too!
When you 'su' your $USER variable does not change, but your $UID
variable does. (root's UID is 0 {zero})
Try running this version of your test case as a normal user, an su'd
user, and a shell initially logged in as root (you may need to ctrl-alt
f# out of X and log in as root to see the last case work... alt-f7
should get your X back, unless you have whacky video drivers like i used
to!!! -- If you are not running X, just switch to another virtual
console and login as root)
Try setting the lower-case $user and $uid with the bash built-in
'export' - see my sample.
<snippage>
#!/bin/bash
# utest - Conditional test of $USER and $UID - dap 12.10.2002
# Extra debug .... let's view the variables
echo "debug: $USER $UID"
# This will always be null, unless you export user or uid by hand
# Note: MS-DOS environment vars are upper case in reality, but not case
# sensitive in practice.
echo "case-sensitivity debug: $user $uid"
if [ $USER = root ]
# Not the same as $USER=root or $user = root
then
echo "Hello (user) root"
else
echo "You aren't a (user) root"
fi
if [ $UID = 0 ]
then
echo "Hello (uid) root"
else
echo "You aren't a (uid) root"
fi
</snippage>
<sample>
[davep at ldpx:~/bin]$ utest
debug: davep 501
case debug:
You aren't a (user) root
You aren't a (uid) root
[davep at ldpx:~/bin]$ export user=billgates
[davep at ldpx:~/bin]$ export uid=666
[davep at ldpx:~/bin]$ utest
debug: davep 501
case debug: billgates 666
You aren't a (user) root
You aren't a (uid) root
[davep at ldpx:~/bin]$ su
# I am su'd to root here ...
[root at ldpx bin]# export user=foo
[root at ldpx bin]# export uid=bar
[root at ldpx bin]# ./utest
debug: davep 0
# Note my new UID value
case debug: foo bar
You aren't a (user) root
Hello (uid) root
# Note that the lower-case vars could have been anything and do not
# impact the conditional tests
</sample>
Hope this helps!
aloha,
dave
More information about the clue-tech
mailing list