我有一個檔案中的用戶串列(美國、比利時、印度、斯里蘭卡、不丹)我需要在每臺機器上運行一個腳本來檢查上述用戶是否存在,如果不存在,則創建這些用戶,用戶在檔案中駐留在 /etc/users.txt
users.txt的內容
美國
比利時
印度
斯里蘭卡
不丹
我試過這個,但它沒有檢查我的檔案串列中的用戶。
#!/bin/sh
if grep -q "^$1:" /etc/users.txt ;then
echo exists
else
echo FALSE
fi
uj5u.com熱心網友回復:
你沒有打電話給 users.txt,你只是打電話給 users
你的 grep 中有一堆不正確的正則運算式
(可選)你應該在 grep 上使用 -i 來接受小寫字母
#!/bin/sh if grep -i -q "$1" /etc/users.txt ;then echo exists else echo FALSE fi
uj5u.com熱心網友回復:
使用 while 回圈從檔案中讀取:
while read -r line;
do
if id "$line" >/dev/null 2>&1; then
echo "$line exists"
else
echo "$line does not exist"
sudo useradd "$line"
fi
done < file
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/437424.html
上一篇:如何在ps1腳本中終止終端會話
