Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I would like to make a script that deletes a directory with rmdir after confirming with a password using read to set the variable.

So far I have this:

#!/bin/bash -x
echo "Password:"
read -t 30 S1
    S2='55555'
if [ $S1=$S2 ]; then
    rmdir /home/william/test
else
    echo "fail"
sleep 10
fi

So, I have the -x to try to debug it but every time the script either fails to echo (if I put the password in wrong) or it wont remove the directory needed.

If someone has a modifiable script that I could use or if you could point out the problems with the current script that would be great.

share|improve this question

2 Answers 2

The right way to compare those two strings (S1 and S2) using if is:

if [ "$S1" = "$S2" ]

Do not be stingy in use spaces in this case.

See: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html

share|improve this answer
if [ "$S1" == "$S2" ]; then
  ...
fi

single "=" sets
double "==" compares
share|improve this answer
1  
Nonsense. This is not C, this is bash. –  muru Oct 28 at 16:38
    
More precisely this is bash's internal override of the program [ alias test(1). –  David Foerster Oct 28 at 16:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.