从bash脚本创建用户帐户

时间:2018-01-23 14:19:32

标签: shell unix scripting

这是我必须从文本文件创建用户帐户的代码。用户名来自文本文件,我想将密码设置为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

3 个答案:

答案 0 :(得分:1)

有几个问题:

  • 您的for循环被破坏(do被错误放置)
  • passwd需要stdin input
  • 的特殊选项

这应该按照您的要求执行:

#!/usr/bin/env bash
for i in $(cat unix_tuesday.txt); do
   password=unixlab
   adduser $i 
   echo $password | passwd $i --stdin
done

另请注意adduseruseradd之间有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作业位于fordo之间。将其移至开头会产生一组新的错误:

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)返回的加密密码。默认设置是禁用密码。

     

注意:建议不要使用此选项,因为密码(或   列出流程的用户将看到加密密码。