Check for zero lines output from command over SSH

Sandra Schlichting

If I do the following, and the network is down, then the zero case will be executed, which it shouldn't.

case "$(ssh -n $host zfs list -t snapshot -o name -H | grep "tank/fs" | wc -l | awk '{print $1}')" in
    0)  # do something
        ;;
    1)  # do something else
        ;;
    *)  # fail
esac

Earlier in the script I check that I can SSH to $host, but today I found this problem, where the network failed right after my check.

If I check the return value from the SSH command, then I will always get the return value from awk as it is executed last.

Question

How do I insure that I actually count zero lines that zfs outputted, and not zero lines from a failed SSH connection?

devnull

Say:

set -o pipefail

at the beginning of your script (or before the case statement).

Moreover, check for the return code of the command before executing the case statement:

set -o pipefail
$value=$(ssh -n $host zfs list -t snapshot -o name -H | grep "tank/fs" | wc -l | awk '{print $1}')
if [ $? == 0 ]; then
   case $value in
     ...
   esac
fi

From the manual:

pipefail

If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands in the pipeline exit successfully. This option is disabled by default.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Different output of command over ssh

From Dev

Write SSH command over multiple lines

From Dev

No output from bash -c over ssh

From Dev

bash - count and output lines from command

From Dev

Is there any way to read lines from command output?

From Dev

rsync -- command-line for syncing from local to remote over ssh

From Dev

Set desktop background from command line over ssh

From Dev

How can I blank the screen from the command line over SSH?

From Dev

How can I blank the screen from the command line over SSH?

From Dev

Set desktop background from command line over ssh

From Dev

Output of ssh control command ~#

From Dev

How to create an array from the lines of a command's output

From Dev

Read specific floating point values from lines of the command output

From Dev

Is there a way to find out the number of lines from a command output?

From Dev

How to create an array from the lines of a command's output

From Dev

How to insert tabs before output lines from a executed command

From Dev

Capture output from command into variable retaining new lines

From Dev

grep lines starting with - or plus from yum command output

From Dev

How to understand output from "ssh -O check" in bash scripts?

From Dev

Get output from a Paramiko SSH exec_command continuously

From Dev

how to read ssh output from 'last' and 'who' command

From Dev

get output of this command from another server via ssh

From Dev

"top" not showing output over "ssh"

From Dev

execute command over ssh connection

From Dev

python: getting a response from check_output before the command is finished

From Dev

python: getting a response from check_output before the command is finished

From Dev

Check if dividing by zero, then printing two lines?

From Dev

Trying to make the command lines start plotting from the position zero with a vector as argument

From Dev

Run command output lines as another command in linux

Related Related

  1. 1

    Different output of command over ssh

  2. 2

    Write SSH command over multiple lines

  3. 3

    No output from bash -c over ssh

  4. 4

    bash - count and output lines from command

  5. 5

    Is there any way to read lines from command output?

  6. 6

    rsync -- command-line for syncing from local to remote over ssh

  7. 7

    Set desktop background from command line over ssh

  8. 8

    How can I blank the screen from the command line over SSH?

  9. 9

    How can I blank the screen from the command line over SSH?

  10. 10

    Set desktop background from command line over ssh

  11. 11

    Output of ssh control command ~#

  12. 12

    How to create an array from the lines of a command's output

  13. 13

    Read specific floating point values from lines of the command output

  14. 14

    Is there a way to find out the number of lines from a command output?

  15. 15

    How to create an array from the lines of a command's output

  16. 16

    How to insert tabs before output lines from a executed command

  17. 17

    Capture output from command into variable retaining new lines

  18. 18

    grep lines starting with - or plus from yum command output

  19. 19

    How to understand output from "ssh -O check" in bash scripts?

  20. 20

    Get output from a Paramiko SSH exec_command continuously

  21. 21

    how to read ssh output from 'last' and 'who' command

  22. 22

    get output of this command from another server via ssh

  23. 23

    "top" not showing output over "ssh"

  24. 24

    execute command over ssh connection

  25. 25

    python: getting a response from check_output before the command is finished

  26. 26

    python: getting a response from check_output before the command is finished

  27. 27

    Check if dividing by zero, then printing two lines?

  28. 28

    Trying to make the command lines start plotting from the position zero with a vector as argument

  29. 29

    Run command output lines as another command in linux

HotTag

Archive