现在的位置: 首页 > 综合 > 正文

shell 知识点补充(4)-date/数值运算/test 指令/判断符号 [ ]/预设变数($0)/条件判断:if then

2013年08月19日 ⁄ 综合 ⁄ 共 14072字 ⁄ 字号 评论关闭

1、date

linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟。系统时钟是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电的时钟,这个硬件时钟可以在BIOS中进行设置。当Linux启动时,硬件时钟会去读取系统时钟的设置,然后系统时钟就会独立于硬件运作。

Linux中的所有命令(包括函数)都是采用的系统时钟设置。在Linux中,用于时钟查看和设置的命令主要有date、hwclock。
1、date
名称 : date
使用权限 : 所有使用者
使用方式 :
date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] [--version] [+FORMAT] [MMDDhhmm[[CC]YY][.ss]]
说明 :
date 可以用来显示或设定系统的日期与时间,在显示方面,使用者可以设定欲显示的格式,格式设定为一个加号后接数个标记,其中可用的标记列表如下 :
时间方面 :
% : 印出 %
%n : 下一行
%t : 跳格
%H : 小时(00-23)
%I : 小时(01-12)
%k : 小时(0-23)
%l : 小时(1-12)
%M : 分钟(00-59)
%p : 显示本地 AM 或 PM
%r : 直接显示时间 (12 小时制,格式为 hh:mm:ss [AP]M)
%s : 从 1970 年 1 月 1 日 00:00:00 UTC 到目前为止的秒数
%S : 秒(00-60)
%T : 直接显示时间 (24 小时制)
%X : 相当于 %H:%M:%S
%Z : 显示时区
日期方面 :
%a : 星期几 (Sun-Sat)
%A : 星期几 (Sunday-Saturday)
%b : 月份 (Jan-Dec)
%B : 月份 (January-December)
%c : 直接显示日期与时间
%d : 日 (01-31)
%D : 直接显示日期 (mm/dd/yy)
%h : 同 %b
%j : 一年中的第几天 (001-366)
%m : 月份 (01-12)
%U : 一年中的第几周 (00-53) (以 Sunday 为一周的第一天的情形)
%w : 一周中的第几天 (0-6)
%W : 一年中的第几周 (00-53) (以 Monday 为一周的第一天的情形)
%x : 直接显示日期 (mm/dd/yy)
%y : 年份的最后两位数字 (00.99)
%Y : 完整年份 (0000-9999)
若是不以加号作为开头,则表示要设定时间,而时间格式为 MMDDhhmm[[CC]YY][.ss],其中 MM 为月份,DD 为日,hh 为小时,mm 为分钟,CC 为年份前两位数字,YY 为年份后两位数字,ss 为秒数
参数 :
-d datestr : 显示 datestr 中所设定的时间 (非系统时间)
--help : 显示辅助讯息
-s datestr : 将系统时间设为 datestr 中所设定的时间
-u : 显示目前的格林威治时间
--version : 显示版本编号
例子 :
显示时间后跳行,再
显示目前日期: #date '+%T%n%D'
显示月份与日数: date '+%B %d'

显示日期与设定时间(12:34:56): #date --date '12:34:56'

时间赋值给变量后输出

yee@Loong:~$  date1=` date '+%y-%m-%d'` 小指令 (`) 取得讯息、
yee@Loong:~$ echo $date1
12-10-23
yee@Loong:~$

注意 :当你不希望出现无意义的 0 时(比如说 1999/03/07),则可以在标记中插入 - 符号,比如说 date '+%-H:%-M:%-S' 会把时分秒中无意义的 0 给去掉,像是原本的 08:09:04 会变为 8:9:4。另外,只有取得权限者(比如说 root)才能设定系统时间。
当你以 root 身分更改了系统时间之后,请记得以 clock -w 来将系统时间写入 CMOS 中,这样下次重新开机时系统时间才会持续抱持最新的正确值。
例子:修改日期时间
在命令行输入:
Date:显示当前时间 Fri Aug 3 14:15:16 CST 2007
date –s:按字符串方式修改时间
可以只修改日期,不修改时间,输入: date -s 2007-08-03
只修改时间,输入:date -s 14:15:00
同时修改日期时间,注意要加双引号,日期与时间之间有一空格,输入:
#date -s "2007-08-03 14:15:00"
2、查看硬件时间
# hwclock
设置硬件时间
# hwclock -set -date="07/07/06 10:19" (月/日/年 时:分:秒)
3、硬件时间和系统时间的同步
按照前面的说法,重新启动系统,硬件时间会读取系统时间,实现同步,但是在不重新启动的时候,需要用hwclock命令实现同步。
硬件时钟与系统时钟同步:
# hwclock --hctosys(hc代表硬件时间,sys代表系统时间)
系统时钟和硬件时钟同步:(让系统的时间同步到硬件时钟)

# hwclock –systohc

2、数值运算表示:

计算两个数的积:

  1 #!/usr/bin/bash
  2
  3 echo "this is for multiply two numbers:\n"
  4 read -p "the first number:" number1
  5 read -p "the second number:" number2
  6 multi=$(($number1*$number2))
  7 echo  "the answer is : $multi"

在数字的运算上,我们可以使用『 declare -i total=$firstnu*$secnu 』 也可以使用上面的方式来进行!基本上,建议使用这样的方式来进行运算:
var=$((运算内容))

yee@Loong:~$ num=$((13%3))
yee@Loong:~$ echo $num
1

3、test 指令的测试功能

当我要检测系统上面某些档案或者是相关的属性时,利用 test 这个指令来工作, 真是好用得不得了,举例来说,我要检查 /dmtsai 是否存在时,使用:
[root@linux ~]# test -e /dmtsai
执行结果并不会显示任何讯息,但最后我们可以透过 $? 或 && 及 || 来展现整个结果呢! 例如我们在将上面的例子改写成这样:
[root@linux ~]# test -e /dmtsai && echo "exist" || echo "Not exist"
最终的结果可以告知我们是『exist』还是『Not exist』呢!那我知道 -e 是测试一个『东西』在不在, 如果还想要测试一下该档名是啥玩意儿时,还有哪些标志可以来判断的呢?呵呵!有底下这些东西喔!
测试的标志
代表意义
1. 关于某个档名的『类型』侦测(存在与否),如 test -e filename
-e
该『档名』是否存在?(常用)
-f
该『档名』是否为档案(file)?(常用)
-d
该『文件名』是否为目录(directory)?(常用)
-b
该『文件名』是否为一个 block device 装置?
-c
该『文件名』是否为一个 character device 装置?
-S
该『档名』是否为一个 Socket 档案?
-p
该『档名』是否为一个 FIFO (pipe) 档案?
-L
该『档名』是否为一个连结档?
2. 关于档案的权限侦测,如 test -r filename
-r
侦测该文件名是否具有『可读』的属性?
-w
侦测该档名是否具有『可写』的属性?
-x
侦测该档名是否具有『可执行』的属性?
-u
侦测该文件名是否具有『SUID』的属性?
-g
侦测该文件名是否具有『SGID』的属性?
-k
侦测该文件名是否具有『Sticky bit』的属性?
-s
侦测该档名是否为『非空白档案』?
3. 两个档案之间的比较,如: test file1 -nt file2
-nt
(newer than)判断 file1 是否比 file2 新
-ot
(older than)判断 file1 是否比 file2 旧
-ef
判断 file2 与 file2 是否为同一档案,可用在判断 hard link 的判定上。 主要意义在判定,两个档案是否均指向同一个 inode 哩!
4. 关于两个整数之间的判定,例如 test n1 -eq n2
-eq
两数值相等 (equal)
-ne
两数值不等 (not equal)
-gt
n1 大于 n2 (greater than)
-lt
n1 小于 n2 (less than)
-ge
n1 大于等于 n2 (greater than or equal)
-le
n1 小于等于 n2 (less than or equal)
5. 判定字符串的数据
test -z string
判定字符串是否为 0 ?若 string 为空字符串,则为 true
test -n string
判定字符串是否非为 0 ?若 string 为空字符串,则为 false。 注: -n 亦可省略
test str1 = str2
判定 str1 是否等于 str2 ,若相等,则回传 true
test str1 != str2
判定 str1 是否不等于 str2 ,若相等,则回传 false

6. 多重条件判定,例如: test -r filename -a -x filename
-a
(and)两状况同时成立!例如 test -r file -a -x file,则 file 同时具有 r 与 x 权限时,才回传 true。
-o
(or)两状况任何一个成立!例如 test -r file -o -x file,则file 具有 r 或 x 权限时,就可回传 true。
!
反相状态,如 test ! -x file ,当 file 不具有 x 时,回传 true
OK!现在我们就利用 test 来帮我们写几个简单的例子。首先,判断一下, 让使用者输入一个档名,我们判断:
1. 这个档案是否存在,若不存在则给予一个『Filename does not exist』的讯息,并中断程序;
2. 若这个档案存在,则判断他是个档案或目录,结果输出『Filename is regular file』或 『Filename is directory』
3. 判断一下,执行者的身份对这个档案或目录所拥有的权限,并输出权限数据!
你可以先自行创作看看,然后再跟底下的结果讨论讨论。注意利用 test 与 && 还有 || 等标志!
[root@linux scripts]# vi sh05.sh
#!/bin/bash
# Program:
# Let user input a filename, the program will search the filename
# 1.) exist? 2.) file/directory? 3.) file permissions
# History:
# 2005/08/25 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 1. 让使用者输入档名,并且判断使用者是否真的有输入字符串?
echo -e "The program will show you that filename is exist which input by you.\n\n"
read -p "Input a filename : " filename
test -z $filename && echo "You MUST input a filename." && exit 0
# 2. 判断档案是否存在?
test ! -e $filename && echo "The filename $filename DO NOT exist" && exit 0
# 3. 开始判断档案类型与属性
test -f $filename && filetype="regulare file"
test -d $filename && filetype="directory"
test -r $filename && perm="readable"
test -w $filename && perm="$perm writable"
test -x $filename && perm="$perm executable"
# 4. 开始输出信息!
echo "The filename: $filename is a $filetype"
echo "And the permission are : $perm"

实练:

Loong:/home/yee/shell# ll
总计 32
-rw-r--r-- 1 root root 1401 10-19 16:58 aaa.txt
-rw-r--r-- 1 root root  116 10-23 15:19 backup_date.sh
-rw-r--r-- 1 root root  503 10-23 11:23 full-name-output.sh
-rw-r--r-- 1 root root  562 10-19 11:30 kkk.txt
-rw-r--r-- 1 root root  575 10-19 10:56 lll.txt
-rw-r--r-- 1 root root  193 10-23 16:20 multi.sh
drwxr-xr-x 2 root root 4096 10-24 10:03 test
-rw-r--r-- 1 root root  484 10-24 10:01 test_test.sh
Loong:/home/yee/shell# sh test_test.sh
please input the filename for search :test
test  exist
test  is directory
 the permission are : readable writable executable
Loong:/home/yee/shell# sh test_test.sh
please input the filename for search :lll.txt
lll.txt  exist
lll.txt is regular file
 the permission are : readable writable
Loong:/home/yee/shell#

 1 #!/usr/bin/bash
  2
  3
  4 read -p "please input the filename for search :" filename
  5 test -z $filename && echo "You MUST input a filename." && exit 0
  6 test -e $filename && echo "$filename  exist"|| echo "$filename not exist"
  7 test -d $filename && echo "$filename  is directory"
  8 test -f $filename && echo "$filename is regular file"
  9 test -r $filename && perm="readable"
 10 test -w $filename && perm="$perm writable"
 11 test -x $filename && perm="$perm executable"
 12 echo " the permission are : $perm"
 13

4、判断符号 [ ]
除了我们很喜欢使用的 test 之外,其实,我们还可以利用判断符号『 [ ] 』来进行数据的判断呢! 举例来说,如果我想要知道 $HOME 这个变量是否为空的,可以这样做:
[root@linux ~]# [ -z "$HOME" ]
但使用 [] 要特别注意的是,在上述的每个组件中间都需要有空格键来分隔,假设我空格键使用『□』来表示, 那么,在这些地方你都需要有空格键:
[ "$HOME" == "$MAIL" ]
[□"$HOME"□==□"$MAIL"□]

字符串比较
= 等于,如:if [ "$a" = "$b" ]
== 等于,如:if [ "$a" == "$b" ],与=等价
注意:==的功能在[[]]和[]中的行为是不同的,如下:
1 [[ $a == z* ]] # 如果$a以"z"开头(模式匹配)那么将为true
2 [[ $a == "z*" ]] # 如果$a等于z*(字符匹配),那么结果为true
3
4 [ $a == z* ] # File globbing 和word splitting将会发生
5 [ "$a" == "z*" ] # 如果$a等于z*(字符匹配),那么结果为true
一点解释,关于File globbing是一种关于文件的速记法,比如"*.c"就是,再如~也是.

上面的例子在说明,两个字符串 $HOME 与 $MAIL 是否相同的意思,相当于 test $HOME = $MAIL 的意思啦! 而如果没有空白分隔,例如 [$HOME==$MAIL] 时,我们的 bash 就会显示错误讯息了!这可要很注意啊! 所以说,您最好要注意:
• 在中括号 [] 内的每个组件都需要有空格键来分隔;
• 在中括号内的变量,最好都以双引号来设定;
• 在中括号内的常数,最好都以单或双引号来设定。
举例来说,假如我设定了 name="VBird Tsai" ,然后这样判定:
[root@linux ~]# name="VBird Tsai"
[root@linux ~]# [ $name == "VBird" ]
bash: [: too many arguments
为什么呢?因为 $name 如果没有使用双引号刮起来,那么上面的判定式会变成:
[ VBird Tsai == "VBird" ]
而不是我们要的:
[ "VBird Tsai" == "VBird" ]
这可是差很多的喔!另外,中括号的使用方法与标志与 test 几乎一模一样啊~ 只是中括号比较常用在条件判断式 if ..... then ..... fi 的情况中就是了。 好,那我们也继续来做一个小案例好了:
1. 当执行一个程序的时候,这个程序会让使用者选择 Y 或 N ,
2. 如果使用者输入 Y 或 y 时,就显示『 OK, continue 』
3. 如果使用者输入 n 或 N 时,就显示『 Oh, interrupt !』
4. 如果不是 Y/y/N/n 之内的其它字符,就显示『I don't know what is your choise』
利用中括号、 && 与 || 来继续吧!
[root@linux scripts]# vi sh06.sh
#!/bin/bash
# Program:
# This program will show the user's choice
# History:
# 2005/08/25 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input (Y/N): " yn
[ "$yn" == "Y" -o "$yn" == "y" ] && echo "OK, continue" && exit 0                   [ ]中的逻辑与和逻辑或使用-a 和-o 表示。
[ "$yn" == "N" -o "$yn" == "n" ] && echo "Oh, interrupt!" && exit 0
echo "I don't know what is your choise" && exit 0

实练:

1 #!/usr/bin/bash
  2
  3
  4 echo "will you excute this program ? Y or N"
  5 read -p "your choise is:" ans
  6 [ "$ans" = "Y" ]||[ "$ans" = "y" ] && echo "OK, continue" && exit 0
  7 [ "$ans" = "N" ]||[ "$ans" = "n" ] && echo "oh,interrupt" || echo "I don't knoow what     to do"

Loong:/home/yee/shell# sh judge_bracket.sh
will you excute this program ? Y or N
your choise is:y
[: 6: y: unexpected operator                 #使用“==”后会发生报错,应该是由于使用的解释器不同造成的;
[: 6: y: unexpected operator
[: 7: y: unexpected operator
[: 7: y: unexpected operator
I don't knoow what to do
Loong:/home/yee/shell# sh judge_bracket.sh
will you excute this program ? Y or N
your choise is:y
OK, continue
Loong:/home/yee/shell# sh judge_bracket.sh
will you excute this program ? Y or N
your choise is:n
oh,interrupt
Loong:/home/yee/shell# sh judge_bracket.sh
will you excute this program ? Y or N
your choise is:u
I don't knoow what to do
Loong:/home/yee/shell#

利用这个字符串判别的方法,我们就可以很轻松的将使用者想要进行的工作分门别类呢! 接下来,我们再来谈一些其它有的没有的东西吧!
Tips: 为什么判断式里面下达等于要用 == 而不是一个 = 就好了呢?我们在前一章正规表示法里面的 awk 提到, 只有一个 = 用来给予一个变量设定其内容,逻辑判断时,则会给予两个等于, 亦即『比较』而非『设定』的意思~这里要好好的分辨一下喔!

因为ubuntu默认的sh是连接到dash的,又因为dash跟bash的不兼容所以出错了.执行时可以把sh换成bash 文件名.sh来执行.成功;dash判断字符串相等用 的是=

修改为等号“==”,以bash执行的结果就正常了!

Loong:/home/yee/shell# ./judge_bracket.sh
will you excute this program ? Y or N
your choise is:y
OK, continue
Loong:/home/yee/shell# ./judge_bracket.sh ]
will you excute this program ? Y or N
your choise is:n
oh,interrupt
Loong:/home/yee/shell# ./judge_bracket.sh ]
will you excute this program ? Y or N
your choise is:u
I don't knoow what to do
Loong:/home/yee/shell#

5、预设变数($0, $1...)

当我们执行一个 shell script 时,在这个 shell script 里面就已将帮我们做好一些可用的变量了。 举例来说,在不久的将来,您就会发现,当我们要启动一个系统服务时,可能会下达类似这样的指令:
[root@linux ~]# /etc/init.d/crond restart
那是啥玩意儿?呵呵!就是『向 /etc/init.d/crond 这个 script 下达 restart 的指令』, 咦!我们不是都使用 read 来读取使用者输入的变量内容吗?为啥我可以直接在 script 后面接上这个参数? 这是因为 shell script 帮我们设定好一些指定的变量了!变量的对应是这样的:
/path/to/scriptname opt1 opt2 opt3 opt4 ...
$0 $1 $2 $3 $4 ...
这样够清楚了吧?!执行的文件名为 $0 这个变量,第一个接的参数就是 $1 啊~ 所以,只要我们在 script 里面善用 $1 的话,就可以很简单的立即下达某些指令功能了! 好了,来做个例子吧~假设我要执行一个 script ,执行后,该 script 会自动列出自己的档名, 还有后面接的前三个参数,该如何是好?
[root@linux scripts]# vi sh07.sh
#!/bin/bash
# Program:
# The program will show it's name and first 3 parameters.
# History:
# 2005/08/25 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "The script naem is ==> $0"
[ -n "$1" ] && echo "The 1st paramter is ==> $1" || exit 0
[ -n "$2" ] && echo "The 2nd paramter is ==> $2" || exit 0
[ -n "$3" ] && echo "The 3th paramter is ==> $3" || exit 0
这支程序里面鸟哥加上了一些控制式,亦即利用 && 及 || 来加以判断 $1 ~ $3 是否存在? 若存在才显示,若不存在就中断~执行结果如下:
[root@linux scripts]# sh sh07.sh theone haha quot
The script naem is ==> sh07.sh
The 1st paramter is ==> theone
The 2nd paramter is ==> haha
The 3th paramter is ==> quot

6、条件判断:if then

if .... then 是最常见的条件判断式了~简单的说,就是当符合某个条件判断的时候, 就予以进行某项工作就是了。我们可以简单的这样看:
if [ 条件判断式 ]; then
当条件判断式成立时,可以进行的指令工作内容;
fi
至于条件判断式的判断方法,与前一小节的介绍相同啊!较特别的是,如果我有多个条件要判别时, 除了 sh06.sh 那个案例,也就是将多个条件写入一个中括号内的情况之外, 我还可以有多个中括号来隔开喔!而括号与括号之间,则以 && 或 || 来隔开,他们的意义是:
• && 代表 AND ;
• || 代表 or ;
所以,在使用中括号的判断式中, && 及 || 就与指令下达的状态不同了。举例来说, sh06.sh 那个例子我可以改写成这样:
[root@linux scripts]# vi sh06-2.sh
#!/bin/bash
# Program:
# This program will show the user's choice
# History:
# 2005/08/25 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input (Y/N): " yn
if [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then
echo "OK, continue"
exit 0
fi
if [ "$yn" == "N" ] || [ "$yn" == "n" ]; then
echo "Oh, interrupt!"
exit 0
fi
echo "I don't know what is your choise" && exit 0

如果考虑更复杂的情况,则可以使用这个语法:
if [ 条件判断式一 ]; then
当条件判断式一成立时,可以进行的指令工作内容;
elif [ 条件判断式二 ]; then
当条件判断式二成立时,可以进行的指令工作内容;
else
当条件判断式一与二均不成立时,可以进行的指令工作内容;
fi

如果我要侦测你所输入的参数是否为 hello 呢 , 也就是说,如果我想要知道,你在程序后面所接的第一个参数 (就是 $1 啊!) 是否为 hello ,
1. 如果是的话,就显示 "Hello, how are you ?";
2. 如果没有加任何参数,就提示使用者必须要使用的参数下达法;
3. 而如果加入的参数不是 hello ,就提醒使用者仅能使用 hello 为参数。
整个程序的撰写可以是这样的:
[root@linux scripts]# vi sh08.sh
#!/bin/bash
# Program:
# Show "Hello" from $1....
# History:
# 2005/08/28 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
if [ "$1" == "hello" ]; then
echo "Hello, how are you ?"
elif [ "$1" == "" ]; then
echo "You MUST input parameters, ex> $0 someword"
else
echo "The only parameter is 'hello'"
fi

实练:

1 #!/bin/bash
  2
  3 echo "The script name is ==> $0"
  4 #[ -n "$1" ] && echo "The 1st paramter is ==> $1" || exit 0  使用exit 后会导致后续程序不再执行
  5 if [ "$1" == "hello" ]; then
  6         echo "hello, how are you ?"
  7 elif [ "$1" == "" ]; then
  8         echo "you must use the correct usage"
  9 else
 10         echo "you only can use the argument with 'hello'"

Loong:/home/yee/shell# ./condition_judge.sh hello
The script name is ==> ./condition_judge.sh
The 1st paramter is ==> hello
hello, how are you ?
Loong:/home/yee/shell# ./condition_judge.sh false
The script name is ==> ./condition_judge.sh
The 1st paramter is ==> false
you only can use the argument with 'hello'
Loong:/home/yee/shell# ./condition_judge.sh
The script name is ==> ./condition_judge.sh
Loong:/home/yee/shell# ./condition_judge.sh
The script name is ==> ./condition_judge.sh
Loong:/home/yee/shell# ./condition_judge.sh
The script name is ==> ./condition_judge.sh      去掉exit 0 后执行
you must use the correct usage
Loong:/home/yee/shell#

撰写一支小程序,用来『计算退伍日期还剩几天?』也就是说:
1. 先让使用者输入他们的退伍日期;
2. 再由现在日期比对退伍日期;
3. 由两个日期的比较来显示『还需要几天』才能够退伍的字样。
似乎挺难的样子?其实也不会啦,利用『 date --date="YYYYMMDD" +%s 』就能够达到我们所想要的啰~如果您已经写完了程序,对照底下的写法试看看:
[root@linux scripts]# vi sh10.sh
#!/bin/bash
# Program:
# Tring to calculate your demobilization date at how many days
# later...
# History:
# 2005/08/29 VBird First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 1. 告知使用者这支程序的用途,并且告知应该如何输入日期格式?
echo "This program will try to calculate :"
echo "How many days about your demobilization date..."
read -p "Please input your demobilization date (YYYYMMDD ex>20050401): " date2
# 2. 测试一下,这个输入的内容是否正确?利用正规表示法啰~
date_d=`echo $date2 |grep '[0-9]\{8\}'`
if [ "$date_d" == "" ]; then
echo "You input the wrong format of date...."
exit 1
fi
# 3. 开始计算日期啰~
declare -i date_dem=`date --date="$date2" +%s`                  时间转化为秒的方法
declare -i date_now=`date +%s`
declare -i date_total_s=$(($date_dem-$date_now))
declare -i date_d=$(($date_total_s/60/60/24))
if [ "$date_total_s" -lt "0" ]; then
echo "You had been demobilization before: " $((-1*$date_d)) " ago"
else
declare -i date_h=$(($(($date_total_s-$date_d*60*60*24))/60/60))
echo "You will be demobilized after $date_d days and $date_h hours."
fi

实练:

 1 #!/bin/bash
  2
  3
  4 echo "please input your army over date with the format of 'yyyymmdd'"
  5 read -p "the date is(date>20121024): " date1
  6 date_now=` date '+%s'`
  7 date_over=`date --date="$date1" +%s`
  8 date_total_s=$(($date_over-$date_now))
  9 date_days=$(($date_total_s/60/60/24))
 10 if [ "$date_total_s" -lt "0" ]; then
 11         echo "You had been army over before: " $((-1*$date_days)) " ago"
 12 else
 13         date_h=$(($(($date_total_s-$date_days*60*60*24))/60/60))
 14         echo "You will be army over after $date_days days and $date_h hours."
 15 fi

Loong:/home/yee/shell# sh calculate_time.sh
please input your army over date with the format of 'yyyymmdd'
the date is(date>20121024): 20130605
You will be army over after 223 days and 6 hours.
Loong:/home/yee/shell# sh calculate_time.sh
please input your army over date with the format of 'yyyymmdd'
the date is(date>20121024): 20120406
You had been army over before:  201  ago
Loong:/home/yee/shell#

抱歉!评论已关闭.