Click to See Complete Forum and Search --> : shell script question


nillo
11-07-2002, 11:41 PM
i try to run a script i wrote and get the following:


$ mygrep2 un322
mygrep2[3]: un322:lqPPBjOWp6inA:284:80:jacob: bad number
un322:lqPPBjOWp6inA:284:80:jacob
mygrep2[3]: larsen:/home/unix3/un322:/bin/ksh: bad number
larsen:/home/unix3/un322:/bin/ksh
mygrep2[7]: shift: bad number


the contents of the script are as follows:

$ cat mygrep2
for file in `grep $1 /etc/passwd`
do
if [ $file -eq 0 ]
then echo $1" was not found in /etc/passwd"
else echo $file
fi
shift
done

im havin a bit o trouble with for statements

help pleasE?

much thanx ;)

Hena
11-08-2002, 02:36 AM
Well it seems that grep returns to $file the row it finds from the /etc/passwd file. Then you compare those to number and that fails.

I haven't tried this but it might work.
#!/bin/bash
row=`grep $1 /etc/passwd`
if [[ $row ]]
then echo "$row"
else echo "Was not found from /etc/passwd"
fi
exit 0

santellij
11-14-2002, 10:10 AM
I think the last reply was a fine answer but you can also suppress the normal output and check the return status. I'm not in front of my linux computer to check the man page but I think it's -q. Then you could check the return status (echo $?).

something like:

#!/bin/bash
/bin/grep -q $1 /etc/passwd
if [ $? -eq 0 ]
then echo $1" was not found in /etc/passwd"
else
echo $file
fi