LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-15-2006, 05:14 AM   #1
furquan
Member
 
Registered: Feb 2002
Posts: 30

Rep: Reputation: 15
Shell Script :- to get UID and GID


Hi,

i am trying to write a scrpt (eg : me.sh) and pass arguments to it so that if it is executed on the command prompt "me.sh -g" i should get the groupid of the user who has issued this
command. Similarly if some one issues "me.sh -u" i should get the userid.


Can some one assit ?

Thanks
 
Old 02-15-2006, 05:21 AM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,743
Blog Entries: 4

Rep: Reputation: 76
Well, I could write it for you, but if you want to learn why not post what you have so far and we'll go from there...

Hints: $1 $2 $3 etc are for accessing positional arguments from within a script.
'id' command will tell you uid and gid.
 
Old 02-15-2006, 05:30 AM   #3
furquan
Member
 
Registered: Feb 2002
Posts: 30

Original Poster
Rep: Reputation: 15
Hi bulliver,

Thanks for the quick reply, what i have done so far is mentioned below, but m not sure if i make sence here, but here it goes :-


-------------

#!/bin/sh


if ["$1" - g]; then
echo -n "Your Groupid is" gid

else

if ["$1" - u]; then
echo -n "Your Userid is" uid

else

if ["$1" - u - g]; then
echo -n "Your Userid and Grouid is" uid gid

exit 1

fi
fi
fi
--------------------
PS :- Let me clarify that i am a newbie, so still
 
Old 02-15-2006, 05:32 AM   #4
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
some more hints: http://www.linuxcommand.org/wss0090.php
 
Old 02-15-2006, 05:45 AM   #5
furquan
Member
 
Registered: Feb 2002
Posts: 30

Original Poster
Rep: Reputation: 15
Hi Muha,

i have already gone through that link, thankx to google. but since this is my first script, i need some assistance with my code.

Let me know guys, if you need any further inputs on what i m doing.
 
Old 02-15-2006, 05:52 AM   #6
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,743
Blog Entries: 4

Rep: Reputation: 76
Code:
if ["$1" - g];
Your test constructs are not correct. See string comparison here:
http://www.tldp.org/LDP/abs/html/comparison-ops.html

Code:
echo -n "Your Groupid is" gid
'gid' and 'uid' are not real commands (on my system anyway). As I mentioned, you can use 'id' and parse the output using awk. ( http://www.tldp.org/LDP/abs/html/awk.html )

Code:
exit 1
You don't need an explicit exit. The exit code will be set automatically depending on the status of the scripts exit ('0' for success, non-zero otherwise). You can check the scripts exit status with "echo $#"

Finally, you have:
Code:
if
else
if
else
if
else
fi fi fi
which is not correct syntax.

Try:
Code:
if
elif
elif
fi
I would help you more specifically but I suspect this is homework

Last edited by bulliver; 02-15-2006 at 05:53 AM.
 
Old 02-15-2006, 06:04 AM   #7
furquan
Member
 
Registered: Feb 2002
Posts: 30

Original Poster
Rep: Reputation: 15
hahaha, not really an homework, but u can say that. I have just started to learn scripting and i have been assigned by my friend to do this as an assignment.

I'll get back after i look at the docs that you have mentioned.

Thanks again for every thing
 
Old 02-15-2006, 06:15 AM   #8
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
for gid,
Code:
id | sed 's/^.*=//;s/(.*$//'
for uid,
Code:
id | sed 's/^uid=//;s/(.*$//'
 
Old 02-15-2006, 06:21 AM   #9
furquan
Member
 
Registered: Feb 2002
Posts: 30

Original Poster
Rep: Reputation: 15
Hi Kshikid,


Would appreciate if you could also explain what both those ID commands do.


cheers
 
Old 02-15-2006, 06:38 AM   #10
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
key request in your question is to know the uid and gid of the user!

id command displays UID for the user and GID for which group he belongs to !

its just a manipulation using stream editor
 
Old 02-15-2006, 06:48 AM   #11
furquan
Member
 
Registered: Feb 2002
Posts: 30

Original Poster
Rep: Reputation: 15
Yes i do need to know the GID and UID, but only if the user asks for either of them or both. For Example the script is called "me.sh" so the user should be able to pass argument to this script, like this :-

"me.sh -g" to know the groupid or
"me.sh -u" to know the userid or
"me.sh -g -u" to know both.

I hope this clarifies any doubts.

Thanks
 
Old 02-15-2006, 06:49 AM   #12
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 11, Sabayon 3.1
Posts: 1,464

Rep: Reputation: 46
dont forget that "id" takes options. i'm sitting on a hp-ux box, so linux might be different but i can do:

# id -un
root

# id -nG
sys root other bin adm daemon mail lp users

as you can see this saves using 'sed' to strip out the () etc.
 
Old 02-15-2006, 06:58 AM   #13
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by satinet
dont forget that "id" takes options. i'm sitting on a hp-ux box, so linux might be different but i can do:

# id -un
root

# id -nG
sys root other bin adm daemon mail lp users

as you can see this saves using 'sed' to strip out the () etc.
then get the command from directory,
/usr/xpg4/bin/id
that would do

i just made use of /usr/bin/ entries
 
Old 02-15-2006, 07:16 AM   #14
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 11, Sabayon 3.1
Posts: 1,464

Rep: Reputation: 46
yeah, sorry maybe i should go back to the HP forums :-D

well i post there too!
 
Old 02-15-2006, 07:28 AM   #15
satinet
Senior Member
 
Registered: Feb 2004
Location: England
Distribution: Slackware 11, Sabayon 3.1
Posts: 1,464

Rep: Reputation: 46
Code:
case $1 in

-u)     /usr/xpg4/bin/id -un
        ;;
-g)     /usr/xpg4/bin/id -uG
        ;;
*)      echo "usage -u -g"
        exit 1
        ;;
esac

if test -n $2
then
case $2 in

-u)    /usr/xpg4/bin/-id -un
      ;;
-g)    /usr/xpg4/bin/id -uG
      ;;
*)    echo "usage -u -g"
      exit 1
      ;;
esac
fi
something like that might suffice. you dont really need the test -n. depends on your preferences.
 
  


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
what is uid and gid rmanocha Linux - Software 9 08-18-2008 11:03 PM
What is my uid/gid? Jeebizz Slackware 2 11-22-2005 11:39 AM
How can I change a user's uid and gid? homesp Debian 2 05-31-2005 02:39 PM
vpopmail:- How can we efficiently use uid & gid amit_28oct Linux - Networking 4 09-30-2004 01:02 AM
changing uid, gid zeke1955 Linux - General 4 01-09-2004 11:53 PM


All times are GMT -5. The time now is 07:23 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration