LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 04-29-2001, 07:36 PM   #1
delthrin
LQ Newbie
 
Registered: Apr 2001
Posts: 2

Rep: Reputation: 0
Question


how would i do this using vi in the bash shell, suing "if then"?? I'm completely lost

Ok, I want you to write a script that will take a user name as input and if
the user name exist, print out each of the 7 fields used to represent the
user's account. Have a brief description of each field. The script must
include comments that explain how the script works. The script must use
positional parameters and check to make sure the correct number of arguments
exist.

./userinfo coleman

user name: coleman
password: x
User ID: 500
Group ID: 500
Comment: Chris Coleman
Home Dir: /home/coleman
Shell: /bin/bash

 
Old 04-29-2001, 09:24 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 121Reputation: 121
The post is screaming school assignment, but it was so easy that I found it difficult to provide examples of what you will need to check for while trying not to write the script for you. Here is my poor attempt:

Code:
#!/bin/sh

PASSWORD_FILE="/etc/passwd"

if [ $# -eq 1 ]; then
   USER_NAME="$1"
   REGX_USER_NAME="^$USER_NAME:"
   PASS_ENTRY=`cat $PASSWORD_FILE | grep $REGX_USER_NAME`

   if [ $? -eq 0 ]; then
      # ...
      USER_ID=`echo $PASS_ENTRY | cut -d':' -f3`
      # ...

   else
      echo "Username: $USER_NAME not found"
   fi

else
   echo "Usage: ScriptName username"
fi

exit
[*]$# is the number of parameters passed into the script[*]$1 is the first parameter[*]The regular expression was needed to make sure that grep only found the username at the begining of the line.[*]$? is the result from the last command run (grep). If it's zero then grep did find a match.[*]Read the man page for cut, it's simple.[*]Remember that you can use bash -x scriptname to debug the script.

 
  


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



LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:36 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration