Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

This is my code:

#!/bin/bash
if [[ -d ~/viwiki ]]; then
 cd ~/viwiki
else
 mkdir ~/viwiki
 cd ~/viwiki
fi
if ! [[ -d ./log ]]; then
 mkdir log
 mkdir log/log
 mkdir "log/wget"
elif ! [[ -d ./log/log ]]; then
 mkdir log/log
elif ! [[ -d "./log/wget" ]]; then
 mkdir "log/wget"
fi

When running, it has errors:

tuankiet65@UbuntuPC:~$ sh viwik/test2.sh
viwik/test2.sh: 2: viwik/test2.sh: [[: not found
viwik/test2.sh: 8: viwik/test2.sh: [[: not found

How can I fix this?

share|improve this question
4  
Maybe the Bash version is too old? –  Daniel Beck Feb 14 '13 at 10:56
    
I don't think so because I am running Ubuntu 12.10 –  Hồ Tuấn Kiệt Feb 14 '13 at 11:03
    
[[ is ancient, added bash-2.02, released April 1998. –  mr.spuratic Feb 14 '13 at 11:54
    
not even using bash; using sh. run scripts using ./{script} (after chmod u+x), not sh {script} –  michael_n Apr 13 '13 at 23:22
    
Old question, I know, but the entire code above could be replaced by the following mkdir command: mkdir -p ~/viwiki/log/{log,wget} –  Dennis Mar 7 at 2:15

1 Answer 1

up vote 8 down vote accepted

I'm guessing that if you run

readlink -f $(which sh)

you will not get Bash as return value, but Dash. You have the correct preamble, but that only matters if you run the script as ./test2.sh after making it executable.

Right now you force-run the script via the sh interpreter, which probably is Dash, and the [[]] construct is a Bash specific one.

That's "why?". If you just replace the double brackets with single ones (and change #!/bin/bash to #!/bin/sh, since your script is now only using POSIX functions anyway) it should run as intended.


Demonstration on Debian, with test.sh with contents:

#!/bin/bash
if [[ "string" == "string" ]]; then
    echo This is Bash
fi

this happens:

$ readlink -f $(which sh)
/bin/dash
$ sh test.sh 
test.sh: 2: test.sh: [[: not found
$ bash test.sh 
This is Bash
$ chmod 755 test.sh
$ ./test.sh
This is Bash
share|improve this answer
    
Thanks :) It fix my problem. I will learn more thing from this :) –  Hồ Tuấn Kiệt Feb 14 '13 at 11:15
    
@HồTuấnKiệt: The Ubuntu wiki is a good resource on differences between Dash and Bash, with rationale on why the change was made. –  Daniel Andersson Feb 14 '13 at 11:21

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.