谁 | awk Bash 脚本

谁 | awk Bash 脚本

尝试运行我的脚本时收到以下错误:

./watchuser: line 12: syntax error near unexpected token `echo'
./watchuser: line 12: `echo ("$User online since:" who| awk ' { print "User: " $1" -    Terminal: "$2" - Login @ " $3" "$4}') '

这是我的脚本,它应该以 Epoch 格式显示在线用户。

#!/bin/bash
for User in $(cd /home;ls --hide=lost+found)do
echo ("$User online since:" who| awk ' { print "User: " $1" - Terminal: "$2" - $
NOW=$ ( date +%s -d "Jan 1, 1980 00:00:01")
USR=$ ( who |awk ' {print $1, ":",$5,$6,$9 } ' | grep $U)
USRDATE=$(echo $USR | cut -d ":" -f2)
(( USRDATE = $(date --date "$USRDATE" +%s) / 86400 ))
(( NOW = NOW/86400 ))
(( DAYS = NOW - USRDATE ))
done

答案1

是的,你当然会收到错误。你的脚本很乱。我不明白你到底想做什么,但也许这个变体会让你知道你错在哪里:

#!/bin/bash

for User in $(cd /home;ls --hide=lost+found); do
    echo "$User online since:" 
    who | awk '{ print "\tUser: "$1" - Terminal: "$2" - "$3" "$4}'
    NOW=$(date +%s -d "Jan 1, 1980 00:00:01")
    USR=$(lastlog |awk ' {print $1, ":",$5,$6,$9 } ' | grep $User)
    USRDATE=$(echo $USR | cut -d ":" -f2)
    (( USRDATE = $(date --date "$USRDATE" +%s) / 86400 ))
    (( NOW = NOW/86400 ))
    (( DAYS = NOW - USRDATE ))
done

相关内容