How do I make an output for grep fail?

Zac

I am using if-else statement to search for keywords and displaying the results in the terminal, here's an example of my code.

read finding

if ["$finding" != "" ]; then
   grep $finding information.txt
else
   echo "No such information in database."
fi

But the terminal does not display anything if i key in information that does not exist. I started shell about a week back, might need more explanation on how certain code works.

jimmij
  • Add space after [ (it is a command)
  • Use -n to test if length of string is nonzero, or -z to test if it is zero
  • Put double quotes around variables

So:

read finding

if [ -z "$finding" ]; then
    echo "You didn't enter anything"
else
    grep "$finding" information.txt
    if [ ! "$?" -eq 0 ]; then
        echo "No such information in database."
    fi
fi

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How do I allow privileged commands to fail but respond?

来自分类Dev

How do I make this JS Turbolinks friendly?

来自分类Dev

How do I redirect the output of a system() function to a variable?

来自分类Dev

Java: How do I make a loop which creates arrays?

来自分类Dev

how do i use find, nm, and grep to find a symbol among many shared libraries?

来自分类Dev

How do I make this javascript function wait until I've scrolled x pixels down the page?

来自分类Dev

How do I iterate through each line of a command's output in bash?

来自分类Dev

How do I fix the output of my fraction class's overloaded + operator?

来自分类Dev

How do I make Auto Hotkey do Alt+F4 when I press right mouse button and scroll wheel?

来自分类Dev

How do I make a bitmap version of a WMF file that is loaded into a TImage.Picture and move that to a TSpeedButton.Glyph

来自分类Dev

How do I make vim automatically apply c++ syntax highlight on Arduino files (.ino/.pde)?

来自分类Dev

How can I recursively grep through several directories at once?

来自分类Dev

How can I make this Makefile more generic?

来自分类Dev

在Validator对象上模拟fail()和make()

来自分类Dev

Bash:grep -i -vFf

来自分类常见问题

How do I resolve ClassNotFoundException?

来自分类Dev

how can I save the output of a search for files matching *.txt to a variable?

来自分类Dev

Make a copy of the audio output into a file

来自分类Dev

If i know how a traced output looks, how can i begin creating my function? (Scheme)

来自分类Dev

How to make gnome-do recognize a specific application

来自分类Dev

支持 Ctrl+C / Ctrl+Z 在“ answer=$( while ! head -c 1 | grep -i '[ny]' ; do true ; done ) ”

来自分类Dev

How can I make two iframes fit the width of the page?

来自分类Dev

Sensitivity analysis with R: How can I make my own plot

来自分类Dev

How can I make JSSOR change its aspect ratio?

来自分类Dev

/ proc / [pid] / make-if-fail的含义和用法

来自分类Dev

How do i sort my listview alphabetically?

来自分类Dev

How do I parse a RestSharp response into a class?

来自分类Dev

How do I override a default keybinding in LightTable?

来自分类Dev

How do I modularize polyfills in Angular?

Related 相关文章

  1. 1

    How do I allow privileged commands to fail but respond?

  2. 2

    How do I make this JS Turbolinks friendly?

  3. 3

    How do I redirect the output of a system() function to a variable?

  4. 4

    Java: How do I make a loop which creates arrays?

  5. 5

    how do i use find, nm, and grep to find a symbol among many shared libraries?

  6. 6

    How do I make this javascript function wait until I've scrolled x pixels down the page?

  7. 7

    How do I iterate through each line of a command's output in bash?

  8. 8

    How do I fix the output of my fraction class's overloaded + operator?

  9. 9

    How do I make Auto Hotkey do Alt+F4 when I press right mouse button and scroll wheel?

  10. 10

    How do I make a bitmap version of a WMF file that is loaded into a TImage.Picture and move that to a TSpeedButton.Glyph

  11. 11

    How do I make vim automatically apply c++ syntax highlight on Arduino files (.ino/.pde)?

  12. 12

    How can I recursively grep through several directories at once?

  13. 13

    How can I make this Makefile more generic?

  14. 14

    在Validator对象上模拟fail()和make()

  15. 15

    Bash:grep -i -vFf

  16. 16

    How do I resolve ClassNotFoundException?

  17. 17

    how can I save the output of a search for files matching *.txt to a variable?

  18. 18

    Make a copy of the audio output into a file

  19. 19

    If i know how a traced output looks, how can i begin creating my function? (Scheme)

  20. 20

    How to make gnome-do recognize a specific application

  21. 21

    支持 Ctrl+C / Ctrl+Z 在“ answer=$( while ! head -c 1 | grep -i '[ny]' ; do true ; done ) ”

  22. 22

    How can I make two iframes fit the width of the page?

  23. 23

    Sensitivity analysis with R: How can I make my own plot

  24. 24

    How can I make JSSOR change its aspect ratio?

  25. 25

    / proc / [pid] / make-if-fail的含义和用法

  26. 26

    How do i sort my listview alphabetically?

  27. 27

    How do I parse a RestSharp response into a class?

  28. 28

    How do I override a default keybinding in LightTable?

  29. 29

    How do I modularize polyfills in Angular?

热门标签

归档