当前位置: 技术问答>linux和unix
有关目录操作的问题
来源: 互联网 发布时间:2015-01-26
本文导语: 小弟写了一段shell代码用于检测目录: #!/bin/sh #ifmkdir #parameter is passed as $1 but reassigned to DIRECTORY DIRECTORY=$1 #is the string empty?? if [ "$DIRECTORY" = "" ]; then echo "Usage:$0 directory to create " >&2 exit 1 fi if [ -d...
小弟写了一段shell代码用于检测目录:
#!/bin/sh
#ifmkdir
#parameter is passed as $1 but reassigned to DIRECTORY
DIRECTORY=$1
#is the string empty??
if [ "$DIRECTORY" = "" ];
then
echo "Usage:$0 directory to create " >&2
exit 1
fi
if [ -d $DIRECTORY ];
then:#do nothing
else
echo "The direcotry does exist"
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 "ERROR creating the directory $DIRECTORY" >&2
exit 1
fi
else:#do nothing
fi
fi
可是老是出0403-057 Syntax error at line 15 : `else' is not expected.
这个错误请各位大哥大姐帮忙看看是哪里的错误,谢谢了!
#!/bin/sh
#ifmkdir
#parameter is passed as $1 but reassigned to DIRECTORY
DIRECTORY=$1
#is the string empty??
if [ "$DIRECTORY" = "" ];
then
echo "Usage:$0 directory to create " >&2
exit 1
fi
if [ -d $DIRECTORY ];
then:#do nothing
else
echo "The direcotry does exist"
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 "ERROR creating the directory $DIRECTORY" >&2
exit 1
fi
else:#do nothing
fi
fi
可是老是出0403-057 Syntax error at line 15 : `else' is not expected.
这个错误请各位大哥大姐帮忙看看是哪里的错误,谢谢了!
|
主要是then和else后面的格式不对,我给你修改了程序,已经顺利运行。附在后面,供你参考。
#parameter is passed as $1 but reassigned to DIRECTORY
DIRECTORY=$1
#is the string empty??
if [ "$DIRECTORY" = "" ];
then
echo "Usage:$0 directory to create " >&2
exit 1
fi
if [ -d $DIRECTORY ];
then
echo "$DIRECTORY already exist"
exit 0
else
echo "The direcotry does not exist"
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 "ERROR creating the directory $DIRECTORY" >&2
exit 1
fi
else
echo "exit"
fi
fi
#parameter is passed as $1 but reassigned to DIRECTORY
DIRECTORY=$1
#is the string empty??
if [ "$DIRECTORY" = "" ];
then
echo "Usage:$0 directory to create " >&2
exit 1
fi
if [ -d $DIRECTORY ];
then
echo "$DIRECTORY already exist"
exit 0
else
echo "The direcotry does not exist"
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 "ERROR creating the directory $DIRECTORY" >&2
exit 1
fi
else
echo "exit"
fi
fi
|
#!/bin/sh
#parameter is passed as $1 but reassigned to DIRECTORY
DIRECTORY=$1
#is the string empty??
if [ "$DIRECTORY" = "" ]
then
echo "Usage:$0 directory to create " >&2
exit 1
fi
echo "The direcotry does exist"
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 "ERROR creating the directory $DIRECTORY" >&2
exit 1
fi
fi