红联Linux门户
Linux帮助

Solaris下shell问题

发布时间:2010-11-25 15:38:21来源:红联作者:meycine
在Solaris创建目录的shell脚本:
#!/bin/sh
#ifmkdir
#parameter is passed as $1 but reassigned to DIRECTORY
DIRECTORY=$1
#is the string empty??
if [ "$DIRECTORY" = "" ]
then
echo "Usage: `basename $0` directory to create" >&2
exit 1
fi

if[ ! -d $DIRECTORY ]
then
echo "The directory does't exist" #14行
echo -n "Create it now? [y..n] :"
read ANS
if [ "$ANS" = "y" || "$ANS" = "Y" ]
then
echo "creating now"
#create directory and send all output to /dev/null
mkdir $DIRECTORY >/dev/null 2>&1
if [ $? != 0 ]
then
echo "Errors creating the directory $DIRECTORY" >&2
exit 1
fi
fi
fi

执行该脚本:./ifmkdir.sh kkk
报错如下:
./ifmkdir.sh: if[: 没找到
./ifmkdir.sh: 语法错误在14行:`then' 非期望的

请各位大侠帮帮忙,我刚学习shell,希望大家帮忙指点一下,谢谢!!!
文章评论

共有 4 条评论

  1. mg271603433 于 2011-01-05 18:33:18发表:

    顶一个。

  2. meycine 于 2010-11-25 16:39:05发表:

    问题在if[ ! -d $DIRECTORY ],if后面少了一个空格,正确的写法应该是if [ ! -d $DIRECTORY ]
    ,和/bin/sh无关,谢谢shenhao0129的提示!!

  3. meycine 于 2010-11-25 15:58:24发表:

    将/bin/sh修改成/bin/bash后,又出现了新的问题:
    bash-3.00$ ./ifmkdir.sh tt
    ./ifmkdir.sh: line 12: if[ ! -d tt ]: command not found
    The directory does exist
    Create it now? [y..n] :y
    creating now

    这是ls发现tt目录已经被创建,但是为什么会有./ifmkdir.sh: line 12: if[ ! -d tt ]: command not found这样的提示呢,这类的if应该怎么写啊,请各位大侠帮忙指点一下,谢谢!!

  4. meycine 于 2010-11-25 15:53:25发表:

    看了一些其他关于shell的帖子,发现有的大侠说把#!/bin/sh改成#!/bin/bash,按照这种方式修改后则不再出现上面提示的错误。
    那么在写shell时,怎样确定应该用/bin/sh还是/bin/bash呢??