LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 10-06-2010, 12:22 AM   #1
henrtm05
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Rep: Reputation: 0
having only one instance of my search results


This script has been solved thanks.

Last edited by henrtm05; 10-08-2010 at 10:49 PM.
 
Old 10-06-2010, 02:00 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I looked up sort's options on my man page, and to be honest, I don't know what is meant by "output only the first of an equal run." Or more specifically, what an "equal run" is.

Regardless, the way I've always done what you're trying to do is to pipe into uniq. Such as:
Code:
finaldata=`echo "$whole" | sort | uniq`
EDIT: Well, after thinking about it, I guess "equal run" is a strange way of saying "repeated sequence." Anyways...

Last edited by Dark_Helmet; 10-06-2010 at 02:05 AM.
 
Old 10-06-2010, 02:21 AM   #3
henrtm05
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Dark_Helmet View Post
I looked up sort's options on my man page, and to be honest, I don't know what is meant by "output only the first of an equal run." Or more specifically, what an "equal run" is.

Regardless, the way I've always done what you're trying to do is to pipe into uniq. Such as:
Code:
finaldata=`echo "$whole" | sort | uniq`
EDIT: Well, after thinking about it, I guess "equal run" is a strange way of saying "repeated sequence." Anyways...
I have actually tried that the result is as follows this is my whole data

one
duplicate
two
two
three
three
two
three

and it gives me the following output with the finaldata=`echo "$whole" | sort | uniq` command.

duplicate
one
three
two
three
two

my command the finaldata=`echo "$whole" | sort -u` does the same thing as finaldata=`echo "$whole" | sort | uniq` I have tried it both ways with the same result.

Last edited by henrtm05; 10-06-2010 at 02:24 AM.
 
Old 10-06-2010, 03:05 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
I'm about to fall asleep, but I don't think there's a problem with what you're trying to do with sort. There is very likely a problem with the script's logic that's causing weird output. Here's why I say that.

I made a simple script, using the test data you gave:
Code:
#!/bin/bash

echo -e "one\nduplicate\ntwo\ntwo\nthree\nthree\ntwo\nthree" | sort -u
the output:
Code:
duplicate
one
three
two
So, sort works as you expect. So the problem must be in the script logic. I can't debug it completely, but I do see a potential problem: you're doing no error-checking at all.

To emphasize the point, what happens if one of the groups in the comma-separated list given by the user doesn't exist, or there's a typo? That can wreak havoc on your assignment to firstgrp. If no match is found, then you'll grep for an empty string between to colons (e.g. "::") on /etc/passwd. I'd be willing to bet that will turn up some matches for non-human accounts, but the script will just keep on processing.
 
Old 10-06-2010, 09:36 AM   #5
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
I notice your $whole contains users (letters) from /etc/passwd and group IDs (numbers) from /etc/group. Can you tell me why you're doing that?

If I were you I would commend out the following lines
Code:
groupadd $newgroup
usermod -G $newgroup "$finaldata"
and echo the variables so you know how the script is behaving. Also your system won't go haywire from a buggy script. I hope you're not running this as root with these serious bugs. I also think that usermod command needs to be in a for or while loop because $finaldata contains newline chars. I'm not sure usermod can handle that (I may be wrong).

Like Dark Helmet says you need error checking. Before running
Code:
whole=`echo -e "$firstgrp\n$remaingrpsmod"`
You should check that there aren't any zero length strings.
Code:
if [ ! -z $firstgrp ];then
  whole=`echo -e "$firstgrp\n$remaingrpsmod"`
fi;
This is all quick info off the top of my head so be sure to research my statements. Also please make use of the forum formatting such as the code tags (which can be viewed when you go into the advanced post editor by clicking 'Go Advanced'). Gotta run!

Last edited by sag47; 10-06-2010 at 09:40 AM.
 
Old 10-06-2010, 11:46 PM   #6
henrtm05
LQ Newbie
 
Registered: Sep 2010
Posts: 11

Original Poster
Rep: Reputation: 0
I am taking users from multiple groups and adding them into one and not duplicating the process if a user is in two of those groups.
 
  


Reply



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
LQ search results format newbiesforever LQ Suggestions & Feedback 4 03-20-2010 08:13 PM
Search results in RSS Rotwang LQ Suggestions & Feedback 24 01-18-2007 11:44 AM
Idea for better search results ErrorBound LQ Suggestions & Feedback 1 07-27-2006 06:52 PM
Filter Search Results wwnexc Linux - Software 1 05-08-2006 11:06 AM
search within results toastermaker LQ Suggestions & Feedback 1 11-20-2003 01:11 AM

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

All times are GMT -5. The time now is 06:49 AM.

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