在bash中以0开头的数字递增 [英] incrementing a number in bash with leading 0

查看:77
本文介绍了在bash中以0开头的数字递增的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是bash脚本的新手,并且正在尝试编写一些代码来解析和处理我正在处理的文件.

I am a very newbie to bash scripting and am trying to write some code to parse and manipulate a file that I am working on.

我需要增加和减少文件中一堆不同时间的时间.我的问题发生在时间是例如2:04或14:00时.

I need to increment and decrement the minute of a time for a bunch of different times in a file. My problem happens when the time is for example 2:04 or 14:00.

文件示例:

2:43
2:05
15:00

我当前从bash脚本中摘录的内容是这样的

My current excerpt from my bash script is like this

for x in `cat $1`;
  do minute_var=$(echo $x | cut -d: -f2);
  incr_min=$(($minute_var + 1 | bc));
  echo $incr_min;
done

当前结果:

44
6
1

必填结果:

44
06
01

任何建议

推荐答案

使用printf:

incr_min=$(printf %02d $(($minute_var + 1 )) )

如果仅涉及整数,则不需要bc.

No that bc is not needed if only integers are involved.

这篇关于在bash中以0开头的数字递增的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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