这是我必须从文本文件创建用户帐户的代码。用户名来自文本文件,我想将密码设置为unix lab。但是,这个脚本似乎不起作用。
#!/bin/sh
for i in `cat unix_tuesday.txt`
password = "unixlab"
do
echo "adduser $i"
adduser $i
echo -e "$password" | passwd $password
done
答案 0 :(得分:1)
有几个问题:
do
被错误放置)这应该按照您的要求执行:
#!/usr/bin/env bash
for i in $(cat unix_tuesday.txt); do
password=unixlab
adduser $i
echo $password | passwd $i --stdin
done
另请注意adduser
和useradd
之间有difference。
答案 1 :(得分:0)
通过ShellCheck报告执行此操作:
Line 2:
for i in `cat unix_tuesday.txt`
^-- SC1073: Couldn't parse this for loop. Fix to allow more checks.
Line 3:
password = "unixlab"
^-- SC1058: Expected 'do'.
^-- SC1072: Expected 'do'. Fix any mentioned problems and try again.
password
作业位于for
和do
之间。将其移至开头会产生一组新的错误:
Line 2:
password = "unixlab"
^-- SC1068: Don't put spaces around the = in assignments.
Line 3:
for i in `cat unix_tuesday.txt`
^-- SC2013: To read lines rather than words, pipe/redirect to a 'while read' loop.
^-- SC2006: Use $(..) instead of legacy `..`.
Line 6:
adduser $i
^-- SC2086: Double quote to prevent globbing and word splitting.
Line 7:
echo -e "$password" | passwd $password
^-- SC2039: In POSIX sh, echo flags are undefined.
答案 2 :(得分:0)
man adduser
-p, - password PASSWORD crypt(3)返回的加密密码。默认设置是禁用密码。
注意:建议不要使用此选项,因为密码(或 列出流程的用户将看到加密密码。