【发布时间】:2016-03-18 16:29:22
【问题描述】:
您好,我正在 bash 中创建一个归档系统,其中包含一个冗长且凌乱的 if 语句,该语句当前不适用于 else if 语句的最后几行中的当前错误。尽管我预计此声明仍然存在进一步的错误。变量选项来自终端中显示的菜单。因此,如果他们选择选项 1,则用户必须输入要写入 crontab 文件的数据。
if [ $choice -eq "1" ] then
echo "Enter the MINUTE"
read minute
if [ $minute -eq 0 ] then
GLOBIGNORE=*
minute="*"
echo minute
fi
echo "Enter the Hour"
read hour
if [ $hour -eq 0 ] then
GLOBIGNORE=*
hour="*"
echo hour
fi
echo "Enter the Day"
read day
if [ $day -eq 0 ] then
GLOBIGNORE=*
day="*"
echo day
fi
echo "Enter the Month"
read month
if [ $month -eq 0 ] then
GLOBIGNORE=*
month="*"
echo month
fi
echo "Enter the weekday"
read weekday
if [ $weekday -eq 0 ] then
GLOBIGNORE=*
weekday="*"
echo weekday
fi
echo $minute $hour $day $month $weekday " date > ~/testcron.log" > testcron.txt fi
elif [ $choice -eq "2" ]
then
echo "Enter the Source and Destination Locations"
fi
【问题讨论】:
-
请修正缩进。它应该可以帮助您识别代码的结构,例如
fi属于哪个if。 -
发布整个声明(我看到多个
if``s but only onefi`)。 -
bash 不是 python。缩进不影响操作顺序。您的所有 if 语句都没有终止
fi,因此实际上您只有一系列深度嵌套的if语句。 -
已更新以包含 fi 语句,对于底部的格式问题表示歉意,由于某种原因,代码无法正确粘贴
-
在编写 crontab 文件后,您在
fi之前缺少换行符,这是您的代码中的问题还是这里的复制/粘贴错误?另外,对于基本的语法检查,请在打扰人类之前尝试shellcheck.net。
标签: bash shell unix ubuntu if-statement