a script with prompts user and returns the value of there home directory and full name
#!/bin/bash
echo "please enter your login ID"
read login_id
while $login_id -ne `grep $login_id /etc/passwd | cut -f1 -d:`
is they anything wrong with it
a script with prompts user and returns the value of there home directory and full name
#!/bin/bash
echo "please enter your login ID"
read login_id
while $login_id -ne `grep $login_id /etc/passwd | cut -f1 -d:`
is they anything wrong with it
I would do it with a grep of the login_id in /etc/password
grep $login_id | awk '{print $5, $6}'
sorry forgot
cat /etc/passwd (in the beginniing)
gnom: Useless Use of cat and grep
awk -F: -v id="$login_id" '$1==id {print $5,$6}' /etc/passwd
Apart from the useless use of grep and cut, I guess there is something wrong with your expression as you're comparing strings and test operator should be for strings is "!=" instead of "-ne". Correct me guys if I am wrong as I am still a newbie.
The main problem, though, is that your while loop is incomplete, and also doesn't really appear to have a purpose. I guess you are trying to check whether the user input is a valid login ID according to /etc/passwd?
#!/bin/bash
echo "please enter your login ID"
read login_id
if grep "^$login_id:" /etc/passwd >/dev/null
then
echo You guessed right
else
echo Wrong >&2
exit 127
fi
In the grep, I added the ^ and : to only search the first colon-delimited field; if a match is found, grep will print it and return true (so the "if" succeeds, and the "then" branch is taken); otherwise, it will print nothing, and return false (so we go into the "else" branch).
Because we don't really want to see the output from grep when there is a match -- we only do it for the return code -- the output is redirected to /dev/null.
The double quotes around the regular expression are important; otherwise the script will break if the user types something unexpected (like anything with a space in it).
Of course, you cannot be sure whose account name was typed in; if you want the user's own account name, that should be available in $LOGNAME
The correct syntax for a while loop is
while command
do
commands
done
If you leave out do or done, you get a syntax error. (Well, if you leave out command or commands, too.) This will run command and -- like if -- check its result code; if it is true, then commands are executed, and the loop returns back to the top, and does the same thing again, until command fails to return a true exit code.