在检查 /etc /group /etc /passwd中检查用户和组是否存在时,运算符和用户输入未找到 /出乎意料

发布于 2025-02-04 03:59:15 字数 1774 浏览 3 评论 0原文

我不明白我的代码怎么了。我正在尝试做到这一点,以便如果在/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

行雁书 2025-02-11 03:59:15

在Shebang中有一个缺少的,第一行应该是:

#!/bin/bash

elif s之后,缺少[

您可以始终使用shellCheck命令来解析并列出bash脚本中的所有问题:

shellcheck ./exercise_10.sh

最后,您可以依靠grep命令命令返回状态,如果语句像下面的那个:

if grep -q "^${group}:" /etc/group 2>/dev/null && grep -q "^${username}:" /etc/passwd 2>/dev/null ; then
  printf "Both exist.\n"
...

尝试一下,只能解析每个文件一次:

#!/bin/bash

read -rp 'Username: ' lusername
read -rp 'Group: ' lgroup

if grep -q "^${lgroup}:" /etc/group 2>/dev/null ; then
  if grep -q "^${lusername}:" /etc/passwd 2>/dev/null ; then
    printf "Both exist\n"
  else 
    printf "One exists, one does not\n"
  fi
else
  if grep -q "^${lusername}:" /etc/passwd 2>/dev/null ; then
    printf "One exists, one does not\n"
  else
    printf "Both not found\n"
  fi
fi

There is a missing ! in the shebang, the first line should be:

#!/bin/bash

There are missing [ right after the elifs.

You could always use shellcheck command to parse and list all issues in a bash script:

shellcheck ./exercise_10.sh

Finally you could rely on grep command return status with if statements like the one below:

if grep -q "^${group}:" /etc/group 2>/dev/null && grep -q "^${username}:" /etc/passwd 2>/dev/null ; then
  printf "Both exist.\n"
...

Give a try to this, which only parse each file once:

#!/bin/bash

read -rp 'Username: ' lusername
read -rp 'Group: ' lgroup

if grep -q "^${lgroup}:" /etc/group 2>/dev/null ; then
  if grep -q "^${lusername}:" /etc/passwd 2>/dev/null ; then
    printf "Both exist\n"
  else 
    printf "One exists, one does not\n"
  fi
else
  if grep -q "^${lusername}:" /etc/passwd 2>/dev/null ; then
    printf "One exists, one does not\n"
  else
    printf "Both not found\n"
  fi
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文