LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-05-2012, 09:59 AM   #1
Crimsonlegion
LQ Newbie
 
Registered: Dec 2012
Posts: 2

Rep: Reputation: Disabled
script for searching valid users?


I have a script that the purpose of it is to prompt the user for user account names, then it searches the /etc/passwd file to see if that name exists. if the name exists it echos the name and passwd file entry. if the name doesnt exist, it should log the name into a variable(noaccnts) that is used when a end of file signal is sent by the user. it should then echo the non-existent accounts that the user entered during the loop.

Quote:
#! /bin/bash
clear
# if [[ $EUID -ne 0 ]]; then
# echo "You must be Root.";
# exit 1;
#fi

declare noaccnts=""

#if [ -f /etc/passwd ]; then
# echo "password file exists."
#else
# echo "password file doesn't exist."
#exit 1;
#fi

counter=0
while true
do

echo "please enter username: (Press Ctrl-D to end)" read username
echo " "

array=($(grep $username /etc/passwd | cut -d: -f1))

if [[ ${array[0]} = $username ]]; then
echo "$username is a valid user."
grep "^$username" /etc/passwd

elif [[ ${array[0]} != $username ]]; then
echo "$username is not a valid user."
noaccnts=$noaccnts", "$username

elif [ echo "$username" | grep -q '^$' ] ; then
break
echo ${noaccounts}

fi
counter=`expr ${counter} + 1`
done
the point I am trying to get at is the script as it is so far does the above things, except for storing the invalid accounts into a variable and the loop ending with the user either using the end of file signal(ctrl-D).

does anyone know how to make a loop for that or any pointers for this script?
 
Old 12-05-2012, 05:56 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
It's due to your loop type. There should be a few threads in the /Programming forum explaining it.

*BTW if the system has 'getent' you can store output of 'getent passwd somestring' and save making it presentable for when you exit:
Code:
resolvAcct() { RES[${1}]=$(getent passwd $INPUT >/dev/null 2>&1||echo $INPUT); }
NUM=1; unset RES; while [ $NUM -le 3 ]; do echo "Which account?"; 
read INPUT; resolvAcct ${NUM} INPUT; ((NUM++)); done; echo ${RES[@]}

Last edited by unSpawn; 12-05-2012 at 06:00 PM. Reason: //Less *is* more
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Which are valid Linux Users? b10m3ch4 Linux - Server 3 01-09-2010 09:37 PM
Samba user coneining space in valid users neo571 Linux - Networking 1 10-01-2009 04:18 AM
Samba - If I use @valid users, only root can log in essdeeay Linux - General 6 04-10-2006 01:05 PM
SMB.Conf --> valid users = %G???? Dmjmusser Linux - Networking 0 01-20-2006 04:50 PM
Enabling valid users to relay thru SMTP learning_slowly Linux - Networking 1 11-04-2002 04:07 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:53 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