Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash scripting - execute and grep command inside script

Tags:

bash

Okay, so I'm learning Bash, and there's that exercise;

"Write a script that checks every ten seconds if the user 'user000' is logged in."

My idea is to grep a who, but I don't know how to incorporate this into a script. I tried things like

if [ `who | grep "user000"` ] then things

but it returns the matched lines with grep, not true/false.

like image 340
geord Avatar asked Dec 03 '25 00:12

geord


1 Answers

You want grep -q. That's "quiet mode"; just sets status based on whether there were any matches, doesn't output anything. So:

if who | grep -q "user000"; then things; fi
like image 118
chaos Avatar answered Dec 05 '25 21:12

chaos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!