Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的bash脚本不告诉它就创建文件?_Bash_File_Shell_Scripting_Osx Yosemite - Fatal编程技术网

为什么我的bash脚本不告诉它就创建文件?

为什么我的bash脚本不告诉它就创建文件?,bash,file,shell,scripting,osx-yosemite,Bash,File,Shell,Scripting,Osx Yosemite,我正在运行下面的脚本,它有一个函数,用于告诉我一个日期是否早于另一个日期,如脚本底部所示 现在,脚本有一些bug。但其中一个特别奇怪。该脚本创建由最后一个参数输入的日期命名的文件 它创建名为“09”、“12”和“2015”的文件。为什么要创建这些文件?这里是函数。您会注意到最后几行使用输入调用函数 function compare_two { if [ $1 < $2 ]; then return 2 elif [ $1 > $2 ]; then re

我正在运行下面的脚本,它有一个函数,用于告诉我一个日期是否早于另一个日期,如脚本底部所示

现在,脚本有一些bug。但其中一个特别奇怪。该脚本创建由最后一个参数输入的日期命名的文件

它创建名为“09”、“12”和“2015”的文件。为什么要创建这些文件?这里是函数。您会注意到最后几行使用输入调用函数

function compare_two {
if [ $1 < $2 ];
then
        return 2
elif [ $1 > $2 ];
then
        return 3
else
        return 4
fi
}


function compare_dates {
# two input arguments:
# e.g.  2015-09-17 2011-9-18

date1=$1
date2=$2

IFS="-"


test=( $date1 )
Y1=${test[0]}
M1=${test[1]}
D1=${test[2]}

test=( $date2 )
Y2=${test[0]}
M2=${test[1]}
D2=${test[2]}

compare_two $Y1 $Y2
if [ $? == 2 ];
then
        echo "returning 2"
        return 2
elif [ $? == 3 ];
then
        return 3
else
        compare_two $M1 $M2;
        if [ $? == 2 ];
        then
                echo "returning 2"
                return 2
        elif [ $? == 3 ];
        then
                return 3
        else
                compare_two $D1 $D2;
                if [ $? == 2 ];
                then
                        echo $?
                        echo "return 2"
                        return 2
                elif [ $? == 3 ];
                then
                        echo "returning 3"
                        return 3
                else
                        return 4
                fi
        fi
fi
}

compare_dates 2015-09-17 2015-09-12
echo $?

我知道结果不正确。但我以后会解决的。创建这些文件的是什么?如何停止?谢谢。

较低和较大的符号被解释为重定向。
键入man test并找出正确的语法

您的问题在于
[$1<$2]
,因为
不是您认为的运算符。它不是
[
中的大于运算符。它是输出重定向。您需要
-gt
[[[$1>$2]]
。它是您的朋友。
[
确实有
作为字符串比较运算符,但您必须对其进行转义,以便
[
实际将其作为参数接收。
returning 2
2
[ $1 \< $2 ]
[ $1 -lt $2 ]
(( $1 < $2 ))                     # works in bash.
(( 10#$1 < 10#$2 ))
(( 10#$1 > 10#$2 ))
a=$(date -d '2015-09-17' '+%s');
b=$(date -d '2015-09-12' '+%s');
compare_two "$a"  "$b"