【问题标题】:reading from 2 files in a while loop BASH SCRIPTING在 while 循环中读取 2 个文件 BASH SCRIPTING
【发布时间】:2013-10-12 04:36:34
【问题描述】:

我有 2 个文件,其中一个名为 Group.csv 包含

thisisgroupone

还有一个叫Users.csv

Testuser1,Testuser2,TestUser3

到目前为止这是我的脚本:

# this part of the script will now add the created users into the created group!
while IFS=, read col1 col2 col3
do
        usermod  -g $READ GROUP.CSV$ $col1
        usermod  -g $READ GROUP.CSV$ $col2
        usermod  -g $READ GROUP.CSV$ $col3

    done < Users.csv

echo "Users now moved to new group sepcifyed in Group.csv"
  • 我正在想办法同时读取这两个文件以完成此语句。

谢谢=D

【问题讨论】:

  • Group.csv 是否仅包含 1 个条目?

标签: bash csv


【解决方案1】:

一种技术是:

while IFS=, read col1 col2 col3
do
        read -u 3 GROUP
        usermod  -g $GROUP $col1
        usermod  -g $GROUP $col2
        usermod  -g $GROUP $col3
done < Users.csv 3< Group.csv

while IFS=, read col1 col2 col3 && read -u 3 GROUP
do
        usermod  -g $GROUP $col1
        usermod  -g $GROUP $col2
        usermod  -g $GROUP $col3 
done < Users.csv 3< Group.csv

以上都不能确保文件具有相同的行数,您可能需要检查一下。

【讨论】:

    【解决方案2】:

    paste -d' ' Group.csv Users.csv 会生成如下输出:

    Testgroup1 Testuser1 Testuser2 Testuser3
    Testgroup2 Testuser4 Testuser5 Testuser6
    ...
    

    因此您可以将其输出发送到您的脚本,它可以从单个文件中读取行,其中每行的第一个字段是组,其余的是用户。

    您可以使用|paste 的输出直接发送到您的脚本。或者您可以创建一个临时文件paste -d' ' Group.csv Users.csv &gt; temp.csv,然后使用&lt; temp.csv 读取该文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-24
      • 1970-01-01
      • 2013-07-08
      • 2015-09-13
      • 2012-03-27
      • 2019-05-06
      • 2018-04-17
      相关资源
      最近更新 更多