我正在編寫一個shell腳本,用於查找文件「/etc/user.deny
」中的用戶列表,並且如果多次遇到用戶名,程序將報告一條出口錯誤消息。到目前爲止,我有:Shell腳本拒絕用戶登錄多次
while read user
do
#something
done < ./etc/user.deny
我該如何比較每個用戶名。有沒有簡單的方法來做到這一點?
我正在編寫一個shell腳本,用於查找文件「/etc/user.deny
」中的用戶列表,並且如果多次遇到用戶名,程序將報告一條出口錯誤消息。到目前爲止,我有:Shell腳本拒絕用戶登錄多次
while read user
do
#something
done < ./etc/user.deny
我該如何比較每個用戶名。有沒有簡單的方法來做到這一點?
我覺得他要殺死所有重複登錄
#!/bin/bash
#creat a sorted list
who -u | sort -k1,5 -r > userlogins.txt
#
user=""
olduser=""
# read userlist
cat userlogins.txt | while read line; do
# [0] of array is the username
# [1] is terminal
# [3] is login date
# [4] is PID
user=($line)
# echo ${user[0]}
#
# kill terminal if user has more than one login
if [$olduser = ${user[0]}]; then
echo ${user[0]} duplicate
pkill -t ${user[1]}
fi
# remember old user
olduser="${user[0]}"
done
不幸的是我有沒有可能在目前來測試這個腳本。
既然你提到了重複登錄,我想OP會拒絕後續登錄,如果用戶已經登錄。那麼也許它會是'/ etc/profile'執行的腳本。 – alvits
@亞歷山大Baltasar嗨,我試了腳本,但我錯誤第17行:[:失蹤]''任何想法是什麼錯,事先感謝。 =) –
'/ etc/user.deny'的格式是什麼?可能排序和排序就足夠了 – fedorqui
不幸的是,這是一個實驗室,並且該文件不是真實的,但我相信該文件將只在每行只有一個用戶名,這就是它 – coopwatts
嘗試'sort/tmp/test | uniq -c | sed -n's/^ [^ 0-9] \ + [2-9] \ + \(。* \)$/\ 1/p''。 – alvits