Linux time命令和read命令
time的命令,用于测量命令的运行时间,还可以测量内存、I/O等的使用情况。
time free -m
total used free shared buff/cache available
Mem: 7982 218 3664 8 4099 7455
Swap: 0 0 0
real 0m0.016s
user 0m0.002s
sys 0m0.013s
time 命令直接接 普通命令即可, real 这一行是指程序真实运行时间,单位为秒。
user 指的是用户态cpu使用时间
sys指的是系统态cpu使用时间
下面具体的代码演示:
[root@08 etc]# type -a time time is a shell keyword time is /usr/bin/time #type命令查询出time命令是shell的一个关键字 [root@08 etc]# /usr/bin/time -o /root/f1 free -m total used free shared buff/cache available Mem: 7982 219 3663 8 4099 7455 Swap: 0 0 0 #必须完整命令路径 -o 需要保存的路径及文件名称 执行的命令 free -m [root@08 etc]# cat f1 0.00user 0.00system 0:00.00elapsed 87%CPU (0avgtext+0avgdata 1528maxresident)k 0inputs+0outputs (0major+460minor)pagefaults 0swaps #所生成的文件的内容 [root@08 ~]# time free -m total used free shared buff/cache available Mem: 7982 218 3664 8 4099 7455 Swap: 0 0 0 real 0m0.010s user 0m0.004s sys 0m0.006s #正常执行time命令所输出的东西 [root@08 ~]# /usr/bin/time -v free -m total used free shared buff/cache available Mem: 7982 219 3663 8 4099 7455 Swap: 0 0 0 Command being timed: "free -m" User time (seconds): 0.00 System time (seconds): 0.00 Percent of CPU this job got: 100% Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size (kbytes): 0 Average total size (kbytes): 0 Maximum resident set size (kbytes): 1528 Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 0 Minor (reclaiming a frame) page faults: 459 Voluntary context switches: 1 Involuntary context switches: 1 Swaps: 0 File system inputs: 0 File system outputs: 0 Socket messages sent: 0 Socket messages received: 0 Signals delivered: 0 Page size (bytes): 4096 Exit status: 0 #详细的free -m 程序所使用的系统资源使用情况 如何将上述命令结果导入文本文件中?? [root@08 ~]# { /usr/bin/time -v free -m; } 2>a.txt total used free shared buff/cache available Mem: 7982 219 3662 8 4101 7455 Swap: 0 0 0 [root@08 ~]# cat a.txt Command being timed: "free -m" User time (seconds): 0.00 System time (seconds): 0.00 Percent of CPU this job got: 87% Elapsed (wall clock) time (h:mm:ss or m:ss): 0:00.00 Average shared text size (kbytes): 0 Average unshared data size (kbytes): 0 Average stack size (kbytes): 0 Average total size (kbytes): 0 Maximum resident set size (kbytes): 1528 Average resident set size (kbytes): 0 Major (requiring I/O) page faults: 0 Minor (reclaiming a frame) page faults: 459 Voluntary context switches: 1 Involuntary context switches: 1 Swaps: 0 File system inputs: 0 File system outputs: 0 Socket messages sent: 0 Socket messages received: 0 Signals delivered: 0 Page size (bytes): 4096 Exit status: 0 #错误输出重定向即可解决该类问题
read命令:
该命令处理键盘的标准输入以及读入文本文档的一行并将其作为一个变量在shell脚本中使用,并且配合参数 t 可以控制键盘输入的等待时间,下面代码简单演示:
[root@08 ~]# read -t 30 -p "请输入密码:" passwd 请输入密码:1111 [root@08 ~]# echo $passwd 1111 #30秒之内根据提示输入内容,该内容为变量passwd的值,如果超时,程序自动退出并返回非零的任意状态码 [root@08 ~]# read -t 2 -p "请输入密码:" passwd 请输入密码:[root@08 ~]# echo $? 142 #返回了一个非零的状态码
该脚本读入一个文本文档,每一行都通过read命令读取后作为变量username的值。该脚本算是比较经典的一个添加用户的脚本了