使用脚本生成一堆新用户以生成newusers命令所需的输出

问题描述 投票:1回答:2

我正在编写一个脚本,我想在数字1到15之间进行迭代,但希望它显示为01 02 03 ... 13 14 15.基本上我要做的是使用newusers命令添加15个用户并使用此脚本为<命令。新用户需要采用以下格式:

pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell

基本上,当我使用arguments =运行脚本时,它应该是这样的

cstuser01:EzVlK9Je8JvfQump:1001:1001:CST8177 user:/home/cstuser01:/bin/bash
cstuser02:EsKOfvhgnWpiBT6c:1002:1002:CST8177 user:/home/cstuser02:/bin/bash
cstuser03:qzQuR5vRgxdzY6dq:1003:1003:CST8177 user:/home/cstuser03:/bin/bash

我得到了大部分工作,但我收到以下错误:

./15users.sh:57:./15users.sh:非法数字:08

这是我到目前为止的脚本(我拿出了几个错误检查部分)=

#!/usr/bin/env bash
PATH=/bin:/usr/bin ; export PATH
umask=022

#num=1 (this variable is needed depending on which loop I use below)
user=$prefix"user"
uid=1001
gid=$uid
home=/home/$user
shell=/bin/bash
pad=printf "%02d\n"

#echo "pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell"
#PASSWD=$(openssl rand -base64 12)

我最初有这个,但遇到了一些问题:

while [ $NUM -le 15 ] ; do
       if [ $NUM -lt 10 ] ; then
               NUM=0$NUM
       fi
       echo "$USER$NUM:$(openssl rand -base64 12):$UID:$GID:$GECO:$HOME$NUM:$SHELL"
       UID=$(( UID + 1 ))
       GID=$(( GID + 1 ))
       NUM=$(( NUM + 1 ))
done

我的一个朋友建议这样做,它完全没问题。但我正试图将来证明这件事。如果我要添加100或1,000个用户,该怎么办?

for NUM in 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 ; do
       echo "$USER$NUM:$(openssl rand -base64 12):$UID:$GID:$GECO:$HOME$NUM:$SHELL"
done

这不起作用:

for num in {01..15} ; do
    i=09
    echo "$(( 10#$num + 1 ))"
    10
done

然后我尝试了这个但是没有填充0或增加uid或gid =

for (( num=1; num<=15; num++ )) ; do
     echo "$user$pad$num:$(openssl rand -base64 12):$uid:$gid:$geco:$home$num:$shell"
done

我试过这个,但seq垂直打印不是水平的:

#iterate=$(seq -w 1 15)
for $iterate ; do
       echo "$user$num:$(openssl rand -base64 12):$uid:$gid:$geco:$home$num:$shell"
done
linux bash scripting centos7
2个回答
1
投票

bash支撑扩展应该做的工作!

$ for i in {01..15}; do echo "user$i"; done

user01
user02
user03
user04
user05
user06
user07
user08
user09
user10
user11
user12
user13
user14
user15

0
投票

我修好了,谢谢#Inian,在这里。我有一个更挑剔的事情,我想问一下。

#!/usr/bin/env bash
PATH=/bin:/usr/bin ; export PATH
umask=022

我想对下面的错误检查进行修正。输出应该看起来像这样=

cstuser01:EzVlK9Je8JvfQump:1001:1001:CST8177 user:/home/cstuser01:/bin/bash
cstuser02:EsKOfvhgnWpiBT6c:1002:1002:CST8177 user:/home/cstuser02:/bin/bash
cstuser03:qzQuR5vRgxdzY6dq:1003:1003:CST8177 user:/home/cstuser03:/bin/bash

如果我在下面阻止空参数,你如何允许Gecos字段中的空格,即CST8177用户? Gecos应该基本上是你的全名,所以很明显它会有空格。但是当我尝试像这样运行时:

./15users cat“Feline user”(例如)它会输出我为错误检查创建的错误消息。

#This if statements checks if you passed anything other than 2 arguments to the script file in the command-line

if [ "$#" -ne 2 ] ; then
    echo 1>&2 "$0: Expecting only two arguments to be passed to the script. Found: $# arguments, which was or were ($*)"
    echo 1>&2 "Usage: $0 Username-prefix GECOS/Description"
    exit 2
fi

#This case statement checks to ensure that all arguments passed to the command line are letters only and contain no special characters or blank arguments

还有一种方法可以将下面这两个case语句变成一个,因为它们正在检查相同事物的第一个和第二个参数吗?

case "$1" in
'' ) echo 1>&2 "$0: Expecting two arguments that use alphabetic characters. Found: $# arguments, which was or were ($*)"
    echo 1>&2 "Usage: $0 Username-prefix GECOS/Description (Cannot use blank arguments)" ;;
*[![:alpha:]]* ) echo 1>&2 "$0: Expecting two arguments that use only letters. Found: $# arguments, which was or were ($*)"
    echo 1>&2 "Usage: $0 Username-prefix GECOS/Description (No numbers, spaces, or special characters)" ;;
* ) prefix=$1  ;;
esac

#This case statement checks to ensure that all arguments passed to the command line are letters only and contain no special characters or blank arguments

case "$2" in
'' ) echo 1>&2 "$0: Expecting two arguments that use alphabetic characters. Found: $# arguments, which was or were ($*)"
    echo 1>&2 "Usage: $0 Username-prefix GECOS/Description (Cannot use blank arguments)" ;;
*[![:alpha:]]* ) echo 1>&2 "$0: Expecting two arguments that use only letters. Found: $# arguments, which was or were ($*)"
    echo 1>&2 "Usage: $0 Username-prefix GECOS/Description (No numbers, spaces, or special characters)" ;;
* ) geco=$2 ;;
esac

num=1
user=$prefix"user"
uid=1001
gid=$uid
home=/home/$user
shell=/bin/bash

#echo "pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell"
#PASSWD=$(openssl rand -base64 12)

for (( num=1; num<=30; num++ )) ; do
    echo "$user$(printf "%02d\n" $num):$(openssl rand -base64 12):$uid:$gid:$geco:$home$(printf "%02d\n" $num):$shell"
    uid=$(( uid + 1 ))
    gid=$(( gid + 1 ))
done
© www.soinside.com 2019 - 2024. All rights reserved.