在多个文件中搜索用户名,如果使用bash脚本不存在,则进行打印

问题描述 投票:0回答:1

我有一个由多个用户组成的文件,需要将其与多个文件进行比较,如果文件名中的文件中没有任何特定的用户,则需要进行打印。

#!/bin/bash
awk '{print $1}' $1 | while read -r line; do
if ! grep -q "$line" *.txt;
then
echo "$line User doesn't exist"
fi
done

在上面的脚本中,将user_list文件作为$ 1传递,并且能够为单个目标文件找到用户,但是对于多个文件却失败。

文件内容:

user_list:
Johnny
Stella
Larry
Jack

One of the multiple files contents:
root:x:0:0:root:/root:/bin/bash
Stella:x:1:1:Admin:/bin:/bin/bash
Jack:x:2:2:admin:/sbin:/bin/bash

用法:

./myscript user_list.txt

所需的输出:

File1:
Stella doesn't exist
Jack doesn't exist

File2:
Larry doesn't exist
Johnny doesn't exist

这里有没有建议为带有打印文件名标题的多个文件实现它?

bash shell
1个回答
0
投票

使用for循环迭代每个文件并分别为每个文件执行代码。

#!/bin/bash
for f in *.txt; do
    echo $f:
    awk '{print $1}' $1 | while read -r line; do
        if ! grep -q "$line" $f
        then
            echo "$line doesn't exist"
        fi
    done
    echo 
done
© www.soinside.com 2019 - 2024. All rights reserved.