Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何向已使用的用户名添加递增的数字_Linux_Bash_Scripting - Fatal编程技术网

Linux 如何向已使用的用户名添加递增的数字

Linux 如何向已使用的用户名添加递增的数字,linux,bash,scripting,Linux,Bash,Scripting,我试图编写一个脚本,从一个文件中创建用户名,但是如果有两个相同的用户名,则后一个用户名每次递增1。我想我搞砸的地方是if/else声明,但如果有任何帮助,我们将不胜感激 #!/bin/bash if [ $# -ne 0 ]; then echo "usage: $0 < file" exit 1 fi while read user_list; do first=echo $user_list | cut -f 1 -d ',' `last=`echo $user_l

我试图编写一个脚本,从一个文件中创建用户名,但是如果有两个相同的用户名,则后一个用户名每次递增1。我想我搞砸的地方是if/else声明,但如果有任何帮助,我们将不胜感激

#!/bin/bash

if [ $# -ne 0 ]; then
    echo "usage: $0 < file" exit 1
fi

while read user_list; do
    first=echo $user_list | cut -f 1 -d ',' `last=`echo $user_list | cut -f 2 -d ','
    lastl=echo $last | head -c 1 usern=echo $first $lastl | tr 'A-Z' 'a-z'
    tname=echo $first $last 
    groupadd $usern useradd -c "${first}${last}" -d /home/${usern} -g $usern -m -s /bin/bash $usern
    passwd=$(cat /dev/urandom |tr -dc 'A-Za-z0-9' | head -c 8)
    cat /tmp/pw.tmp | /usr/bin/passwd --stdin ${usern} > /dev/null
    echo "adding $tname : $usern, $passwd"
done

original_username=$usern
username_digit=0

while read line; do
    grep -w $usern /etc/passwd
    if [[ ! usern /etc/passwd]]; then
        username_digit=$((username_digit +1))
        usern=$((original_username + $username_digit))
    else
        usern+$original_username
    fi
done
#/bin/bash
如果[$#-ne 0];然后
回显“用法:$0<文件”退出1
fi
在读取用户列表时;做
first=echo$user_list | cut-f1-d','last='echo$user_list | cut-f2-d','
lastl=echo$last | head-c1 usern=echo$first$lastl | tr'A-Z'A-Z'
tname=echo$first$last
groupadd$usern useradd-c“${first}${last}”-d/home/${usern}-g$usern-m-s/bin/bash$usern
passwd=$(cat/dev/urandom | tr-dc'A-Za-z0-9'|头-c 8)
cat/tmp/pw.tmp |/usr/bin/passwd--stdin${usern}>/dev/null
echo“添加$tname:$usern,$passwd”
完成
原始用户名=$usern
用户名\数字=0
读行时;做
grep-w$usern/etc/passwd
如果[[!usern/etc/passwd]];然后
用户名\数字=$((用户名\数字+1))
usern=$((原始用户名+用户名数字))
其他的
usern+$original\u用户名
fi
完成

你能用你想使用的用户名的简短列表展开吗;您希望输出是什么;输出结果是什么?-1这个脚本很混乱,真的。。。这就像是虚构的代码。答案没有得到很好的解释/形成。背虱不会筑巢。使用
$()
,这样做。2.
first=echo…
毫无意义。3.在所有地方都无用地使用
cat
。使用
cmd
代替
cat file | cmd
。4.获取一本关于shell脚本的好书,并阅读您的shell手册页:-)@floris例如,文本文件中有Jet Black这个名称。输出应该类似于添加黑色、黑色、黑色(随机密码)。
并且输出完美,因此我知道上半部分工作正常。但是,如果我在文件中有另一个名为jetbrown的用户,用户名仍然希望是jetb,但由于它已经被占用,因此必须添加一个数字,因此它将是jetb1。如果出现更多的jetb用户名,则必须是jetb2、jetb3等。对于错误的代码,我几周前才开始学习。
$usern
/etc/passwd
中的其他字段(如root的主目录或登录shell)匹配时会发生什么?您应该更具体地锚定搜索。
if grep -q "^${usern}:" /etc/passwd; then
    username_digit=$((username_digit++))
    usern="$original_username$username_digit"
(...)