You are not logged in.

#1 2009-10-10 16:12:37

Afnaf
Member
From: Sweden
Registered: 2009-07-29
Posts: 27
Website

Another Bash question.

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

#2 2009-10-10 16:25:40

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: Another Bash question.

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

#3 2009-10-10 17:11:30

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: Another Bash question.

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

#4 2009-10-10 17:18:24

Afnaf
Member
From: Sweden
Registered: 2009-07-29
Posts: 27
Website

Re: Another Bash question.

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 sad

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

#5 2009-10-10 17:23:21

hbekel
Member
Registered: 2008-10-04
Posts: 311

Re: Another Bash question.

This is wrong:

$grepit=(grep -o -m 1 ^$username /etc/passwd)

try

grepit=$(grep -o -m 1 ^$username /etc/passwd)

Offline

#6 2009-10-10 17:29:29

Afnaf
Member
From: Sweden
Registered: 2009-07-29
Posts: 27
Website

Re: Another Bash question.

Thanks, now it's working silly me smile 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

Board footer

Powered by FluxBB