在检查 /etc /group /etc /passwd中检查用户和组是否存在时,运算符和用户输入未找到 /出乎意料
我不明白我的代码怎么了。我正在尝试做到这一点,以便如果在/etc/group和etc/passwd中都找不到ECHO中的用户名和组都没有找到。
如果发现一个但没有发现一个回声,最后发现它们都被发现,那么它们都存在Echo。
这是我的代码:
#/bin/bash
read -p 'Username: ' username
read -p 'Group: ' group
if [ $(grep "^$group:" /etc/group | cut -f 1 -d ':') != $group ] && [ $(grep "^$username:" /etc/passwd | cut -f 1 -d ':') != $username ]
then
echo Both not found
elif $(grep "^$group:" /etc/group | cut -f 1 -d ':') != $group ] || [ $(grep "^$username:" /etc/passwd | cut -f 1 -d ':') != $username ]
then
echo One exists, one does not.
elif $(grep "^$group:" /etc/group | cut -f 1 -d ':') = $group ] && [ $(grep "^$username:" /etc/passwd | cut -f 1 -d ':') = $username ]
then
echo both exist
fi
这是输出:
$ ./exercise_10.sh
Username: user
Group: root
./exercise_10.sh: 10: root: not found
./exercise_10.sh: 14: root: not found
$ ./exercise_10.sh
Username: dfgdfggd
Group: root
./exercise_10.sh: 10: root: not found
./exercise_10.sh: 10: [: !=: unexpected operator
./exercise_10.sh: 14: root: not found
$ ./exercise_10.sh
Username: dfgdg
Group: cgdgdfg
./exercise_10.sh: 6: [: !=: unexpected operator
./exercise_10.sh: 10: !=: not found
./exercise_10.sh: 10: [: !=: unexpected operator
./exercise_10.sh: 14: =: not found
我已经在命令行中测试了GREP和剪切命令,它有效:
$ grep '^user:' /etc/passwd | cut -f 1 -d ':'
user
I can't understand what's wrong with my code. I'm trying to make it so that if the passed in username and group are both not found in /etc/group and etc/passwd echo both not found.
If one but not the other is found echo one is found and finally if they are both found then echo both exist.
Here is my code:
#/bin/bash
read -p 'Username: ' username
read -p 'Group: ' group
if [ $(grep "^$group:" /etc/group | cut -f 1 -d ':') != $group ] && [ $(grep "^$username:" /etc/passwd | cut -f 1 -d ':') != $username ]
then
echo Both not found
elif $(grep "^$group:" /etc/group | cut -f 1 -d ':') != $group ] || [ $(grep "^$username:" /etc/passwd | cut -f 1 -d ':') != $username ]
then
echo One exists, one does not.
elif $(grep "^$group:" /etc/group | cut -f 1 -d ':') = $group ] && [ $(grep "^$username:" /etc/passwd | cut -f 1 -d ':') = $username ]
then
echo both exist
fi
Here is the output:
$ ./exercise_10.sh
Username: user
Group: root
./exercise_10.sh: 10: root: not found
./exercise_10.sh: 14: root: not found
$ ./exercise_10.sh
Username: dfgdfggd
Group: root
./exercise_10.sh: 10: root: not found
./exercise_10.sh: 10: [: !=: unexpected operator
./exercise_10.sh: 14: root: not found
$ ./exercise_10.sh
Username: dfgdg
Group: cgdgdfg
./exercise_10.sh: 6: [: !=: unexpected operator
./exercise_10.sh: 10: !=: not found
./exercise_10.sh: 10: [: !=: unexpected operator
./exercise_10.sh: 14: =: not found
I've tested the grep and cut command in the command line and it works:
$ grep '^user:' /etc/passwd | cut -f 1 -d ':'
user
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在Shebang中有一个缺少的
!
,第一行应该是:在
elif
s之后,缺少[
。您可以始终使用
shellCheck
命令来解析并列出bash脚本中的所有问题:最后,您可以依靠
grep
命令命令返回状态,如果语句像下面的那个:尝试一下,只能解析每个文件一次:
There is a missing
!
in the shebang, the first line should be:There are missing
[
right after theelif
s.You could always use
shellcheck
command to parse and list all issues in a bash script:Finally you could rely on
grep
command return status withif
statements like the one below:Give a try to this, which only parse each file once: