Shell:整数表达式预期错误

时间:2016-06-01 14:21:40

标签: shell unix syntax-error

我是Unix环境编程的初学者,我在开始时遇到了一些困难。我在Windows上使用PUTTY。  这是我的脚本和代码,但是当我运行它时,它告诉我

  

预期的整数表达式

function standardAjaxRequest(process_script_path, redirect = false) {

    var loading = $(".loading");
    var response_message = $(".response_message");

    // runs the ajax to add the entry submitted
    form.on("submit", function(event) {
        event.preventDefault();

        removeNoticeError();

        var data = form.serialize();

        $.ajax({
            url:      process_script_path,
            type:     "POST",
            dataType: "json",
            data:     data,
            cache:    false,
            success: function(response) {
                if(response.status === false)
                {
                    response_message.addClass("error").text(response.message).fadeIn(1);
                }
                else
                {
                    response_message.addClass("notice").text(response.message).fadeIn(1);
                    if(redirect)
                    {
                        setTimeout(function () {
                            window.location.reload();
                        }, 1000);
                    }
                    else
                    {
                        response_content.after(response.content);
                    }

                }
            },
            error: function() {
                response_message.addClass("error").text("There was a problem adding your entry.").fadeIn(1);
            },
            beforeSend: function() {
                toggleLoading();
            },
            complete: function() {
                toggleLoading();
            }
        });
    });
}

它似乎无法正常使用管道或其他东西;我认为它没有将'日期'翻译成原始日期,而是将其作为一个单词。

1 个答案:

答案 0 :(得分:4)

您的代码会显示常规引号'…',而不是反向标记`…`。使用

hour=$(date | cut -c12-13)

cut-c12-13之间的间距很重要。通常,使用$(…)比使用反向标记更好(有关原因的讨论,请参阅Pipe standard input and command line arguments in Bash)。另外,请在这里了解How to debug a Bash script,这意味着bash -x script.sh

使用写入的代码,$hour中的值是字符串date|cut-c12-13(不是命令的输出),它不是整数表达式,因此是错误消息。此外,您很可能没有名为cut-c12-13的命令,这就是为什么间距很重要的原因。

正如对问题的评论所示,还有许多其他问题也应该解决,从直接从date命令生成您想要的信息到在变量引用周围使用双引号,而不是使用弃用的运算符{ {1}}和-a-o,并且有些人认为您应该[优先使用[[ … ]],或者使用特定于Bash的[ … ] 1}}等等。