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 |
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 |
Quote:
Code:
for dir in $(cat $INPUTFILE) |
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. 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/.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 |
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 Code:
tom 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 Code:
Entry for user tom is "abuse@google.com" |
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. |
You're quite welcome, and thank you as well for your kindness.
|
All times are GMT -5. The time now is 03:48 AM. |