分段故障在UNIX shell脚本(核心转储)错误。帮助找到错误? [英] Segmentation fault (core dumped) error in unix shell script. Help finding bug?

查看:472
本文介绍了分段故障在UNIX shell脚本(核心转储)错误。帮助找到错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我已经知道,这意味着有一个bug,但我不能找到它。你能不能帮我复习code和尝试发现什么是错的?
我周围的创建日期函数的错误消息围绕。所有其它功能在这一code正常工作。

错误:

  sguthrie1 @ CS:〜$ ./finalproject.sh -d
分段错误(核心转储)

code:

 功能检查
{
        回声用法:hw14.sh选项参数
请输入一个或多个选项或参数。
        出口
}
功能日期
{
        如果[[$ myvar的==-d]]
        然后日期+%D%B,%Y
        科幻
}
功能主机
{
        如果[[$ myvar的==-h]]
        然后主机
        科幻
}
功能谁
{
        如果[[$ myvar的==-w]]
        然后WHOAMI
        科幻
}
功能帮助
{
        如果[[$ myvar的==-help]]
        然后回声
有效的选项:
在日 - 月 - 年的格式-d =显示今天的日期
计算机-h =显示名称您目前正在
-w =显示你是谁,身份登录
参数:
输入的任何参数进行检查,看它是否是一个文件名

        科幻
}
如果[$#-le 0]
然后检查
科幻
对于MYVAR

        如果[[$ myvar的== - *]
        然后日期;主办;谁;帮帮我
        科幻
DONE


解决方案

日期函数是递归没有终止条件自称。这将在可能总是出现段错误。使用命令日期来调用date命令,而不是功能。在bash 4.2,您还可以通过设置 FUNCNEST 变量来帮助检测这类错误设定递归深度限制。

Now, I already know that this means that there is a bug, But I cant find it. Could you help review my code and try to spot what is wrong? The error message revolves around the date function I created. All the other functions work fine in this code.

Error:

sguthrie1@cs:~$ ./finalproject.sh -d 
Segmentation fault (core dumped)

Code:

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

解决方案

The date function is calling itself recursively with no termination condition. This will probably always segfault in Bash. Use command date to call the date command instead of the function. In bash 4.2 you can also set a recursion depth limit by setting the FUNCNEST variable to help detect such errors.

这篇关于分段故障在UNIX shell脚本(核心转储)错误。帮助找到错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆