-1

I am trying to check if 2 variables are empty or not defined at the same time in bash. If that is the case, no user password will be changed.

#!/bin/bash
while true
do
    read -s  -p "Enter root password: " rootpass1
    echo
    read -s  -p "Enter root password again: " rootpass2
    echo
    if  [[-z "$rootpass1"] && [-z "$rootpass2"]]
    then
         echo "Password will not be changed"
         break
    else
        if [ $rootpass1 != $rootpass2 ]
        then
            echo "Passwords are not identical"
        else
            echo "user:$rootpass1" | chpasswd
            break
        fi
    fi
done

But I am getting the following error:

script.sh: line 8: [: missing `]'

Any clue?

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
FedeKrum
  • 435
  • 1
  • 6
  • 15
  • 1
    It is a terrible idea to write your own version of the [passwd command](http://linux.die.net/man/1/passwd). Especially since even after debugging it is still broken [because you are still using](http://stackoverflow.com/a/32297965/282912) `if [ $rootpass1 != $rootpass2 ]` – msw Aug 30 '15 at 16:32

3 Answers3

2

You need double brackets around both tests, like this:

if  [[ -z "$rootpass1" ]] && [[ -z "$rootpass2" ]]
Steve Vinoski
  • 19,847
  • 3
  • 31
  • 46
1

How about

#!/bin/bash
read -s  -p "Enter root password: " rootpass1
echo
read -s  -p "Enter root password again: " rootpass2
echo

if  [[ -z "$rootpass1" && -z "$rootpass2" ]]
then
    echo "Password will not be changed"
else
    if [[ "$rootpass1" != "$rootpass2" ]]
    then
        echo "Passwords are not identical"
    else
        echo "user:$rootpass1" | chpasswd
    fi
fi

Note that the whitespace around [[ and ]] is important. I also think || would be just a little bit better for your first test: don't do anything if either of the passwords is empty (since either they are both empty, or they are not identical, so you spare some effort).

  • Sorry !! I am new and learning !!! Did what you told me and posted my very own answer below !!! Thanks – FedeKrum Aug 30 '15 at 15:14
  • 1
    @FedeKrum it's OK, you'll get to grips with it in time:) You just misunderstood me, I tried to explain again below your answer. Eventually we should have: 1. your question, edited to contain the final solution **as well as the original question**, 2. the answers by Steve and me, 3. none of our chatty comments:) I'll remove mine, like this one, when we reach a final conclusion. – Andras Deak -- Слава Україні Aug 30 '15 at 15:17
  • @AndrasDeak No, that's wrong. The question should contain just the question. The accepted answer should be more or less what the OP ended up using; sometimes the OP will accept one of the answers but still post one of her/his own just to clarify how it was adapted. – tripleee Aug 31 '15 at 17:33
  • @tripleee, I totally agree, but OP seems adamant on including the final solution one way or the other... My proposed solution is already a compromise. (Their original solution was changing the question to include only the final code...) – Andras Deak -- Слава Україні Aug 31 '15 at 17:44
0

I corrected the script with the sugestions and it did work !!! Thanks !!! I will give points for that when I get able to do that.

#!/bin/bash
while true
do
    read -s  -p "Enter admin password: " rootpass1
    echo
    read -s  -p "Enter admin password again: " rootpass2
    echo
    if  [[ -z "$rootpass1" ]] && [[ -z "$rootpass2" ]]
    then
        echo "Password will not be changed. Both are empty."
        echo
        break
    else
        if [[ $rootpass1 != $rootpass2 ]]
        then
                echo "Passwords are not identical. Try again."
                echo
        else
                echo "root:$rootpass1" | chpasswd
                echo "Password changed."
                echo
                break
        fi
    fi
done
FedeKrum
  • 435
  • 1
  • 6
  • 15
  • Getting better. You don't need to add an answer of your own as I have already done so:) But if you want to include the final solution still, then edit your original question and add the final version of the working code, with a proper separation from the original question. You should put a meaningful header in between. – Andras Deak -- Слава Україні Aug 30 '15 at 15:13
  • 2
    This still lacks proper quoting in the second `if`. See also http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-variable – tripleee Aug 31 '15 at 17:35
  • 2
    For consistency, you probably want to use `[[` everywhere, instead of mixing `[` and `[[`. – tripleee Aug 31 '15 at 17:35