simple script
I've used basic linux for a while, really basic. Now I need to write a simple script that will list all my users password status to a file.
I have a list of users (userlist.txt) and need to read that file and do chage -l user. If you could tell me the steps are doing, it would help me to try other scripts. I'm using bash shell. Thanks, Karen |
Quote:
Code:
~ $ chage Code:
chage -l < '/path/to/userlist.txt' < means following string is your input file path Quotes on the file path are necessary only if it includes embedded whitespace (spaces, tabs, newlines), and may be single or double in this case /path/to/ is the path to the directory containing the file of user names, not needed if that is the current directory And it would show out put like this Code:
~ $ chage -l g To send the output to a file instead of printing it on the console, instead use the command as Code:
chage -l < '/path/to/userlist.txt' > '/path/to/chageoutput.txt' |
I did as you suggested. But received the following results. Maybe I'm missing something?
# chage -l < '/tmp/userlist.txt > '/tmp/output.txt' > > > |
You're missing a single quote after userlist.txt
The > prompt is the shell waiting for you to finish the command. It has only encountered three quotes so far, it's expecting a fourth. You can use Ctrl+C to cancel the command, then you can fix it and run it again. |
Sorry, missed that, but this is what I had when I first tried feeding the file in and then outputting.
chage -l < '/users/userlist.txt' > '/tmp/results.txt' Usage: chage [-l] [-m min_days] [-M max_days] [-W warn] [-I inactive] [-E expire] [-d last_day] user # |
Yeah it looks like chage doesn't like that syntax. Try this
Code:
while read user; do echo $user; chage -l $user; done < /users/userlist.txt > /tmp/results.txt Code:
while read user; do |
This is giving the results that prompted me to post this question. But I do appreciate the help. The data in the results.txt file is the same listing that is in the userlist.txt. So it's just outputting the file it read.
# while read user; do echo $user; chage -l $user; done < /users/userlist.txt > /tmp/results.txt chage: unknown user: abennet chage: unknown user: gwhite chage: unknown user: jallan chage: unknown user: csmith # |
It's just outputting the same file it read because apparently none of the users exist on the system, so all you get is the "echo $user" with blank output from the chage -l.
This is how the output should look, if the users actually exist: Code:
# cat users.txt |
I must be doing something incorrectly. I have over 150 users, all exist in the /users/ directory. If I manually do chage -l user1, I get the correct output. But if user1 is in the list /users/userlist.txt, it doesn't output anything, except the name of the user. I've checked for spaces, and there aren't any. I could see one or two typos' in the list, but not all of them.
|
try
Quote:
|
Quote:
|
To see if the user names from the file are as expected, what is the output from
Code:
while read -r user; do echo ">$user<"; done < /users/userlist.txt |
Try this approach
Code:
~ $ cat fred.txt y=${#x[@]} gets the number of cells in array x The 'for' loop delivers the content of each cell in array x to chage, including the linebreak. That is, it simulates exactly what happens when you enter the username at the console, and press Return. Mine is a single user machine, so /etc/passwd has only my user entry in it, hence the rejection of Root and q Following is the 'for' loop rewritten to send chage's output to a file, o.txt. >> is used because we need each user output appended to the file Code:
~ $ for ((i=0; i<y; i++)); do chage -l ${x[$i]} >> o.txt; done And as chage doesn't bother to tell you the user name its dealing with, you might want to make the 'for' loop something like Code:
for ((i=0; i<y; i++)); do chage -l ${x[$i]} >> o.txt; echo "Above Entry for User ${x[$i]}"; done |
I copied and pasted your exact line and received
<abennet <gwhite <jallan <csmith It seems so simple, which is why I'm so stumped. I feel like I'm just missing some obvious typo. |
Hmmm, it is a bash shell, but no mapfile found. I'm running Red Hat Enterprise Linux ES release 4 (Nahant Update 7)
While this hasn't solved my problem, I really appreciate the help and this has given me a lot more to work with. Thanks. |
All times are GMT -5. The time now is 07:11 AM. |