【问题标题】:Zenity: HOW TO create a user and change its password?Zenity:如何创建用户并更改其密码?
【发布时间】:2020-02-09 13:18:56
【问题描述】:

我是 zenity 的新手,想创建一个 GUI 来创建新用户和密码。我的代码是这样的:

#!/bin/bash
zenity --info --text='Welcome to Account Creation Wizard'
zenity --entry --text='What do you want to set your username as?' > username
zenity --password > test
cat username >> username1
cat username >> username1
cat test >> test1
cat test >> test1
useradd -m < username1
passwd < username1 < test1
rm test test1
rm username username1
exit

但它不起作用。在终端中,我得到了这个:

[root@archevaris Desktop]# ./UserCreator.sh
Usage: useradd [options] LOGIN
       useradd -D
       useradd -D [options]

Options:
      --badnames                do not check for bad names
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
      --btrfs-subvolume-home    use BTRFS subvolume for home directory
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       home directory of the new account
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
                                account
  -G, --groups GROUPS           list of supplementary groups of the new
                                account
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                                the user
  -o, --non-unique              allow to create users with duplicate
                                (non-unique) UID
  -p, --password PASSWORD       encrypted password of the new account
  -r, --system                  create a system account
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -s, --shell SHELL             login shell of the new account
  -u, --uid UID                 user ID of the new account
  -U, --user-group              create a group with the same name as the user

New password: Retype new password: passwd: password updated successfully
[root@archevaris Desktop]#

实际上它所做的是更改了我的 root 密码:P(这个 bash 脚本执行起来有点危险,但幸运的是我是在 VM 快照中完成的)

那么,如何解决这个问题?我应该对代码进行哪些更改?

【问题讨论】:

  • 您可能应该查看手册页:如果这些命令没有从标准输入中读取,则重定向将不起作用。
  • 我认为为 shell 脚本实现 GUI 并不难(以前没有尝试过)尤其是使用 zenity。但我不知道该怎么做。 Forum link 哦,那是我在那个论坛。我在那里问了一个问题,但我还没有得到答复..

标签: linux bash shell root zenity


【解决方案1】:

要通过zenity 获取用户名和密码,这将起作用:

export INFO=$(zenity --password --username)

$INFO 将被管道分割,因此如果您的用户名是“Fred”,密码是“123abc”,$INFO 将如下所示:

Fred|123abc

从这里,只需使用cut来分隔数据:

USERNAME=$(echo $INFO | cut -d"|" -f1)
PASSWORD=$(echo $INFO | cut -d"|" -f2)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-06-27
    • 1970-01-01
    • 2020-05-22
    • 1970-01-01
    • 2012-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多