修正する

修正する

組み込みポッキーLinuxはntpdate利用できません。

次のコマンドのどれも適切なものがないようです。

# date -s "$(curl -s --head http://google.com | grep '^Date:' | cut -d' ' -f 3-)" 
date: invalid date '08 Mar 2021 13:22:34 GMT'
# date -s "$(curl -s --head http://google.com | grep '^Date:' | cut -d' ' -f 3-6)"
date: invalid date '08 Mar 2021 13:22:34'
#date +"%d %b %Y %H:%M:%S" -s "$(curl -s --head http://google.com | grep '^Date:' | cut -d' ' -f 3-6)"
date: invalid date '08 Mar 2021 13:22:34'

修正する

BusyBoxの日付は、次の日付形式のみを受け入れます。

@seconds_since_1970
hh:mm[:ss]
[YYYY.]MM.DD-hh:mm[:ss]
YYYY-MM-DD hh:mm[:ss]
[[[[[YY]YY]MM]DD]hh]mm[.ss]

-D使用しているバージョンのBusyBoxでそのオプションが利用可能な場合は、提案されているようにこのコマンドを使用できます。スチールドライバー:

busybox date -d '08 Mar 2021 13:22:34' -D '%d %b %Y %H:%M:%S'

ベストアンサー1

busybox date最後に、Googleの時間文字列を解析して、使い慣れた形式()に変換しましたYYYY-mm-dd HH:MM:SS。誰かが将来これが役に立つと思うことを願っています。

#!/bin/sh

monthnumber() {
    month=$1
    months="JanFebMarAprMayJunJulAugSepOctNovDec"
    tmp=${months%%$month*}
    month=${#tmp}
    monthnumber=$((month/3+1))
    printf "%02d\n" $monthnumber
}

G_DATE="$(curl -s --head http://google.com | grep '^Date:' | cut -d' ' -f 3-6)"
G_SPLIT=($(echo $G_DATE | tr " "))

BB_DATE="${G_SPLIT[2]}-$(monthnumber ${G_SPLIT[1]})-${G_SPLIT[0]} ${G_SPLIT[3]}"

date -s "$BB_DATE"

monthnumber()から取得した関数https://stackoverflow.com/a/41385862/9815377

おすすめ記事