现在,我已经知道这意味着有一个错误,但我无法找到它.你能帮忙检查我的代码并试着找出问题所在吗?
错误消息围绕我创建的日期函数.所有其他功能在此代码中正常工作.
错误:
sguthrie1@cs:~$./finalproject.sh -d
Segmentation fault (core dumped)
码:
function check
{
echo "usage: hw14.sh option argument
Please enter one or more options or arguments."
exit
}
function date
{
if [[ $myvar == "-d" ]]
then date "+%d %B,%Y"
fi
}
function host
{
if [[ $myvar == "-h" ]]
then hostname
fi
}
function who
{
if [[ $myvar == "-w" ]]
then whoami
fi
}
function help
{
if [[ $myvar == "-help" ]]
then echo "
valid options:
-d = display today's date in day-month-year format
-h = display name of computer you are currently working on
-w = display who you are logged in as
arguments:
Any argument entered is checked to see if it is a file name
"
fi
}
if [ $# -le 0 ]
then check
fi
for myvar
do
if [[ $myvar == "-"* ]]
then date; host; who; help
fi
done
解决方法:
日期函数以递归方式调用自身,没有终止条件.这将是Bash中的probably always segfault.使用命令日期来调用date命令而不是函数.在bash 4.2中,您还可以通过设置FUNCNEST变量来设置递归深度限制,以帮助检测此类错误.