Click to See Complete Forum and Search --> : shell scripting problem


Clutch1994
04-29-2001, 02:18 AM
I hope someone can help me with this, I would appreciate it.

I am trying to write a script that takes a user's name as input, and if the
username exists, the script will print out each of the 7 fields of the
/etc/passwd file for that user. It has to use positional parameters and has to
check to make sure the correct number of arguments exist. For example, if the
script is named userinfo, and the username is johnson, I am supposed to be able
to enter:

./userinfo johnson

And get:

username: johnson
password: x
User ID: 500
Group ID: 500
Comment Doug Johnson
Home Dir: /home/johnson
Shell: /bin/bash


Can anybody tell me how I go about doing this? Thank you.

clutch1994@aol.com :confused:

Antho
04-29-2001, 02:40 AM
Read a beginners tutorial in shell scripting , either the Adv-Bash-Programming-HOWTO available from the Linux Documentation Project or the shell scripting chapter in the latest release of "Rute Users Tutorial and Exposition" v0.8.8. Both of these helped me a great deal.

X_console
04-29-2001, 03:24 AM
#!/bin/bash

username=$(grep $1 /etc/passwd | awk -F: '{print $1}')
password=$(grep $1 /etc/passwd | awk -F: '{print $2}')
uid=$(grep $1 /etc/passwd | awk -F: '{print $3}')
gid=$(grep $1 /etc/passwd | awk -F: '{print $4}')
comment=$(grep $1 /etc/passwd | awk -F: '{print $5}')
home=$(grep $1 /etc/passwd | awk -F: '{print $6}')
shell=$(grep $1 /etc/passwd | awk -F: '{print $7}')

printf "Username: $username\n"
printf "Password: $password\n"
printf "User ID: $uid\n"
printf "Group ID: $gid\n"
printf "Comment: $comment\n"
printf "Home directory: $home\n"
printf "Shell: $shell\n"


There is a Shell Scripting tutorial in the NHF section: http://www.linuxnewbie.org/nhf/intel

Clutch1994
04-29-2001, 03:06 PM
Thank you for the help, I printed it out and I'm going to have to sit down and study it for a bit.