0

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.

4
  • for node in $( cat file )... is much better written while read node; do ...; done < file Commented Sep 19, 2012 at 21:39
  • @WilliamPursell: Might be true in this case (dunno), but not generally true--There's a semantic difference (lines vs words) Commented Sep 22, 2012 at 21:18
  • @JoSo, when only one variable is given, that variable gets the entire line. Each variable argument to read is assigned a word, and the last argument gets the final word plus the remainder of the line. Commented Sep 22, 2012 at 21:22
  • @WilliamPursell: Which is opposite to the for x in $(cat y) semantics. Commented Sep 22, 2012 at 21:25

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.