LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-13-2012, 05:12 AM   #1
Monalisa Chan
LQ Newbie
 
Registered: Nov 2012
Posts: 8

Rep: Reputation: Disabled
How to check all user ids are unique


I want to check all user ids are unique or not in my bash shell.Which command should I use in my script?

Last edited by Monalisa Chan; 11-13-2012 at 05:24 AM.
 
Old 11-13-2012, 05:25 AM   #2
kakaka
Member
 
Registered: Sep 2003
Posts: 382

Rep: Reputation: 87
Do you mean that you want to write a bash script to check that each User ID has been assigned to only one User?
 
Old 11-13-2012, 05:29 AM   #3
Monalisa Chan
LQ Newbie
 
Registered: Nov 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
yes
 
Old 11-13-2012, 05:37 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Quote:
Originally Posted by Monalisa Chan View Post
I want to check all user ids are unique or not in my bash shell.Which command should I use in my script?
I'm not sure what you mean by the bold part, but have a look at this:
Code:
awk -F: '_[$3]++ == "1" { print $3 }' /etc/passwd
The above will print the UID's that are used more then once (if any).
 
Old 11-13-2012, 05:38 AM   #5
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 103Reputation: 103
Lightbulb

Quick hack...


Code:
#! /bin/bash

cat /etc/passwd | sort |tee orig |  uniq > fixed | diff -u orig fixed > results
rm orig fixed
less results
 
Old 11-13-2012, 06:09 AM   #6
Monalisa Chan
LQ Newbie
 
Registered: Nov 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Both of your commands cannot be used in my script.I found error. unexpected EOF while looking for matching `" '
 
Old 11-13-2012, 06:15 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Quote:
Originally Posted by Monalisa Chan View Post
Both of your commands cannot be used in my script.I found error. unexpected EOF while looking for matching `" '
Without knowing what your script looks like and how the above is implemented we cannot help you any further.

BTW: Please put your script/data inside [code] ... [/code] tags it preserves all spacing. If you don't know how: LQ - BB Code List.
 
Old 11-13-2012, 06:31 AM   #8
Monalisa Chan
LQ Newbie
 
Registered: Nov 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Code:
 
#! /usr/bin/bash
echo "Name: "
echo "Test criteria: All user ids are unique"
userid= awk -F: ' [$3]++ == "1" { print $3 }' /etc/passwd
if [$userid>1 ]
then
echo "Exception: YES"
cat /etc/passwd
else
echo "Exception:NO"
fi
 
Old 11-13-2012, 06:45 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,768

Rep: Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886
First, probably just a copy problem: #! must be the first two chars of the files, there can be no lines before it!

userid= here something is missing. You are not allowed to use space before and after = sign, and also you would need to assign the return of awk. It has different syntax:
userid=$(awk ... )

Probably it will return more values, the following if may fail. Also you would use " around and space after and before [ (also would prefer [[ )
if [[ "$userid" > 1 ]]
 
1 members found this post helpful.
Old 11-13-2012, 06:45 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
I see multiple mistakes:

- You did not copy/paste my solution correctly (missing _)
- Storing the output of a command isn't syntactically correct. The correct way:
Code:
variable=$( command )
- Spaces are important, you are missing them here:
Code:
[$userid>1 ]
The above script can be re-written to:
Code:
#!/usr/bin/bash

echo "Name: "
echo "Test criteria: All user ids are unique"

userid=$( awk -F: '_[$3]++ == "1" { print $3 }' /etc/passwd )

if [[ $userid ]]
then
  echo "Exception: YES"
  for i in $userid
  do
    grep $i /etc/passwd
  done
else
  echo "Exception:NO"
fi
These might be of value:

Bash resources:

Last edited by druuna; 11-13-2012 at 07:14 AM. Reason: Fixed error: missing fi
 
Old 11-13-2012, 07:45 AM   #11
Monalisa Chan
LQ Newbie
 
Registered: Nov 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
Thank you for your help.but still have error.
Error is no such file or directory in below line.
Code:
userid=$( awk-F: '_[$3]++=="1" { print $3 }' /etc/passwd )
 
Old 11-13-2012, 07:50 AM   #12
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
As stated before: Spaces are important!!!!
Code:
userid=$( awk-F: '_[$3]++=="1" { print $3 }' /etc/passwd )
Should be:
Code:
userid=$( awk -F: '_[$3]++ == "1" { print $3 }' /etc/passwd )
 
Old 11-13-2012, 08:02 AM   #13
Monalisa Chan
LQ Newbie
 
Registered: Nov 2012
Posts: 8

Original Poster
Rep: Reputation: Disabled
If I don't put space after awk,output comes out with error message.
If I put space after awk, NO OUTPUT COMES OUT
 
Old 11-13-2012, 08:57 AM   #14
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 982
Blog Entries: 2

Rep: Reputation: 244Reputation: 244Reputation: 244
If there's no output it means you have no duplicated uids.

Code:
cat /etc/passwd /etc/passwd   >  my_dup_passwd
awk -F: '_[$3]++ == "1" { print $3 }' my_dup_passwd
Now you should get some output showing that intentional duplication.
 
Old 11-13-2012, 09:04 AM   #15
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,768

Rep: Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886Reputation: 2886
you can also insert the following line into your script:
set -xv
(second line)
and you will see what's happening
 
  


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
LXer: Google Chrome to do away with unique IDs LXer Syndicated Linux News 0 03-12-2010 11:50 AM
User IDs DarReNz Solaris / OpenSolaris 4 08-16-2006 07:13 AM
Use of LOGNAME for non-unique user ids Geoff_Hawke Linux - Newbie 1 10-13-2005 01:37 AM
Linux User IDs ? xwastedmindx Linux - General 2 11-12-2003 08:52 PM
User IDs confuse me. davee Linux - Newbie 1 02-11-2003 06:42 AM

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

All times are GMT -5. The time now is 03:32 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration