微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

时差bash脚本

我正在尝试创build一个bash脚本来计算用户的第一次login和用户最近login之间的时间差。 任何帮助表示赞赏:)

这是我迄今为止:

read -p "Enter a user ID: " ID echo "You entered the following ID(s): $ID" #/bin/egrep -i "^$ID" /etc/passwd echo -n "The users real name is: " /bin/grep "^$ID" /etc/passwd | cut -f5 -d : echo -n "$ID's first login time is: " l1=`last "$ID" | tail -n 1` echo -n "$ID's last login time is: " l2=`last "$ID" | head -n 1` echo $l1 echo $l2 echo -n "The time difference is $(l1-l2) "

在linux中用batch file启动多个firefoxconfiguration文件

在Linux中recursion地添加bash脚本作为命令?

在Bash中导出path

在bash文件中交换2个单词的最佳方式是什么?

以更好的方式分配variables值

从一行中提取多个浮点数

如何在terminal上编写和运行一个shell程序而不保存它

如何基于列join多个txt文件

在bash中捕获键盘中断

在Linuxterminal中的目录之间快速和智能导航

这是基于你想提供一个用户名而不是一个ID的假设。

首先,你想正确地执行你的捕捉

l1=$(last "$ID" | tail -n 1) l2=$(last "$ID" | head -n 1)

在我的例子离开

l1="wtmp begins Sun Nov 9 07:32:12 2014" l2="graham pts/11 :0 Sat Nov 29 22:13 still logged in"

这是不好的,因为我们需要日期

所以我们来解决这个问题。 这里有一些hacky解析只获得时间:

l1=$(last -RF | grep $ID | tail -n 1 | tr -s ' ' | cut -d ' ' -f 3,4,5,6) l2=$(last -RF "$ID" | head -n 1 | tr -s ' ' | cut -d ' ' -f 3,6)

我grep为l1因为上次离开最后登录,但为了一致性,我只抓住最后一行。 last -RF删除主机( -R ),因为我们不感兴趣,并使时间更好一些( -F )。 tr修剪所有额外的空间,并剪切,由空白分隔,抓住日期。

我们要计算之间的时间,所以让我们改变日期时间字符串和减去:

a=$(date -ud "$l2" +"%s") b=$(date -ud "$l1" +"%s") d=$(( ab ))

最后让我们打印

echo "$ID's last login time is: $l1" echo "$ID's first login time is: $l2" echo "The time difference is $d seconds"

您的脚本似乎包含一些错误

/ bin / grep“^ $ ID”/ etc / passwd | 切-f5 -d:

这匹配/etc/passwd文件在开始( ^ )为uid,AFAIK,passwd文件始终以用户名开头,而不是uid。

也许你对用户ID是什么意思感到困惑; 在UNIX中,用户ID始终是指每个用户拥有的数字ID; 用户名是指您登录时输入的登录名。

无论如何,使用getent是更可靠的方法; 我们可以grep for :$uid:但是如果组ID与用户ID(在其他场景中)相同,则可能会中断。 getent也可以同时使用用户名用户名

另外,使用/bin/grep几乎总是一个坏主意; 在$PATH查找几乎总是更好(所以只需使用grep )。

l1=`last "$ID" | tail -n 1`

last期望一个用户名,而不是一个用户ID; 也许有一种味道也接受一个uid(?); 无论如何,使用用户名是更可靠的。

echo -n“时差是$(l1-l2)”

last的日期是字符串格式( Sat Nov 1 00:39 ); 你不能只是把它们作为整数,你首先需要用date来解析它们。

这是一个工作版本的样子。 我也做了一些可能对你有用的其他(小的)改进:

#!/bin/sh # Keep asking for the uid until *something* is entered while :; do read -p "Enter a user ID: " uid [ -n "$uid" ] && break done # Get line from /etc/passwd passwd=$(getent passwd "$uid") # Exit code was non-zero; the uid is unkNown if [ $? -ne 0 ]; then echo "User id '$uid' is unkNown" exit 1 fi # Get data from passwd username=$(echo "$passwd" | cut -d: -f1) realname=$(echo "$passwd" | cut -d: -f5) # Get info from last,strip last 2 lines since they're not useful for us. Use # ISO format so that date can parse them lastlog=$(last --time-format iso "$username" | head -n-2) # Get first & last line; we only need the date last_login=$(echo "$lastlog" | head -n1 | tr -s ' ' | cut -d ' ' -f 4) first_login=$(echo "$lastlog" | tail -n1 | tr -s ' ' | cut -d ' ' -f 4) # Parse dates with date,output time in seconds since 1-1-1970 ('epoch') diff=$(( $(date --date "$last_login" +%s) - $(date --date "$first_login" +%s) )) # Format the date diff_fmt=$(date --date @$diff +'%d days %H hours %M minutes %s seconds') # Output info echo "Found user $username ($realname) for userid $uid" echo "First recorded login: $first_login" echo "Last recorded login: $last_login" echo "Difference: $diff_fmt (total of $diff seconds)"

不幸的是,这只会在Linux系统上工作。 在所有 UNIX风格上工作需要更多的工作(shell脚本通常很难做便携)

示例输出

[~]% sh test.sh Enter a user ID: 1001 Found user martin (Martin Tournoij) for userid 1001 First recorded login: 2014-11-01T00:13:28+0100 Last recorded login: 2014-11-30T06:08:54+0100 Difference: 30 days 06 hours 55 minutes 26 seconds (total of 2526926 seconds)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐