if I have
if [ ! -e $dir ];
then
mkdir $dir
fi
work, but not
[[ ! -e $dir ]] || mkdir $dir
why ?
Edit 0
with [[ ... I get
line 34: [[ !: command not found
Edit 1
bash --version
GNU bash, version 3.2.25(1)-release (i686-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
Edit 2
in some case work and some case don’t work, two consecutives commands
[user@host ~]$ [ -e /tmp ] && date
-bash: [: missing `]'
[user@host ~]$ [ -e /tmp ] && date
mar jun 26 10:05:50 CLT 2012
Especially after edit 2, this really looks to me like a problem with “funny” characters, either nonprinting characters getting mixed in, or normal-looking-but-weird characters like nonbreaking spaces. These can be fairly hard to detect; things can look completely normal in an editor, and even if you view a script with something like
cat -vit won’t always show funny characters clearly. If you havexxdon your system, it’s a really good way to see precisely what’s in the file. Here’s a quick demo of this type of problem:The script looks completely normal with
cat -v(andmore,vi, etc), but the first two commands fail.xxdshows why: the first command has a UTF-8 nonbreaking space between the[and the-e(this shows as c2a0 in the hex listing,[..-ein the text listing) and the second command has a nonbreaking space between/tmpand](/tmp..]in the text listing).The
-xdisplay (I usedbash -xto invoke it, you can also useset -xas @CodeGnome suggested) also gives a hint about what’s going on. For the first command, it listed it as'[ -e' /tmp ']'— note the quotes around[ -e, which indicates that the shell is treating that all as one “word”, which means it doesn’t think that’s a space in the middle of it. Similarly, the second command is displayed as'[' -e '/tmp ]'with the quotes indicating that it thinks/tmp ]is all one “word”.