I am trying to write the output of exec ls to a variable. I am new to bash so probably overlooking something simple. The aim is to check a mount within a docker container and if it comes back with a certain error then restart the container. So far I have:
test=$(docker exec Test ls /mnt/1 )
echo $test
if [[ $(echo $test | grep "ls: cannot access '/mnt/1': Transport endpoint is not connected") ]]; then
echo "$(date "+%d.%m.%Y %T") Mount 1 disconnected will restart container" >> checkmounts.log
It gives the error, but it does not write it to the variable so I cannot grep it. When I echo $test it is blank.
Any help with what I am doing wrong would be greatly appreciated.
Thanks
test=$(...)
, but you'll also need the error, if you want to grep it. Better to test the exit status: https://unix.stackexchange.com/a/340858/70524 – muru Jan 24 '21 at 09:45stderr
output onto thestdout
output (which is what the command substitution captures), sotest=$(docker .... 2>&1)
, see this answer. But testing the return value of the call is probably the safer way. – AdminBee Jan 25 '21 at 08:58