简体   繁体   English

BASH grep脚本

[英]BASH grep script

I am trying to work on a bash script that checks for a username in the argument of the script and then outputs the relevant lines from the /etc/passwd and /etc/group files (not the /etc/shadow file). 我正在尝试使用bash脚本来检查脚本参数中的用户名,然后输出/ etc / passwd和/ etc / group文件(而不是/ etc / shadow文件)中的相关行。 Currently, I am utilizing a if then else loop to check the contents of the /etc/* directory and output the relevant information. 目前,我正在使用if then else循环来检查/ etc / *目录的内容并输出相关信息。 My intention was to output simple text line if a match user is not found in the two files, thus a null value. 如果在两个文件中找不到匹配用户,我的意图是输出简单的文本行,因此为空值。 However, it is outputting information that is totally incorrect for what I am looking for.As a new user to BASH, and linux is general, I am sure there are some glaring issues right away. 但是,它输出的信息对我正在寻找的东西完全不正确。作为BASH的新用户,linux是通用的,我相信它会立即出现一些明显的问题。 However, I am trying to learn. 但是,我正在努力学习。

Any help with the code of my script or a point in the right direction would be greatly appreciated. 任何有关我的脚本代码的帮助或正确方向的一点将不胜感激。 Thank you. 谢谢。

#! /bin/bash                                                              


USERLOOK='grep -h $USERID ~/etc/* | grep :x:'                       

grep $1 ~/etc/*                                                     
if [ -z $1 ]; then                                                        
    echo "User not found."                                            
else                                                                      
    echo "$USERLOOK"                                                  
fi                                                                        
exit 0

Finds any lines in /etc/passwd or /etc/group that contain the inputted username: /etc/passwd/etc/group中查找包含输入用户名的任何行:

#!/bin/bash

USERLOOK=$(grep -h "$1" /etc/passwd /etc/group)

if [ -z "$1" ] || [ -z "${USERLOOK}" ]; then
    echo "User not found."
else
    echo "$USERLOOK"
fi

I want to have the script function where I input ./script user_to_check. 我希望在输入./script user_to_check的地方使用脚本功能。 If the username is found, I want to output all lines where it was found... However, if the username was not found, I wanted to echo that. 如果找到用户名,我想输出找到它的所有行...但是,如果找不到用户名,我想回应一下。

It can be as simple as 它可以很简单

#!/bin/bash
grep "^${1}:" /etc/passwd   /etc/group
[ $? -ne 0 ] && echo "User : ${1} not found"

As the user name appears in the beginning in both /etc/passwd & /etc/group we placed a ^ in grep to match stuff at beginning and by tradition a : appears just after the username. 当用户名出现在/etc/passwd/etc/group的开头时,我们在grep中放置一个^来匹配开头的东西,按照传统a :出现在用户名之后。

Run the script as 运行脚本为

./script 'username'

Stop, you're thinking about this all wrong. 停下来,你在想这一切都错了。

A UNIX shell is an environment from which to call UNIX tools with a language to sequence those calls, that is all. UNIX shell是一个环境,可以使用一种语言调用UNIX工具来对这些调用进行排序,这就是全部。 The general purpose UNIX tool to manipulate text is awk. 用于操作文本的通用UNIX工具是awk。 So if you need to look for text in a file and have control logic to do anything with it, that should be an awk script, not a shell script. 因此,如果您需要在文件中查找文本并让控制逻辑对其执行任何操作,那么应该是awk脚本,而不是shell脚本。 Shells role is to just call awk, something like this: shell的作用是调用awk,如下所示:

awk -v user="$1" '
$0 ~ user { line = $0 }
END { print (line != "" ? line : "User not found") }
' /etc/passwd /etc/group

but note that it's trivial with awk to focus the search on just one field, even a different field for each file, unlike how difficult that is in general with grep. 但请注意,使用awk将搜索重点放在一个字段上,即使是每个文件的不同字段也是微不足道的,这与grep的一般难度不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM