Why is my condition always false?

BlueFox

I tried to write a script which creates a user.

First it needs to check if:

  1. root is running
  2. if user maybe already exists

I Also tried to set the password equal to the username which was typed in.

Overall this works fine if i copy it step by step and execute it step by step. But the whole script won't work.

#!/bin/bash
if [ "$(id -u)" = "0" ]; then
    read -p "User: " username
    pass=$username

    if [ getent username>/dev/null 2>&1 ]; then
        echo "$username already exists"
        sleep 10
        exit 1
    else    
        useradd -m $username
        echo "$pass" | passwd $username --stdin
        [ $? -eq 0 ] && echo "User was created" || echo "Error while creating"
        unset username

    fi
else
    echo "No root"
    sleep 3
    exit 2  
fi
Gilles 'SO- stop being evil'

The [ command introduces a conditional; it is synonymous with test except for requiring a closing bracket at the end. Inside the brackets, you need a condition such as -n "$foo" to test if a variable is non-empty, -e foo to test if a file exists, etc.

[ getent username>/dev/null 2>&1 ] is equivalent to test getent username>/dev/null 2>&1. The conditional expression is not well-formed, so this produces an error message such as [: 1: getent: unexpected operator, which is redirected to /dev/null so you don't see it. The conditional command returns 0 if the condition is true, 1 if it's false and 2 if there is an error; here it returns 2, and if takes then then branch only if the command returns 0, so the if statement takes the else branch.

You can get an idea of what's going on by activating traces by putting set -x as the second line of your script. This will tell you that the [ getent username>/dev/null 2>&1 ] command is executed followed by the useradd -m $username command, but to see why [ getent username>/dev/null 2>&1 ] is failing, you have to remove the redirection.

Since you don't want to use a conditional expression here, but to test if getent "$username" succeeds (not getent username, by the way), leave off the brackets:

if getent "$username" >/dev/null 2>&1; then …

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why the if condition is always false?

From Dev

I want to know why my 'login logic' always false condition?

From Dev

Why is my if statement always false?

From Dev

Why is the first condition of "else if" statement always false?

From Dev

Why does my condition always evaluate as true?

From Dev

Why is my "if" condition (string comparison) always true?

From Dev

Why is my condition showing false results?

From Dev

condition always returns false

From Dev

if condition always returning False

From Dev

Why does my ACF custom field return always false or array?

From Java

Why is my ClaimsIdentity IsAuthenticated always false (for web api Authorize filter)?

From Dev

Why is my Receiver for charging status always returning false?

From Dev

Why is @first always false in my handlebars #each loop?

From Dev

Why if i disable my textBox to false, is always equal to true?

From Dev

Why does my getColor function always return false?

From Dev

If always false, why?

From Dev

My resultset is always returning false

From Dev

Why is isDrawerVisible always showing as false

From Dev

Why is typeid always returning false?

From Dev

Why is isDrawerVisible always showing as false

From Dev

MDX: IIf condition on the value of a dimension is always false

From Dev

Compiler: What if condition is always true / false

From Dev

(isset $_POST['submit']) condition is always false

From Dev

Tmux if-shell condition always false

From Dev

Always include children even if condition is false

From Dev

Why is this PHP if condition always true?

From Dev

Why is this PHP if condition always true?

From Dev

Why does my function always returns false, even if it finds return true before?

From Dev

CouchDB-Why my rerduce is always coming as false ? I am not able to reduce anything properly

Related Related

  1. 1

    Why the if condition is always false?

  2. 2

    I want to know why my 'login logic' always false condition?

  3. 3

    Why is my if statement always false?

  4. 4

    Why is the first condition of "else if" statement always false?

  5. 5

    Why does my condition always evaluate as true?

  6. 6

    Why is my "if" condition (string comparison) always true?

  7. 7

    Why is my condition showing false results?

  8. 8

    condition always returns false

  9. 9

    if condition always returning False

  10. 10

    Why does my ACF custom field return always false or array?

  11. 11

    Why is my ClaimsIdentity IsAuthenticated always false (for web api Authorize filter)?

  12. 12

    Why is my Receiver for charging status always returning false?

  13. 13

    Why is @first always false in my handlebars #each loop?

  14. 14

    Why if i disable my textBox to false, is always equal to true?

  15. 15

    Why does my getColor function always return false?

  16. 16

    If always false, why?

  17. 17

    My resultset is always returning false

  18. 18

    Why is isDrawerVisible always showing as false

  19. 19

    Why is typeid always returning false?

  20. 20

    Why is isDrawerVisible always showing as false

  21. 21

    MDX: IIf condition on the value of a dimension is always false

  22. 22

    Compiler: What if condition is always true / false

  23. 23

    (isset $_POST['submit']) condition is always false

  24. 24

    Tmux if-shell condition always false

  25. 25

    Always include children even if condition is false

  26. 26

    Why is this PHP if condition always true?

  27. 27

    Why is this PHP if condition always true?

  28. 28

    Why does my function always returns false, even if it finds return true before?

  29. 29

    CouchDB-Why my rerduce is always coming as false ? I am not able to reduce anything properly

HotTag

Archive