gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:05:23 26 4
gpt4 key购买 nike

我是脚本编写新手,我不确定我的脚本出了什么问题。我还没有添加 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

输出应该类似于添加 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

其中还有一些需要研究的内容 - 子字符串扩展 (${last:0:1})、tr 的字符类([:upper:] 等)、捕获命令的输出 ($(...))。

关于linux - 从 bash 脚本中的文本文件添加新用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15393599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com