You are not logged in.
Pages: 1
Hey! I'm in the middle of writing a admin script witch can handle users and groups etc. My problem is this:
echo ""
echo "Please select a username for the new account: "
read username
echo "And now the password: "
stty_orig=`stty -g`
stty -echo
read password
stty $stty_orig
grepit='grep -o -m 1 ^$username /etc/passwd'
$grepit
if
[ "$grepit" = "$username" ]; then
echo "This username already exist, please try again"
This part doesn't work at all, maybe its me that is a retard but in my opinion and based on my knowledge it should work, but it doesn't. Any ideas?
Offline
Your if construct is wide open...
There is also no need to evaluate grep's output with another test clause. Grep will either find what it's looking for (0) or not (1) and you can use the exit code just fine:
grep bla $bla || echo "Oh my, we didn't find bla!"
Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy
Offline
Apart from what B mentioned, your error is here:
grepit='grep -o -m 1 ^$username /etc/passwd'
This simply assigns the literal string 'grep -o -m...' to the grepit variable.
What you meant was either
grepit=`grep -o -m 1 ^$username /etc/passwd`
or
grepit=$(grep -o -m 1 ^$username /etc/passwd)
Note that single quotes(') are different from backticks(`). Since they can easily be confused, the second syntax $(...) is usually preferred.
Last edited by hbekel (2009-10-10 17:12:31)
Offline
Hmm i'm not really good at this but i am willing to learn so here comes the whole code instead so you can explain a little more because i've tested your suggestions and it isn't working any better
select opt in $OPTIONS; do
if [ "$opt" = "Add-user" ]; then
echo ""
echo "Please select a username for the new account: "
read username
echo "And now the password: "
stty_orig=`stty -g`
stty -echo
read password
stty $stty_orig
$grepit=(grep -o -m 1 ^$username /etc/passwd)
fi
if
[ "$grepit" = "$username" ]; then
echo "This username already exist, please try again"
else
echo "Select a primary group for the account (the group must exist): "
read group
echo "Are you sure you want to add this user? (y/n)"
read yorn
fi
if [ "$yorn" = "y" ]; then
useradd -g $group -p $password $username
echo "$username added with the primary group $group."
echo ""
fi
Offline
This is wrong:
$grepit=(grep -o -m 1 ^$username /etc/passwd)
try
grepit=$(grep -o -m 1 ^$username /etc/passwd)
Offline
Thanks, now it's working silly me So if i wish to use this same function on the "add group" command is it the same grep command or do i need another?
Offline
Pages: 1