Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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_Date_Printf_Pad - Fatal编程技术网

Bash 如何抑制日期输出中的前导零?

Bash 如何抑制日期输出中的前导零?,bash,date,printf,pad,Bash,Date,Printf,Pad,我有以下代码: printf -v s '%(%S)T' -1 # grab the current second if ((s == 0)); then # at the top of the minute, run some code fi 此代码在每分钟的第八秒和第九秒抛出一个错误: bash: ((: 08: value too great for base (error token is "08") bash: ((: 09: value too great for base (

我有以下代码:

printf -v s '%(%S)T' -1 # grab the current second
if ((s == 0)); then
  # at the top of the minute, run some code
fi
此代码在每分钟的第八秒和第九秒抛出一个错误:

bash: ((: 08: value too great for base (error token is "08")
bash: ((: 09: value too great for base (error token is "09")

我怎样才能纠正这个问题?基本上,我们需要抑制由
printf

生成的日期输出中的前导零,在格式字符串中使用
-
前缀,因此:

printf -v s '%(%-S)T' -1
这将抑制前导零

解决此问题的一种更通用的方法是以这种方式在Bash算法中指定基,同时保持
printf
命令不变:

if ((10#$s == 0)); then

有关Unix和Linux堆栈交换的帖子:


在格式字符串中使用
-
前缀,因此:

printf -v s '%(%-S)T' -1
这将抑制前导零

解决此问题的一种更通用的方法是以这种方式在Bash算法中指定基,同时保持
printf
命令不变:

if ((10#$s == 0)); then

有关Unix和Linux堆栈交换的帖子:


s=${s##*0}
是一个简单的参数扩展,用于删除所有前导零。@DavidC.RankinDav
s=10
呢?这将删除所有内容。@DavidC.Rankin这可能更好:
shopt-s extglob;s=${s##+(0)}
。同意
extglob
提供了更好的方法来保护非前导零。另一个是子字符串替换,例如,
${a//^0*/}
@DavidC.Rankin锚点实际上应该是
#
。但是,使用
${a//{0*/}
不会有帮助(
在这里不被视为锚,因为额外的
/
),也不会(
${a/{0*/}
)(
在这里被视为锚,但如果数字至少以一个
0
开头,它将放弃所有内容,因为
*
)。我只看到了一个可能的选项,再次使用extglob:
${a/#+(0)/}
${a/#*(0)/}
s=${s##*0}
是一个简单的参数扩展,可以删除所有前导零。@DavidC.ranav kindav
s=10
呢?这将删除所有内容。@DavidC.Rankin这可能更好:
shopt-s extglob;s=${s##+(0)}
。同意
extglob
提供了更好的方法来保护非前导零。另一个是子字符串替换,例如,
${a//^0*/}
@DavidC.Rankin锚点实际上应该是
#
。但是,使用
${a//{0*/}
不会有帮助(
在这里不被视为锚,因为额外的
/
),也不会(
${a/{0*/}
)(
在这里被视为锚,但如果数字至少以一个
0
开头,它将放弃所有内容,因为
*
)。我只看到一个可能的选项,再次使用extglob:
${a/#+(0)/}
${a/#*(0)/}