LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-07-2009, 02:15 PM   #1
richinsc
Member
 
Registered: Mar 2007
Location: Utah
Distribution: Gentoo Linux (3.0.6-gentoo)
Posts: 222

Rep: Reputation: 32
Bash Help: Check if file exists


I am trying to write a script that will check to see if .forward file exists in all users home directories. Then I am having those entries logged to a file. If the file does not exists it just states that is it missing. If the file does exist I log it then want to parse the file to see what the file contains to ensure that forward is correct.

Here is the code that I have so far. While it works, it seems to pull all user names from whole file instead of reading user entries line by line. I am also likely doing this in a more complex way then it should be.

Code:
#!/bin/bash

INPUTFILE="usernames"
users="$(gawk 'BEGIN {FS="\n" ; RS="\n"} {print $1}' $INPUTFILE)"
files="/eng/$users/.forward"
logfile="/root/forwardlog.txt"
path = "/eng/$users/"
# $i will hold single file at a time in following loop
echo "Checking for $files in $directories"

for file in $files
do
   if [ ! -e $file ]; then
   echo "File $file does not exist. Writing to Log."
   echo "Missing File $file" >> $logfile; echo
   continue
   else
   echo "File Exists, logging entry to file"
   echo "File $files exists for user ccrichix" >> $logfile
   echo "Entry for user $users is" cat $files >> $logfile
   continue
   fi
done
exit 0
If anyone can give me some pointers that would be great.
 
Old 01-07-2009, 03:26 PM   #2
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
If all of your users' home directories are in /eng, and if you wish to examine all of those directories, then you probably don't need to maintain a "usernames" file.
The continue statements are redundant -- harmless in this case, but unnecessary.
The path variable is unused in this script, so I left it out of this example.

Code:
#!/bin/bash

logfile="/root/forwardlog.txt"

for dir in `ls /eng`
do
   file=/eng/$dir/.forward
   if [ ! -e $file ]; then
     echo "File $file does not exist. Writing to Log."
     echo "Missing File $file" >> $logfile
   else
     echo "File Exists, logging entry to file"
     echo "Entry for user $dir is `cat $file`" >> $logfile
   fi
   echo
done
exit 0

Last edited by raconteur; 01-07-2009 at 03:43 PM. Reason: fix typos
 
Old 01-07-2009, 04:37 PM   #3
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 227Reputation: 227Reputation: 227
Quote:
If all of your users' home directories are in /eng, and if you wish to examine all of those directories, then you probably don't need to maintain a "usernames" file.
Very true. If however you do need to maintain the file and I take it your file is just line after line of user names then this would easily do the loop in you example:

Code:
for dir in $(cat $INPUTFILE)
saves all that gawk messing about.
 
Old 01-08-2009, 09:10 AM   #4
richinsc
Member
 
Registered: Mar 2007
Location: Utah
Distribution: Gentoo Linux (3.0.6-gentoo)
Posts: 222

Original Poster
Rep: Reputation: 32
The only problem with that is when I got to log the entry. And it doesn't matter what the path is it doesn't parse correctly what I mean is when I execute the file, regardless if the file exists it says that the file doesn't exist when it does.

This is what is inputted to screen
Code:
File user1 does not exist. Writing to Log.

File user2 does not exist. Writing to Log.

File user3 does not exist. Writing to Log.
It should say the full path /eng/user1/.forward does not exist

This is what is logged to file. Regardless if I use awk or cat, it dumps whole file into log as one entry... See Below. So isn't reading one by one line. It should substitute the user name into the variable full path.

The variable is this /eng/$user/.forward where $user is each of the user names.

Code:
Missing File /eng/user1
Missing File user2
Missing File user3/.forward
Each entry in log should say

Missing File /eng/user1/.forward
Missing File /eng/user2/.forward
etc...

The other thing I am trying to have it do is write the contents of to .forward to logfile as well. But until I get the above issue corrected I don't think I can get this to work because I trying to have it cat the file. But with the way it is running code doesn't help because it thinks it it trying to read /eng/user1 user2 user3/.forward
 
Old 01-08-2009, 06:41 PM   #5
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
As bgeddy said, the cat method works well if your list file contains one user name per line... it sounds like that may not be the case.

I've modified this script from above in a trivial manner to use that method and added quotes around the contents of the .forward file entry in the log.

Code:
#!/bin/bash

logfile="/root/forwardlog.txt"

INPUTFILE=/root/usernames

for name in $(cat $INPUTFILE)
do
   file=/eng/$name/.forward
   if [ ! -e $file ]; then
     echo "File $file does not exist. Writing to Log."
     echo "Missing File $file" >> $logfile
   else
     echo "File Exists, logging entry to file"
     echo "Entry for user $name is \"`cat $file`\"" >> $logfile
   fi
   echo
done
exit 0
I created this input file (named /root/usernames):
Code:
tom
dick
harry
maryjane
and made the appropriate directories and files.
The directory "maryjane" does not contain a .forward file, all of the rest have valid entries.

I get this output on the screen:
Code:
File Exists, logging entry to file

File Exists, logging entry to file

File Exists, logging entry to file

File /eng/maryjane/.forward does not exist. Writing to Log.
and this is the resulting log file:
Code:
Entry for user tom is "abuse@google.com"
Entry for user dick is "nobody@home.com"
Entry for user harry is "root@whitehouse.gov"
Missing File /eng/maryjane/.forward
About the only thing I can think of to make it more informative might be to list the successful file names on the console but they're logged so I didn't mess around with your original output.

Last edited by raconteur; 01-08-2009 at 06:43 PM.
 
Old 01-09-2009, 12:10 PM   #6
richinsc
Member
 
Registered: Mar 2007
Location: Utah
Distribution: Gentoo Linux (3.0.6-gentoo)
Posts: 222

Original Poster
Rep: Reputation: 32
raconteur,

All I can say is Thank You, Thank You... It worked perfectly. Since this was just a one time check, this is why user names were stored in a file. The modification worked marvelously. Kudos, now you can buy that tall alcoholic beverage or soda, whichever you prefer. If only there were a internet pub that would serve you drinks I would sure buy you one.

Thanks again.
 
Old 01-09-2009, 12:27 PM   #7
raconteur
Member
 
Registered: Dec 2007
Location: Slightly left of center
Distribution: slackware
Posts: 276
Blog Entries: 2

Rep: Reputation: 44
You're quite welcome, and thank you as well for your kindness.
 
  


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
Makefile:condition to check whether a file exists ? Ashok_mittal Linux - Newbie 4 12-06-2011 07:52 PM
[SOLVED] check whether a file exists or not ?? vikas027 Programming 5 06-05-2008 04:45 AM
bash check folder exists zerocool22 Programming 22 06-01-2008 07:53 AM
Shell script problem. check file already exists sinister1 Linux - Server 8 11-20-2007 03:13 PM
c++ check if file exists Genjix Programming 3 03-15-2004 12:08 AM


All times are GMT -5. The time now is 07:39 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration