The problem was that grive
command was outputting to the error stream, STDERR (File descriptor 2), which is normally used for passing error messages while the STDOUT (File descriptor 1) is used for showing actual (upon success) output of any program (strictly speaking).
I have not used grive
, but according to your words the developer might have used STDERR only to show any messages from the command.
Now the command substitution you have used :
VARABLENAME=$(command)
will save the STDOUT of command
to VARABLENAME
, not STDERR. As a result the variable was empty and you were seeing messages on terminal.
I have suggested :
VARABLENAME="$(command 2>&1)"
this will save both the STDOUT and STDERR to the variable (in your case just STDERR as there is no STDOUT), So you would get desired result using echo "${VARIABLENAME}"
.
Also as there is no STDOUT you can save just the STDERR to the variable (generalization, true for any such program) :
VARABLENAME="$(command 2>&1 >/dev/null)"
Basically, you want this structure
if grep -q "un-clean" /path/to/log_file.log ;
then
# put some command in case we find result is unclean
else
# if the output is ok, do something else
fi
All it does is to silently (without printing to screen) check if there is a match of string "unclean" in the file . If there is we do the if part, otherwise - we do else part.
Here's an example:
$> if grep -q 'root' /etc/passwd ; then echo "This user exists" ; else echo "This user doesn't exist"; fi
This user exists
$> if grep -q 'NOEXIST' /etc/passwd ; then echo "This user exists" ; else echo "This user doesn't exist"; fi
This user doesn't exist
What also can be done is to start command you want from script, but in background. That way we can have it's PID. Here's what I mean
$> echo "HelloWorld" &
[1] 6876
Adding &
causes echo "HelloWorld"
run in background, and we have it's PID stored in $!
variable. Thus, we can do something like,
some-command &
CMD_PID=$!
if grep -q "un-clean" /path/to/log_file.log ;
then
kill -TERM $CMD_PID
else
# if the output is ok, do something else
fi
Best Answer
If you need to use bash
time
builtin function I'd use the following command:Examples: