当datestring为空时,Bash date返回当前日期

当datestring为空时,Bash date返回当前日期,bash,unix,Bash,Unix,日期-d“datestring”-当datestring为null/空时,返回shell脚本中的当前日期。但是我想在datestring为null时使脚本失败。当然,我可以在这行代码上面加一个条件来检查datestring是否为null。但是,有没有办法签入日期行本身?当变量为空或未设置时,shell有退出的语法: : ${datestring:?cannot be empty} 如果未提供值,则用于指定值: date -d "${datestring:-now}" 如果运算符中没有冒号,则

日期-d“datestring”-当datestring为null/空时,返回shell脚本中的当前日期。但是我想在datestring为null时使脚本失败。当然,我可以在这行代码上面加一个条件来检查datestring是否为null。但是,有没有办法签入日期行本身?

当变量为空或未设置时,shell有退出的语法:

: ${datestring:?cannot be empty}
如果未提供值,则用于指定值:

date -d "${datestring:-now}"
如果运算符中没有冒号,则只检查未设置的变量(可以将其设置为空)。原始的Bourne shell只有无冒号的变体,您必须单独检查空值


如果变量为(空或)未设置,则
${var:?error message string}
和无冒号
${var?error message string}
将导致脚本退出,并在
error message string
中显示消息。如果以交互方式使用这些命令,交互式shell将不会退出,但您仍然会收到错误消息,
$?
将设置为非零数字,以机器可读的形式传达错误。

使用GNU coreutils 8.5中的日期:如果datestring为null/空,则输出“Mon-Dec 18 00:00:00 CET 2017”。那不是当前的日期/时间。我说的对吗?“-bash:datestring:exit 1”是消息还是错误?$日期+%Y-%m-%d-d“${datestring:?exit 1}”$-bash:datestring:exit 1$echo$?1$datestring='2010-01-01'$date+%Y-%m-%d-d“${datestring:?退出1}”2010-01-01$echo$?0问号后面的东西只是一个字符串,显示为错误消息。它不会退出交互式shell,但如果在脚本中使用它,则如果变量为空或未设置,它将导致脚本与该消息一起退出。我将更新答案以澄清这一点。