
我有 2 個文字文件,users.txt 包含 1000 個使用者的列表,groups.txt 包含 50 個群組的列表。我想執行一個指令,將 30 個使用者加入到每個群組(例如:使用者 1-30 到群組 1,使用者 31-60 到群組 2,等等)。這樣做最實用的方法是什麼?
我想我可以創建 50 個新文字文件,每個文件包含 30 個用戶的列表,並運行 50 個 bash 腳本,但我覺得有比這更好的方法。
編輯:這是我到目前為止所擁有的:
for i in `cat users.txt` ; do useradd $i; echo Pass$i | passwd $i -- stdin; done
for i in `cat groups.txt ; do groupadd $i; done
答案1
雖然這只是一個練習,你應該自己做,但這裡有一個解決方案:你真的應該自己做,否則就傻了。
使用一些循環和簡單的測試:
while read group
do
i=30
while read user
do
echo $user:$group
let "i--"
if [ $i -eq 0 ]
then
break
fi
done < /tmp/users.txt
sed -i -e '1,30d' /tmp/users.txt
done < groups.txt > userswithgroup.txt
請注意,這將清空您的 users.txt 檔案。所以最好複印一份。
然後你還需要新增用戶;但目前這並不複雜。