LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash Help: Check if file exists (https://www.linuxquestions.org/questions/programming-9/bash-help-check-if-file-exists-695654/)

richinsc 01-07-2009 02:15 PM

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.

raconteur 01-07-2009 03:26 PM

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


bgeddy 01-07-2009 04:37 PM

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.

richinsc 01-08-2009 09:10 AM

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

raconteur 01-08-2009 06:41 PM

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.

richinsc 01-09-2009 12:10 PM

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.

raconteur 01-09-2009 12:27 PM

You're quite welcome, and thank you as well for your kindness.


All times are GMT -5. The time now is 03:48 AM.