【shell脚本】 该脚本是用来新建一个目录,要传入一个参数,sh ifmkdir.sh 123 运行后 提示语法错误
脚本:#!/bin/shdd=$1if["$dd"=""];thenecho"youdidnotinputanything!"exit1fiif[-d$dd]then:e...
脚本:
#!/bin/sh
dd=$1
if [ "$dd" = "" ];then
echo "you did not input anything!"
exit 1
fi
if [ -d $dd ]
then :
else
echo "the directory is exist!"
echo -n "do you create it now [y..n]?"
read mm
if [ "$mm" = "y" ] || [ "$mm" = "Y" ]
then
echo " create it now!"
mkdir $dd
if [ $? != 0 ]
then
echo " it is not OK!"
exit 1
else :
fi
运行: sh ifmkdir.sh 123
运行错误: 展开
#!/bin/sh
dd=$1
if [ "$dd" = "" ];then
echo "you did not input anything!"
exit 1
fi
if [ -d $dd ]
then :
else
echo "the directory is exist!"
echo -n "do you create it now [y..n]?"
read mm
if [ "$mm" = "y" ] || [ "$mm" = "Y" ]
then
echo " create it now!"
mkdir $dd
if [ $? != 0 ]
then
echo " it is not OK!"
exit 1
else :
fi
运行: sh ifmkdir.sh 123
运行错误: 展开
1个回答
展开全部
修改了下,你看看是否可以运行了:
#!/bin/sh
dd=$1
if [ "$dd" = "" ];then
echo "you did not input anything!"
exit 1
fi
if [ -d $dd ]
then
echo "the directory is exist!" <-- 这句按语义该在if分支
exit 1 <-- 这边加个exit好点
else
echo -n "do you create it now [y..n]?"
fi <-- 这边少加了fi,初学者常见错误
read mm
if [ "$mm" = "y" ] || [ "$mm" = "Y" ]
then
echo " create it now!"
mkdir $dd
fi <-- 同上
if [ $? != 0 ]
then
echo " it is not OK!"
exit 1
else
:
fi
#!/bin/sh
dd=$1
if [ "$dd" = "" ];then
echo "you did not input anything!"
exit 1
fi
if [ -d $dd ]
then
echo "the directory is exist!" <-- 这句按语义该在if分支
exit 1 <-- 这边加个exit好点
else
echo -n "do you create it now [y..n]?"
fi <-- 这边少加了fi,初学者常见错误
read mm
if [ "$mm" = "y" ] || [ "$mm" = "Y" ]
then
echo " create it now!"
mkdir $dd
fi <-- 同上
if [ $? != 0 ]
then
echo " it is not OK!"
exit 1
else
:
fi
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询