2016-05-13 74 views
0

我有繼發性的組名列表,例如group_1 group_2.. group_n和用戶名如:user1驗證用戶輔助組

現在我需要做的

  1. 確保所有的羣體都存在

  2. 確保不存在額外的羣組

我嘗試使用id -nG user1 | grep <group_1> | grep <group_2> | .. | grep <group_ n>和評估exitcode,但只確保所需的組存在。我不知道如何驗證沒有額外的組(不在我的列表中的組)不存在。

回答

1

可以使用grep這樣的:

grep -oFf a_file_with_secondary_group_names_per_line 

的示例代碼如何可以達到你想要什麼:

#!/bin/bash 
user=username 
file=file_with_secondary_groups 
if [[ $(id -G "$user" |wc -w) == $(id -nG "$user" | grep -coFf "$file") ]]; then 
    echo "*All groups are present" 
    # i.e the number of group and the number of group matched is the same 
    if [[ $(id -G "$user" |wc -w) == $(grep -co '.' "$file") ]]; then 
    echo "*No extra groups" 
    # i.e the number of groups and the number of groups in the file are same 
    else 
    echo "-Extra groups present" 
    fi 
else 
    echo "-All groups are not present" 
fi 
+0

感謝@jahid但如何確保沒有多餘的羣組(未在我的列表中)是否存在? –

+0

@rdm_:結帳我的示例代碼。 – Jahid

+0

非常感謝@jahid我想'$(id -nG「$ user」| grep -coFf「$ file」)== $(id -G「$ user」| wc -w)'應該足以確保列出的組是存在的,但有些組由空格組成,例如'admin group',所以當我們執行'wc -w'時,這些將被計算爲2個獨立的組''admin'任何幫助,這將不勝感激,也將它可以在不存儲在文件中的情況下傳遞列表。謝謝。 –