LinuxQuestions.org
Review your favorite Linux distribution.
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, 01: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 11:49 PM.
 
Old 10-06-2010, 03:00 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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 03:05 AM.
 
Old 10-06-2010, 03: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 03:24 AM.
 
Old 10-06-2010, 04:05 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
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, 10:36 AM   #5
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,832
Blog Entries: 36

Rep: Reputation: 451Reputation: 451Reputation: 451Reputation: 451Reputation: 451
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 10:40 AM.
 
Old 10-07-2010, 12:46 AM   #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


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


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