简体   繁体   English

我的脚本无法正常工作

[英]My script does not work properly

Problem: I need to create a file where users and passwords are stored. Problem:我需要创建一个存储用户和密码的文件。 But my script only saves the password of user1. 但是我的脚本只保存了user1的密码。

Example: I explain myself better with an example. Example:我用一个例子更好地解释自己。 When the two users are created with random password, my script assigns the same password on both users. 当使用随机密码创建两个用户时,我的脚本在两个用户上分配了相同的密码。

user1:Eehei5oo8ohz:/home/user1:/bin/bash user1:Eehei5oo8ohz:/ home / user1:/ bin / bash

user2:Eehei5oo8ohz:/home/user2:/bin/bash user2:Eehei5oo8ohz:/ home / user2:/ bin / bash

When the result of my script should be something like this: 当我的脚本结果应该是这样的时候:

user1:Eehei5oo8ohz:/home/user1:/bin/bash user1:Eehei5oo8ohz:/ home / user1:/ bin / bash

user2:Kln2149sdpja:/home/user2:/bin/bash user2:Kln2149sdpja:/ home / user2:/ bin / bash

My script: This is the script I have used: My script:这是我使用的脚本:

#!/bin/bash
##Checking if you are root.##
if [ `id -u` -ne 0 ]
then
    echo "you dont are a root user."
    exit 1
fi

x=`pwgen 12 1`

for i in {1..2}
do
    echo "user$i:@:/home/user$i:/bin/bash" >> users.txt
done

for j in $x
do
    sed -i "s/@/$j/" users.txt
done


newusers users.txt

users=`cat users.txt`
login=`echo $i | cut -d: -f1` #username
pass=`cat pass.txt | tr " " _`
password1=`echo $i | cut -d: -f2` #password

for in $users
do
      echo "$login:$password1" | chpasswd -m
done

rm users.txt
rm pass.txt

I hope I have explained correctly and appreciate all the help. 我希望我已经正确解释了,感谢所有帮助。

You could simplify your script and adapt it as follows (I'm referring to the first part only): 您可以简化脚本并将其修改如下(我仅指的是第一部分):

  #!/bin/bash
  # checking if you are root.##
  if [ `id -u` -ne 0 ]
  then
      echo "you are not the root user!"
      exit 1
  fi

  for i in {1..2}
  do
      x=`pwgen 12 1`
      echo "user$i:$x:/home/user$i:/bin/bash" >> users.txt
  done

This will create a file users.txt like the one bellow: 这将创建一个文件user.txt,就像下面这样:

user1:ohng3uxohYi9:/home/user1:/bin/bash
user2:Gah5kiehaemi:/home/user2:/bin/bash

I see no point in creating a file with users and then replacing the @ sign with a generated password since you can do that from the start! 我看不到用用户创建文件然后将@符号替换为生成的密码没有意义,因为您可以从一开始就这样做!

You can simplify this script by getting rid of sed editing, eg change: 您可以通过取消sed编辑(例如更改)来简化此脚本:

for i in {1..2}
do
    echo "user$i:@:/home/user$i:/bin/bash" >> users.txt
done

for j in $x
do
    sed -i "s/@/$j/" users.txt
done

to

   pass=`cat pass.txt`
   i=1
   for pw in $pass
   do
          echo "user$i:$pw:/home/user$i:/bin/bash" >> users.txt
          ((i++))
   done

This way you can get user numbers and passwords in one go and you are guaranteed to get as many usernames as passwords. 这样一来,您可以一次性获得用户名和密码,并且可以确保获得与密码一样多的用户名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM