阅读每个用逗号分隔的单词 [英] Read each word separated by comma

查看:61
本文介绍了阅读每个用逗号分隔的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

截至目前,我正在尝试访问每个单词,第一个单词是 useradd ,第二个单词是 groupadd ,但是我正在使用,"

As of right now, I'm trying to reach each word, with the first word being useradd and the second word being groupadd, but I'm having a bit of trouble with the ","

这是我的 username.txt 文件:

jason,staff
henson,visitor

这是我当前的bash脚本:

This is my current bash script:

#!/bin/bash

username="username2.txt"

while read username group; do
  sudo useradd $username;
  sudo groupadd $group;
done < $username

推荐答案

将字段分隔符 IFS 更改为逗号.

Change the field separator IFS to comma.

filename="username2.txt"
while IFS=, read -r username group
do
    sudo useradd "$username"
    sudo groupadd "$group"
done < "$filename"

并且不要为文件和迭代变量重用变量 $ username .

And don't reuse the variable $username for both the file and your iteration variable.

这篇关于阅读每个用逗号分隔的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆