Operating System - HP-UX
1834908 Members
2577 Online
110071 Solutions
New Discussion

Re: grep: illegal option----????????

 
SOLVED
Go to solution
Constantine_1
Occasional Advisor

grep: illegal option----????????

#! /bin/ksh
#
print "Login Name\t" "Pri Group\t" "Member of Group"

grep -i "${1}" /etc/passwd > /dev/null
if [ $? -eq 0 ]; then
grep -i "${1}" /etc/passwd | awk 'BEGIN {
print "\n"
FS=":"
OFS="\t" }
{
GROUP=""
PGRP=""
"grep -i "$4" /etc/group | cut -f 1 -d ':'" | getline PGRP;
"echo "$5" | cut -f 1 -d ','" | getline RNAME;
"echo "$5" | cut -f 3 -d ','" | getline RNUM;
"/usr/bin/groups -g " $1 | getline GROUP;
print $1"\t\t" PGRP"\t" "\t" GROUP
print " ";
}'
else
echo "No user details found....Sorry. Try again..."
fi


exit 0;

I can't figure out what's wrong
Need fresh eyes on it can somebody help
runs find with desirable output and sneezes in the middle and than continues

live and learn every day
8 REPLIES 8
Leif Halvarsson_2
Honored Contributor

Re: grep: illegal option----????????

Hi,
I get the same error. Check your passwd file if there is any fields with "-" in. grep interpret it as an option.
Constantine_1
Occasional Advisor

Re: grep: illegal option----????????

Hi Leif
I got them ..but I need to keep them ...what can I do to ignore them

Thanks

live and learn every day
Umapathy S
Honored Contributor

Re: grep: illegal option----????????

Constantine,
Check whether /etc/passwd contains entry for nobody with -2 etc.
It is causing the trouble.

HTH,
Umapathy

Arise Awake and Stop NOT till the goal is Reached!
H.Merijn Brand (procura
Honored Contributor
Solution

Re: grep: illegal option----????????

Force it to be a non-option:

grep -i -- "$4" ...

double minus is for most commands (as long as the coder follows the rules) the end of options marker

Enjoy, have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: grep: illegal option----????????

Here is my best guess:

You are feeding this script an argument that contains a "-" and grep is then trying to interpret your ${1} as an option.

Change your grep -i "${1}" to
grep -i -- "${1}"

The -- instructs getopt() that the argument list is ended; anything beyond this is not to be evaluated as an option.
If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: grep: illegal option----????????

Using field $4 in the following line-

"grep -i "$4" /etc/group | cut -f 1 -d ':'" | getline PGRP;

Will get an error if $4 is a negative number (like user nobody) and it thinks you are passing another option.

If you code
grep -i -- "$4" ...

then grep will not try to treat $4 as an option if it begins with a "-"

HTH

-- Rod Hills
There be dragons...
Leif Halvarsson_2
Honored Contributor

Re: grep: illegal option----????????

Hi,
A different method:

try
a=-2
grep \\$(echo $a) /etc/passwd
Constantine_1
Occasional Advisor

Re: grep: illegal option----????????

Hi All
Thank you for your help

live and learn every day