[英]Finding linux users with a bash script
i am newbie in bash-scripting and linux.我是 bash 脚本和 linux 的新手。 I want to write a script to get a username from the terminal and say whether that username is on the passwd or not.
我想编写一个脚本来从终端获取用户名并说明该用户名是否在密码中。 i wrote and tried below script but it doesn't work.
我编写并尝试了下面的脚本,但它不起作用。 help me that what i can do.
帮我做我能做的。
users = cat /etc/passwd
echo User_Name
cat users | grep User_Name
if [ User_Name in users ];
then
echo "User_Name FOUND"
else
echo "User NOT FOUND!"
fi
how i can define a variable to read a string from termnial.我如何定义一个变量来从终端读取字符串。
Your script could look like that:您的脚本可能如下所示:
#!/usr/bin/env bash
while read -rp "Enter user name: " user_name
do
if [ -z "$user_name" ]
then
echo "Enter a non-empty username"
continue
fi
if grep -q "^$user_name:" /etc/passwd
then
echo "$user_name FOUND"
exit 0
else
echo "$user_name NOT FOUND!" >&2
exit 1
fi
done
Here is my take on this question.这是我对这个问题的看法。 Using
getent
to search user in the file /etc/passwd
使用
getent
在文件/etc/passwd
搜索用户
If you just want to check if a user exists.如果您只想检查用户是否存在。
getent passwd "$username"
That should print the name (and all info in the Gecos Field ) of the user if it does exists, and return with zero 0
status which means true.如果确实存在,那应该打印用户的名称(以及Gecos 字段中的所有信息),并返回零
0
状态,这意味着 true。 Otherwise non-zero, see the manual for the meaning of different status.否则为非零,请参阅手册了解不同状态的含义。
Now that we know that information at hand we can use the if-statement
现在我们知道手头的信息,我们可以使用
if-statement
if getent passwd "$username"; then
echo "$username found."
else
echo "$username not found." >&2
fi
Redirect the output to /dev/null
if that is not the desired result, or if you're just after the exit/return status of the test.如果这不是所需的结果,或者您刚好在测试的退出/返回状态之后,请将输出重定向到
/dev/null
。
if getent passwd "$username" >/dev/null; then
echo "$username found."
else
echo "$username not found." >&2
fi
To add that to a full blown script.将其添加到完整的脚本中。
#!/bin/sh
check_user() {
printf '%s' "Enter user name: "
read -r user_name
case $user_name in
'') printf '%s\n' "You have not entered anything!" >&2 ##: If empty input.
return 1;;
esac
if getent passwd "$user_name" >/dev/null; then ##: getent has no -q option unfortunately.
printf '%s\n' "$user_name FOUND"
return 0
else
printf '%s\n' "$user_name NOT FOUND!" >&2
return 1
fi
}
##: Continue the loop until a match in `/etc/passwd` was given.
until check_user; do
printf 'Please try again!\n' >&2 ##: Send message to stderr and loop again.
done
Instead of redirecting to /dev/null
you can save the output of getent
in a variable using Command Substituion and use that value, plus a little bit of Parameter Expansion .您可以使用Command Substituion 将
getent
的输出保存在变量中,而不是重定向到/dev/null
并使用该值,再加上一点Parameter Expansion 。
check_user() {
printf '%s' "Enter user name: "
read -r user_name
case $user_name in
'') printf '%s\n' "You have not entered anything!" >&2
return 1;;
esac
if user=$(getent passwd "$user_name"); then
printf '%s\n' "${user%%:*} FOUND" ##: Print only the username using P.E.
return 0
else
printf '%s\n' "${user:-"$user_name"} NOT FOUND" >&2 ##: If the value of "$user" is empty (No match found by getent)
return 1
fi
}
until check_user; do
printf 'Please try again!\n' >&2
done
until
loop will continue until it receives a 0
return status. until
循环将继续,直到它收到0
返回状态。getent(1)
aka man 1 getent
getent(1)
又名man 1 getent
GNU Linux
and FreeBSD
GNU Linux
和FreeBSD
上测试To read a value as a user input, you can use the "read" command.要将值作为用户输入读取,您可以使用“读取”命令。
read User_Name
echo "User is ${User_Name}"
For convenience, most scripts use ALLCAPS on the variable names for shell scripting.为方便起见,大多数脚本在 shell 脚本的变量名称上使用 ALLCAPS。
Also consider passing this value as a parameter into your script.还可以考虑将此值作为参数传递到脚本中。 It's readable as "$1" for a single parameter.
对于单个参数,它可读为“$1”。
Hint1: You don't need to store the whole file in memory just for this check.提示 1:您不需要为了这个检查而将整个文件存储在内存中。
Hint2: To find if a user is present in the passwd file, you should check for the first column only of that file and try a match.提示 2:要查找用户是否存在于 passwd 文件中,您应该只检查该文件的第一列并尝试匹配。
Hint3: If you want to use the grep command, it will return success when there's a match and failure when there's not match.提示3:如果要使用grep命令,匹配时返回成功,不匹配时返回失败。
The code below would also work for the task下面的代码也适用于该任务
#!/bin/bash
read WHO;
grep "^${WHO}:" /etc/passwd 2>/dev/null >/dev/null && echo "found" || echo "not found"
We use read
to get input from the user at the terminal.我们使用
read
从终端获取用户的输入。 In the below solution, we use POSIX id
(present on BSDs as well as Linux) to determine if the user exists or not.在下面的解决方案中,我们使用POSIX
id
(出现在 BSD 和 Linux 上)来确定用户是否存在。
#!/bin/bash
set -o errexit -o pipefail -o nounset
function interactive_check_if_user_exists {
local username output
read -p 'Username to check: ' -r username
if output="$(id -u "$username" 2>&1)"
then
echo "Found \`$username\` (uid: $output)" >&2
else
local code=$?
if [[ $code -eq 1 ]]
then
echo "User \`$username\` is not present on this system." >&2
else
echo "Unexpected error occurred: $output" >&2
return "$code"
fi
fi
}
interactive_check_if_user_exists
To print and read an argument from terminal, you should use echo and then use read commane and then call it with dollar-sign, finally the script know what you want.要从终端打印和读取参数,您应该使用echo ,然后使用 read Commane ,然后用美元符号调用它,最后脚本知道您想要什么。 for example:
例如:
echo "Enter UserName: "
read User_Name
here is the correct form of your code:这是您的代码的正确形式:
echo "Enter the Username: "
read User_Name
cut -f 1 -d: /etc/passwd | grep -q $User_Name
if [ $? -eq 0 ];
then
echo "User $User_Name FOUND"
else
echo "User NOT FOUND!"
fi
Tips:提示:
cut -f 1 -d #cut the first column of the passwd list
grep -q # find username and do not write anything to standard output in terminal.
if [ $? -eq 0 ] #in linux terminal when we want to know whether the previous command is executed correctly we use $? and the output must be 0. and - eq means equal to 0. and when we use that, it means if previos command (grep the username) executed correctly and the username was found, then run next step and print it found.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.