LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-20-2014, 04:11 AM   #1
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Rep: Reputation: Disabled
Bash question....


Hey Guys,

I've been studying Linux for several months now and am trying to get my head around bash scripting.

I'm playing around with a simple script that will echo one thing if it's run by a particular user and will echo something else if run by another user again..

I've got this so far:

Code:
!/bin/bash

if [[ $EUID -eq 0 ]]; then
   echo "root"

if [[ $EUID -eq 500 ]]; then
   echo "jon"
else
exit 1
fi
fi
The UID for jon is 500 and of course root is 0... in theory I'm sure this should work.. but maybe my syntax is wrong?

Any help would be appreciated.

Thanks
Jon
 
Old 05-20-2014, 04:49 AM   #2
Shadow_7
Senior Member
 
Registered: Feb 2003
Distribution: debian
Posts: 4,137
Blog Entries: 1

Rep: Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875Reputation: 875
I'm more likely to enclose the values in quotes and use the = sign. Although there's probably more modern syntax options these days.

if [ "$EUID" = "0" ]; then
...
fi

Although your script seems a bit unneccessary.

$ whoami

$ groups | awk '{ print $1; }'

would both return basically the same end result. Without having to hard code an ID value or introduce conditionals.
 
Old 05-20-2014, 04:59 AM   #3
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
Hey,

I actually just figured it out.. I ended up with:

Code:
!/bin/bash

if [[ $EUID -eq 500 ]]; then
   echo "Jon"

else

if [[ $EUID -eq 0 ]]; then
   echo "root"

exit 1
fi
fi
However I'm open to a way of carrying it out more efficiently.. I like the idea of the whoami variable but I was thinking that the UID would be more solid.. but I guess it doesn't really matter in that instance. Less code is better, I'll have a play around with it more.

I guess my next step is to add another section where it will return a usage message if run by a different user.
 
Old 05-20-2014, 06:06 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,434

Rep: Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790Reputation: 2790
I don't see why the exit cmd is called if root user? Also, 1st line is wrong...
How about
Code:
#!/bin/bash
if [[ $EUID -eq 500 ]]
then
   echo "Jon"
elif [[ $EUID -eq 0 ]]
then
   echo "root"
else
    echo "neither ..."
fi
 
Old 05-20-2014, 07:31 AM   #5
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
Hey,

Thanks for your reply.

The first line 'shebang' in my script does have the hash, it just for some reason didnt paste into here

I put the Exit in as I want the script to end if none of the statements are true.. but I guess it will end anyway based on what you said?

Tried elif before, but couldn't get it working. Must have had some incorrect syntax.

Will have another play around.

Thanks
Jon
 
Old 05-20-2014, 09:32 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Arch
Posts: 10,037

Rep: Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203Reputation: 3203
My general suggestion when performing arithmetic tests is to use the brackets designed for that:
Code:
if (( EUID == 0 ))
then
...
The nice thing about them is you can use all the traditional mathematic symbols, such >, <, >=, etc
 
Old 05-20-2014, 11:32 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608Reputation: 3608
Quote:
Originally Posted by jonnybinthemix View Post
I've been studying Linux for several months now and am trying to get my head around bash scripting.
FWIW here's some more:

BASH intros:
http://www.gnu.org/software/bash/man...ode/index.html
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html

BASH scripting guides:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
# these are must reads so do read them!

Common questions / problems:
http://mywiki.wooledge.org/ParsingLs
http://mywiki.wooledge.orgDontReadLinesWithFor
http://mywiki.wooledge.org/UsingFind
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

Also see:
http://www.linuxquestions.org/questi...l-links-35334/

The Advanced BASH scripting guide:
http://www.tldp.org/LDP/abs/html/

Bourne shell (compatibility):
http://www.grymoire.com/Unix/Sh.html
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Bash for yes/no question nkitmitto Programming 12 07-11-2010 07:18 PM
bash question slimm609 Linux - General 3 06-12-2007 04:47 AM
Bash question lnxduck Programming 3 09-30-2005 08:50 AM
a bash question thanhVic Linux - General 2 02-05-2005 04:39 AM
BASH question tpe Linux - Software 6 11-17-2004 08:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration