Concept Flow - grep in scripts
Start script
Run grep command
Check grep output
End script
The script runs grep to search text, then acts based on whether grep finds a match or not.
if grep -q "hello" file.txt; then echo "Found hello" else echo "No hello found" fi
| Step | Command | Grep Result | Condition | Action | Output |
|---|---|---|---|---|---|
| 1 | grep -q "hello" file.txt | Match found | True | Enter then branch | echo "Found hello" |
| 2 | echo "Found hello" | N/A | N/A | Print message | Found hello |
| 3 | End | N/A | N/A | Script ends |
| Variable | Start | After grep | After echo | Final |
|---|---|---|---|---|
| grep exit status | N/A | 0 (match) | N/A | 0 |
| output message | N/A | N/A | "Found hello" | "Found hello" |
grep in scripts: Use 'grep -q pattern file' to quietly check for text. Check grep exit status: 0 means match, non-zero means no match. Use if-then-else to run commands based on grep result. Example: if grep -q "text" file; then echo "Found"; else echo "Not found"; fi