bash:数字时钟模式

问题描述 投票:0回答:1

我喜欢数字时钟。我想知道:有多少不同的数字模式构成一条街道?

例如01:23:45 .. 23:54:10

bash 中提供一个很酷的实现,还带有实用程序!

我的出发点是:

i=0
for ht in 0 1 2; do
        for ho in {0..5}; do
                for mt in {0..5}; do
                        for mo in {0..5}; do
                                for st in {0..5}; do
                                        for so in {0..5}; do
                                                al="$ht$ho$mt$mo$st$so"

                                                if (    echo $al |grep -q 0 &&
                                                        echo $al |grep -q 1 &&
                                                        echo $al |grep -q 2 &&
                                                        echo $al |grep -q 3 &&
                                                        echo $al |grep -q 4 &&
                                                        echo $al |grep -q 5 ); then
                                                                ((++i))
                                                                printf "%03d: %s%s:%s%s:%s%s\n" $i $ht $ho $mt $mo $st $so
                                                fi
                                        done
                                done
                        done
                done
        done
done

但最后它又慢又错误!

我做了什么: 我写了一个 bash 脚本 只是为了好玩。

bash utilities
1个回答
0
投票

很慢

因为您正在启动一个新的 shell 来检查每个数字。在一个 shell 中完成所有操作,看看 bash 到底有多慢:

#!/bin/bash
i=0
for ht in 0 1 2; do
    for ho in {0..5}; do
        for mt in {0..5}; do
            for mo in {0..5}; do
                for st in {0..5}; do
                    for so in {0..5}; do
                        al="$ht$ho$mt$mo$st$so"
                        
                        if [[ $al = *0* &&
                              $al = *1* &&
                              $al = *2* &&
                              $al = *3* &&
                              $al = *4* &&
                              $al = *5* ]] ; then
                            ((++i))
                            printf "%03d: %s%s:%s%s:%s%s\n" $i $ht $ho $mt $mo $st $so
                        fi
                    done
                done
            done
        done
    done
done
© www.soinside.com 2019 - 2024. All rights reserved.