0

i want to connect to 4 servers via bashscript and execute on each server some commands. The output of the commands should be saved in a variable locally. So i tried this peace of code.

I've already tried several things. When i just execute an "ls" or something else, i get the right result. Only with zgrep/grep it does not work like expected. The script stops after the 4th echo. So it looks like there is any problem with the grep command, but i dont have any clue what.

for node in $(echo $nodes | sed "s/,/ /g")
do
    echo "############################"
    echo "Searching in Node: $node"
    echo "Searching in file(s) of pattern: $FILENAME"
    echo "Searching for string: $SEARCH_STRING"
    OUT=$(ssh -t -v $user@$node "cd $TESA_LOG_DIR; zgrep $SEARCH_STRING $FILENAME")
    echo $OUT
done
xMaNuu
  • 31
  • 1
  • 7
  • What are these variables set to? A command stopping is often a sign that you are passing an empty variable and are not quoting it properly (as evidenced amply here). – tripleee Jan 05 '19 at 13:02
  • Don't use uppercase for your private variables. Try http://shellcheck.net/ before asking for human help (though none of the errors cause the symptom you are asking for help with). – tripleee Jan 05 '19 at 13:03

1 Answers1

2

We have no idea what these variables are set to; but passing in an empty search string or file name would produce the symptom you describe.

Actually, quoting your variables correctly would fix this sympom, and improve robustness, though obviously we can't know what causes these variables to be empty in the first place.

Furthermore, you should avoid using uppercase variable names, as those are reserved for system use.

Here is a refactoring which hopefully helps you find the root cause and fix some stylistic problems. I have added some inline comments.

# This is still problematic.
# How is $nodes initialized?
# Maybe use an array instead
for node in $(echo "$nodes" | sed "s/,/ /g")
do
    # Massive verbosity removed
    # Avoid useless variable
    # Avoid cd
    ssh -t -v "$user@$node" zgrep "$SEARCH_STRING" "$TESA_LOG_DIR/$FILENAME"
done
tripleee
  • 175,061
  • 34
  • 275
  • 318