我有一个txt
这种格式的:
dfs /home/dfs ashik karki
所以现在我需要一个 bash 脚本来读取文本文件中的每个单词,我要做的是自动添加用户并生成随机密码。这里的用户是 dfs,主目录是 /home/dfs,ashik karki 作为注释。那么我如何通过编写 bash 脚本来自动化这个过程呢?谢谢!
答案1
尝试这个,
# Login as root if necessary
sudo su
# Create your own adduser function to automate the process
adduser2() {
# add the user
adduser --home $2 --disabled-login $1
# Create a password (change 10 to the password length you want)
local pass=$(openssl rand -base64 10)
# Change the password
echo -e "$pass\n$pass" | passwd $1
# Print information
echo "Password for user $1: $pass"
shift 2
echo "Comment: $@"
}
# Loop through lines in your file and execute the adduser2 function with the line as argument.
while IFS= read -r l; do
adduser2 $l;
done < file.txt
笔记: