2015-10-26 84 views
-1

我正在編寫一個shell腳本,用於查找文件「/etc/user.deny」中的用戶列表,並且如果多次遇到用戶名,程序將報告一條出口錯誤消息。到目前爲止,我有:Shell腳本拒絕用戶登錄多次

while read user 
do 
    #something 
done < ./etc/user.deny 

我該如何比較每個用戶名。有沒有簡單的方法來做到這一點?

+1

'/ etc/user.deny'的格式是什麼?可能排序和排序就足夠了 – fedorqui

+0

不幸的是,這是一個實驗室,並且該文件不是真實的,但我相信該文件將只在每行只有一個用戶名,這就是它 – coopwatts

+0

嘗試'sort/tmp/test | uniq -c | sed -n's/^ [^ 0-9] \ + [2-9] \ + \(。* \)$/\ 1/p''。 – alvits

回答

0

我覺得他要殺死所有重複登錄

#!/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 

不幸的是我有沒有可能在目前來測試這個腳本。

+0

既然你提到了重複登錄,我想OP會拒絕後續登錄,如果用戶已經登錄。那麼也許它會是'/ etc/profile'執行的腳本。 – alvits

+0

@亞歷山大Baltasar嗨,我試了腳本,但我錯誤第17行:[:失蹤]''任何想法是什麼錯,事先感謝。 =) –

0

如果您想在一個線出現不止一次地報告,那麼你可能想單獨使用awk

awk 'seen[$0]++ {print "seen more than once: ", $0; exit}' file 

在陣列seen[]我們存儲時代的每一行出現。如果出現多次,它會觸發printexit聲明。否則,它靜靜地結束。

+0

這項多次出現多次用戶的作品是否會多次出現,或者會在首次出現多次用戶時退出? – coopwatts

+0

一旦找到第一個副本,就會退出。這不是要求嗎? – fedorqui