I have been writing a bash script to call in my .bashrc file to print the results of whatis for a random command in my /usr/bin folder and wanted to exclude commands that returned "nothing appropriate" in the result and even if I use grep, wc, expr, ==, nothing seems to work. I have pretty much used every example here, and here with no progress. This is what I have so far but failes to do what I want when it finds somthing that contains "nothing appropriate." If anyone could figure out how to get it to work or what a good solution would be in this situation I would be greatfull.
echo "Did you know that:";
RESULT=$(whatis $(ls /usr/bin | shuf -n 1))
if [[ $RESULT != *"nothing appropriate"* ]]
whatis prints the
nothing appropriate message on the standard error stream. This stream is not caught by the
$( ). This is the reason of your issue.
This is a way to fix it:
#! /bin/bash echo "Did you know that:"; while : do RESULT=$(whatis $(ls /usr/bin | shuf -n 1) 2>&1 | cat - ) if [[ $RESULT != *"nothing appropriate"* ]] then echo $RESULT break fi done
2>&1 | cat - addition does the trick