Save awk record field in bourne shell variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Save awk record field in bourne shell variable
# 1  
Old 08-10-2012
Save awk record field in bourne shell variable

Hello,

I am trying to write a shell script that maintains the health of the passwd file. The goal is to check for duplicate usernames, UID's etc. I am able to find and sort out the UID and login names via awk (which I would like to use), but I can't figure out how to save the record field into a `sh` variable so I can use it for further testing. The algorithm I think of is:

  1. Read in passwd file
  2. determine field seperator
  3. save the UID field ($3) in shell variable.
  4. continue and save the next UID field in another shell variable
  5. compare the two UID's
  6. if the same print user notification
  7. else, continue
Code:
echo "Check for root uid..."
 16 awk -F: '$1 ~/root/ {print "Found Root ID"}' ~/script/passwd
 17 
 18 echo "Check for duplicate user id's..."
 19 counter=0
 20 for line in  ~/script/passwd; do
 21         uidcurrent=`awk -F: '$3' ~/script/passwd`
 22         if [ $counter -eq 0 ]; then
 23                 counter=`expr $counter+1`
 24                 uidprevious=$uidcurrent
 25                 continue
 26         elif [ $uidprevious -eq $uidcurrent ]; then
 27                 echo "Duplicate UID found"
 28         else
 29                 echo "..."
 30                 uidprevious=$uidcurrent
 31         fi
 32 done
 33 echo "Duplicate UID check done"
 34 echo "Check for duplicate login names..."
 35 exit 0

I appreciate your time and effort.

-Daniel
# 2  
Old 08-10-2012
An awk way to get duplicate uids and usernames. Most of the other fields could be harmless when duplicated, like the gid. Also some fields can be okay and empty.

Code:
awk -F':'  ' uname[$1]++ && uname[$1]>1 {print "duplicate user:", $1} 
               uid[$2]++ && uid[$2]>1 {print "duplicate uid:", $2} ' /etc/passwd

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script for field wise record count for different Files .csv files

Hi, Very good wishes to all! Please help to provide the shell script for generating the record counts in filed wise from the .csv file My question: Source file: Field1 Field2 Field3 abc 12f sLm 1234 hjd 12d Hyd 34 Chn My target file should generate the .csv file with the... (14 Replies)
Discussion started by: Kirands
14 Replies

2. Shell Programming and Scripting

Display combination of 4 field uniqe record and along with concatenate 5th and 6th field.

Table ACN|NAME|CITY|CTY|NO1|NO2 115|AKKK|ASH|IND|10|15 115|AKKK|ASH|IND|20|20 115|AKKK|ASH|IND|30|35 115|AKKK|ASH|IND|30|35 112|ABC|FL|USA|15|15 112|ABC|FL|USA|25|20 112|ABC|FL|USA|25|45 i have written shell script using cut command and awk programming getting error correct it and add... (5 Replies)
Discussion started by: udhal
5 Replies

3. Shell Programming and Scripting

Insert field values in a record using awk command

Hi Friends, Below is my input file with "|" (pipe) as filed delimiter: My Input File: HDR|F1|F2||||F6|F7 I want to inser values in the record for field 4 and field 5. Expected output HDR|F1|F2||F4|F5|F6|F7 I am able to append the string to the end of the record, but not in between the... (3 Replies)
Discussion started by: Ajay Venkatesan
3 Replies

4. Shell Programming and Scripting

SED/AWK to edit/add field values in a record

Hi Experts, I am new to shell scripting. Need some help in doing one task given by the customer. The sample record in a file is as follows: 3538,,,,,,ID,ID1,,,,,,,,,,, It needs to be the following: 3538,,353800,353800,,,ID,ID1,,,,,COLX,,,,,COLY, And i want to modify this record in... (3 Replies)
Discussion started by: sugarcane
3 Replies

5. Shell Programming and Scripting

Splitting record into multiple records by appending values from an input field (AWK)

Hello, For the input file, I am trying to split those records which have multiple values seperated by '|' in the last input field, into multiple records and each record corresponds to the common input fields + one of the value from the last field. I was trying with an example on this forum... (4 Replies)
Discussion started by: imtiaz99
4 Replies

6. Shell Programming and Scripting

awk text record - prepend first field to all subsequent fields

Hello everyone, I've suddenly gotten very interested in sed and awk (and enjoying it quite a bit too) because of a large conversion project that we're working on. I'm currently stuck with a very inefficient process for processing text blocks. I'm sure someone here should be able to easily point out... (2 Replies)
Discussion started by: jameswatson3
2 Replies

7. Shell Programming and Scripting

Bourne Shell - Dynamic Variable Error

hi, I am trying to assign a value through 'read' and all works well until I have a space in the in putted value, for the life of me I cant figure out how to escape this. :wall: Any ideas? #!/bin/sh ask_question() { question_text="${1}"; question_answer=""; ... (2 Replies)
Discussion started by: redback
2 Replies

8. Shell Programming and Scripting

Bourne Shell - Problem with while loop variable scope.

Hello I am having issues with a script I'm working on developing on a Solaris machine. The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today. Here is my while loop: last $user | grep -v 'sshd'... (7 Replies)
Discussion started by: DaveRich
7 Replies

9. Shell Programming and Scripting

How to assign record count output of isql to a shell variable ?

isql select count(*) from Table eof How to assign record count output of isql query to a shell variable ? (4 Replies)
Discussion started by: vikram3.r
4 Replies

10. Shell Programming and Scripting

Read file from within AWK and save $1 to a variable

Hi I am very new to NAWK programming so this question is probably going to sound really stupid: I have a NAWK script which contains a DO loop. During each loop it runs a FORTRAN program which in turn generates two output files , each one containing 2 integer variables. I would appreciate it... (8 Replies)
Discussion started by: robbiegregg
8 Replies
Login or Register to Ask a Question