小编Mic*_*ael的帖子

向用户输入添加前导零,但正在使用 printf 进行转换

我目前正在寻找以下代码的替代方案,该代码的工作方式不那么“不稳定”。

printf "Please enter the ticket number:\t"; read vTICKET; vTICKET=$(printf %04d "$vTICKET"); printf "$vTICKET\n";
Run Code Online (Sandbox Code Playgroud)

如果我输入072作为输入,这就是我看到的

Please enter the ticket number: 072
0058
Run Code Online (Sandbox Code Playgroud)

我想知道是否有另一种方法可以让我对输入或read命令更加宽容?printf似乎是在不实际测试字符串长度的情况下添加前导零的酷方法。

bash input shell-script numeric-data printf

3
推荐指数
1
解决办法
391
查看次数

标签 统计

bash ×1

input ×1

numeric-data ×1

printf ×1

shell-script ×1