Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.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 从bash脚本中的文本文件添加新用户_Linux_Bash - Fatal编程技术网

Linux 从bash脚本中的文本文件添加新用户

Linux 从bash脚本中的文本文件添加新用户,linux,bash,Linux,Bash,我不熟悉脚本编写,我不确定我的脚本有什么问题。我还没有添加groupadd或useradd命令来实际添加用户,但我会在这部分正常工作后再添加。到目前为止,我已经: !#/bin/bash if [$# -ne 0 ] then echo "usage: $0 < file' exit 1 fi first=cut -f 1 -d ',' user_list last=cut -f 2 -d ',' user_list lastl=cut -f 2 -d ',' user

我不熟悉脚本编写,我不确定我的脚本有什么问题。我还没有添加groupadd或useradd命令来实际添加用户,但我会在这部分正常工作后再添加。到目前为止,我已经:

!#/bin/bash

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

first=cut -f 1 -d ',' user_list
last=cut -f 2 -d ',' user_list
lastl=cut -f 2 -d ',' user_list | head -c 1

usern=$first $lastl | tr 'A-Z' 'a-z'
tname=$first $last
while read line; do
    echo "adding $tname : $usern\n"
done < user_text
#/bin/bash
如果[$#-ne 0]
然后
echo“用法:$0<文件”
出口1
fi
first=切割-f 1-d',“用户列表”
最后一个=切割-f 2-d',“用户列表”
lastl=切割-F2-d',“用户列表|头-C1
usern=$first$lastl | tr'A-Z''A-Z'
tname=$first$last
边读边做
echo“添加$tname:$usern\n”
完成

输出看起来应该像添加Jet Black:jetb,但是它有点到处都是。任何关于我做错了什么的帮助或提示都会有很大帮助。

你可以通过阅读
IFS
变量在分词中的作用,然后写下这样的内容来简化它:

while IFS=, read first last
do
  usern=$(echo "${first}${last:0:1}" | tr [:upper:] [:lower:])
  tname="${first} ${last}"
  echo "adding ${tname} : ${usern}"
done
usern=`echo ${first}${lastl} | tr 'A-Z' 'a-z'`

还有一些其他需要研究的东西-子字符串扩展(
${last:0:1}
),
tr
[:upper://code>)的字符类(
[:upper://code>),等等),从命令捕获输出(
$(…)
)。

这是脚本的语法正确版本:

#!/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`
        echo "adding $tname : $usern\n"
done < user_text

还有很多改进要做,可能像,但我认为你最好的第一步是理解你的语法错误。

你的语法几乎有一半是错误的。谢谢,我不知道IFS。我已经阅读了一段时间,它真的很有帮助。