LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to grep all users's certain file (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-grep-all-userss-certain-file-610954/)

packets 01-03-2008 07:50 PM

how to grep all users's certain file
 
Can someone lead me how to do this. I have a bunch of users having .qmail in their home directory. How can I grep or cat whats inside in each of the user's .qmail file? I'm thinking of a bash script with 'for' syntax. Here is the sample:

Quote:

#!/bin/bash

users=`cat /etc/passwd | awk -F: '{print $1}'`

for id in $users
do
cd ~$id/ | cat .qmail
done
Now that I got all the list of users, didn't know how to cat/grep the .qmail file in each users. Please advise.


Thanks!

GushpinBob 01-03-2008 08:02 PM

change:

Code:

cd ~$id/ | cat .qmail
to
Code:

if [ -d ~${id} ] ; then
  cd ~${id}
  if [ -e ".qmail" ] ; then
      cat .qmail
  done
done

EDIT:
What this code modification should do (I hope) is that the script will go into the user's home directory (if it exists), and cat the .qmail file (if it exists).
Haven't tested it but give it a shot :)

packets 01-03-2008 08:03 PM

Thanks will try that one.

packets 01-03-2008 08:10 PM

I think there's a mistake in your advice. You didn't put a fi.

Quote:

if [ -d ~id ] ; then
cd ~${id}
if [ -e ".qmail" ] ; then
cat .qmail
fi
fi
Though added an fi, still didn't work

ghostdog74 01-03-2008 08:22 PM

Code:

awk -F":" '
{
    path=$6"/.qmail"
    while ( ( getline line < path) > 0 ) {
          print line
    }
    close( path )
}' /etc/passwd


GushpinBob 01-03-2008 08:35 PM

Quote:

Originally Posted by packets (Post 3010473)
I think there's a mistake in your advice. You didn't put a fi.



Though added an fi, still didn't work

D'oh! My bad, I get the for loops and the if statements mixed up sometimes. Sorry that my solution didn't work anyhow.


All times are GMT -5. The time now is 09:01 PM.