【まとめ】BASH【時間形式】

2883 ワード

詳細
実際の作業で使用される簡単なshellスクリプト
#!/bin/bash

echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"

date=$(date+%Y%m%d)

echo "coco_wyx.log_login ......"
/usr/local/mysql/bin/mysqldump -uroot coco_wyx log_login > /downloads/backup/log_login.$date.sql

echo ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"

exit 0

 
bashの時間フォーマット
   %a          (Sun..Sat)

  %A          (Sunday..Saturday)

  %b         (Jan..Dec)

  %B          (January..December)

  %c            (Sat Nov 04 12:02:33 EST 1989)

  %C n   [00-99]

  %d      (01..31)

  %D       (mm/dd/yy)

  %e      ( 1..31)

  %F        same as %Y-%m-%d

  %h same as %b

  %H 24       (00..23)

  %I 12       (01..12)

  %j         (001..366)

  %k    24       ( 0..23)

  %l    12       ( 1..12)

  %m      (01..12)

  %M      (00..59)

  %n   

  %N       (000000000..999999999)

  %p              (blank in many locales)

  %P              (blank in many locales)

  %r 12        ( : : ,  ) time, 12-hour (hh:mm:ss [AP]M)

  %R 24         ( : ,  )time, 24-hour (hh:mm)

  %s       1970-01-01 00:00:00         (a GNU extension)

  %S     second (00..60);

  %t      (tab)

  %T 24       (hh:mm:ss)

  %u        (       1-7)

  %U               (00..53)

  %V               (01..53)

  %w                (0..6)

  %W               (00..53)

  %x        (mm/dd/yy)

  %X        (%H:%M:%S)

  %y     (00..99)

  %Y   (1970…)

 
ネット上の少し複雑な例は、まだテストに間に合いません.
#!/bin/bash

read -p "Input your birthday(YYYYmmdd):" date1

m=`date --date="$date1" +%m`    #      

d=`date --date="$date1" +%d`    #      

date_now=`date +%s`      #         

y=`date +%Y`            #        

birth=`date --date="$y$m$d" +%s`      #            

internal=$(($birth-$date_now))       #              

if [ "$internal" -lt "0" ]; then           #           

  birth=`date --date="$(($y+1))$m$d" +%s`      #           

  internal=$(($birth-$date_now))        #               

fi

echo "There is :$((einternal/60/60/24)) days."       #    ,