Advertisement
gheja

Bash what.

Nov 14th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.55 KB | None | 0 0
  1. --- script ---
  2. #!/bin/bash
  3.  
  4. echo "a" > a.txt
  5. echo "b" >> a.txt
  6. echo "c" >> a.txt
  7. echo "d" >> a.txt
  8.  
  9. fun1()
  10. {
  11.     local string="$1"
  12.     local line
  13.    
  14.     cat a.txt | while read line; do
  15.         if [ "$line" == "$string" ]; then
  16.             echo "$string: found"
  17.             return
  18.         fi
  19.     done
  20.    
  21.     echo "$string: not found"
  22. }
  23.  
  24. fun1 123
  25. fun1 b
  26. fun1 456
  27.  
  28. --- output ---
  29. 123: not found
  30. b: found
  31. b: not found
  32. 456: not found
  33.  
  34. --- explanation ---
  35. The pipe ("|") spawns a subshell, a separate bash
  36. process and the "return" inside it exits the
  37. subshell not the function.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement