linux 可以导入if语句吗?

hxzsmxv2  于 2024-01-06  发布在  Linux
关注(0)|答案(8)|浏览(141)

我有一个脚本,如果它运行,每次输出大约10行。这些行的内容各不相同。
我真的希望能够在输出中grep,并根据输出做不同的事情。
在伪这是我想做的

  1. cat /etc/password | \\
  2. if [ grep "root" $STDOUT ]; then
  3. echo "root is found"
  4. elif [ grep "nobody" $STDOUT ]; then
  5. echo "nobody is found"
  6. fi

字符串
这里我使用了cat /etc/password作为示例,但它应该被上面提到的脚本所取代。
问题是,如何在if/elif条件下获得cat /etc/password的输出?

8iwquhpp

8iwquhpp1#

正如@Benoit建议的那样,直接使用grep即可。
正如@larsmans所指出的,你可以通过将文件阅读到一个变量中来避免对文件的双重读取。
考虑到bash的可用性,我会这样做:

  1. password=$(< /etc/passwd)
  2. if grep -q root <<< "$password" ; then
  3. echo root found
  4. elif grep -q nobody <<< "$password" ; then
  5. echo nobody found
  6. fi

字符串
一次文件读取,一次或两次grep调用,没有启动其他进程或子shell。

6fe3ivhb

6fe3ivhb2#

我建议使用awk:

  1. cat /etc/passwd | awk '/root/{ do something }/nobody/{ do something else }'

字符串
你可以在bash中使用如下表达式实现同样的效果:

  1. cat /etc/passwd |
  2. while read; do
  3. if echo "$REPLY" | fgrep root; then
  4. something
  5. fi
  6. if echo "$REPLY" | fgrep nobody; then
  7. something_else
  8. fi
  9. done


然而,纯bash解决方案对于大输入的效率较低,因为它为每行运行单独的grep示例。

展开查看全部
ybzsozfc

ybzsozfc3#

你只需要:

  1. if grep -q "root" /etc/passwd ; then
  2. ...
  3. fi

字符串
如果grep exit code为0,它将播放...命令。
请记住\[ is a external command,可能位于/usr/bin/[中(通常它是到test的硬链接,当作为[调用时,它需要匹配的]参数)。也请参阅pitfalls页面,其中许多交易都与该命令有关。

z9zf31ra

z9zf31ra4#

使用子shell可以通过管道连接到if语句,但是这种解决方案会失败,因为您要在管道上运行两个grep命令,其中第一个命令将耗尽管道。
在你的情况下,最好的解决方案可能是将/etc/passwd读入一个变量,然后grep

  1. passwd=$(cat /etc/passwd)
  2. if (echo $passwd | grep -q root); then
  3. echo "root found"
  4. fi
  5. if (echo $passwd | grep -q nobody); then
  6. echo "nobody found"
  7. fi

字符串

h5qlskok

h5qlskok5#

使用&&:

  1. grep -q root /etc/password && echo "root is found"
  2. grep -q nobody /etc/password && echo "nobody is found"

字符串

kyvafyod

kyvafyod6#

下面的例子如何:

  1. #!/bin/bash
  2. if [ -z $1 ]; then
  3. echo Usage: $0 [UID to search for]
  4. exit 1;
  5. fi
  6. SEARCHID="$1"
  7. function notFound() {
  8. echo NOT FOUND
  9. }
  10. function found() {
  11. echo Found it
  12. }
  13. function main() {
  14. grep -i $SEARCHID /etc/passwd
  15. # Move $? to a variable
  16. SEARCHRESULT=$?
  17. if [ "$SEARCHRESULT" != "0" ]; then
  18. notFound;
  19. else
  20. found;
  21. fi
  22. }
  23. # call main function
  24. main

字符串

展开查看全部
pvcm50d1

pvcm50d17#

在一般情况下,您可以使用临时文件。

  1. t=$(mktemp -t passwd.XXXXXXXX)
  2. trap 'rm $t' 0
  3. trap 'exit 127' 1 2 3 5 15
  4. cat >$t
  5. for u in root nobody; do
  6. fgrep $u $t
  7. done

字符串
trap将在之后删除临时文件。
顺便说一句,你可以通过管道连接到if,但是条件中的第一个grep已经消耗了它的所有标准输入。在这种情况下它更有用:

  1. if $option_count ; then
  2. wc -l
  3. else
  4. tac
  5. fi <file

展开查看全部
dgiusagp

dgiusagp8#

我有一个不带if关键字的pipe to语句的简单示例:

  1. echo -e hello | [ $(cat) == hello ] && echo "Ok"

字符串
else太简单了:

  1. echo -e hello | ([ $(cat) == 'bye' ] && echo "Ok" || echo "No Okey")


例如,你想检查http响应的状态码是否等于200:

  1. curl http://google.com -w "%{http_code}" -s -o /dev/null | [ $(cat) == 200 ] && echo "Request successful processed"
  2. # Note: in this example google response is 403 .

展开查看全部

相关问题