Linux Shell脚本中的动态注释更改

时间:2014-04-22 19:47:05

标签: linux shell unix

我有一个/ etc / passwd文件,其中包含三个新用户methun,salam和kalam,并在/ methunfiles / mypractice / myfile / passwd中包含另一个文件,其中包含输入为methun:xxx salam:firstboy kalam:secondboy in a拖柱。第一列包含methun,salam,kalam和第二列包含xxx,firstboy,secondboy。现在我的工作是将/ etc / passwd文件的第一列与/ methunfiles / mypractice / myfile / passwd的第一个colimn的第一列相匹配。如果找到任何匹配项,则插入/ etc / passwd的注释字段 在两个第一列中找到同名的/ methunfiles / mypractice / myfile / passwd文件的第二列。我尝试使用以下代码但没有找到输出。我想在这里使用循环。有人帮忙吗?我的输出应该像methun:x:501:502:xxx:......,salam:x:439:439:firstboy ......等等。

        mainUser=cat /etc/passwd | awk -F ':' '{print $1}'
        modifyUser=cat /methunfiles/mypractice/myfile/passwd | awk -F ':' '{print $1}'
        modifyComment=cat /methunfiles/mypractice/myfile/passwd | awk -F ':' '{print $2}'
        for muser in $mainUser
          do
             for moduser in $modifyUser
               do
                  for mcomment in $modifyComment
                   do
                    if ["$muser" == "$moduser" ]
                      chmod -c "$mcomment" $muser
                    fi
                done
            done
          done

1 个答案:

答案 0 :(得分:1)

join命令就是您所需要的。

f1=/etc/passwd
f2=/methunfiles/mypractice/myfile/passwd

join -t: -j1 -o 2.1,2.2 <(sort -t: -k1,1 $f1) <(sort -t: -k1,1 $f2) |
while IFS=: read user new_comment; do
    if usermod -c "$new_comment" $user; then
        getent passwd $user
    else
        echo "could not modify comment field for $user"
    fi
done