Florin Stingaciu
Florin Stingaciu

Reputation: 8275

Grab output from a script that was ran within another

I know the title is a bit confusing but here's my situation

#!/bin/bash
for node in `cat path/to/node.list`
do
        echo "Checking node: $node" 
        ssh -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no me@$node "nohup  scriptXYZ.sh"
done

Basically scriptXYZ has an output. Something like: Node bla is Up or Node bla is Down. I want to do something that amounts to the following psudo code:

if (output_from_scriptXYZ == "Node bla is Up")
    do this
else 
    do that

I've been trying to find a way to do this online but I couldn't find something that does this. Then again, I might not know what I'm searching for.

Also as a bonus: Is there any way to tell if the scriptXYZ has an error while it ran? Something like a "file does not exist" -- not the script itself but something the script tried to do and thus resulting in an error.

Upvotes: 0

Views: 73

Answers (1)

chepner
chepner

Reputation: 530843

First, is it possible to have scriptXYZ.sh exit with 0 if the node is up, and non-zero otherwise? Then you can simply do the following, instead of capturing the output. The scripts standard output and standard error will be connected to you local terminal, so you will see them just as if you had run it locally.

#!/bin/bash
while read -r node; do
    echo "Checking node: $node" 

    if ssh -o UserKnownHostsFile=/dev/null \
           -o StrictHostKeyChecking=no me@$node scriptXYZ.sh; then
       do something
    else
       do something else
    fi
done < path/to/node.list

It doesn't really make sense to run your script with nohup, since you need to stay connected to have the results sent back to the local host.

Upvotes: 1

Related Questions