0

I have a simple .sh file which controls the empty space of the server. It simply shows red line if the usage is more than 90 percent and yellow line over 50 percent. I have more than 300 remote servers and I want to run my .sh file over ssh connection but I am having problems with it. I can not execute my for loop commands after making ssh connection in my .sh file. Here is the code below;

#!/bin/bash
TXT_BLD=$(tput bold)
TXT_RED=$(tput setaf 1)
TXT_YLW=$(tput setaf 3)
TXT_WARN="${TXT_BLD}${TXT_YLW}"
TXT_ERR="${TXT_BLD}${TXT_RED}"
TXT_RST=$(tput sgr0)
IFS= mapfile -t disk < <(df -h | tail -n +2)



for i in {01..21}; do
    for j in {01..15}; do
    ssh bc$i-$j  
    hostname="$(hostname)"
    echo "#######${hostname}#######"
    IFS= mapfile -t disk < <(df -h | tail -n +2)
    for line in "${disk[@]}"; do
        USAGE=$(echo $line | awk '{print $5/1}')
        if [[ "$USAGE" -gt '90' ]]; then
            echo "${TXT_ERR}$line${TXT_RST}"
        elif [[ "$USAGE" -gt '50' ]]; then
            echo "${TXT_WARN}$line${TXT_RST}"
        else
            echo "$line"
        fi
    done
    done
done

I would be glad if you show me where my lackness is

thx

Anıl Aşık
  • 144
  • 13
  • 1
    `ssh foo` is a *command*. Like every other command, other commands that are after it in the script don't run until it exits (and you're no longer ssh'd into the remote machine). – Charles Duffy Jan 28 '19 at 13:56
  • 1
    See https://gist.github.com/charles-dyfis-net/26ffd5b24ded3b4aef2d3924a47de941 for an example of how you might better implement this. – Charles Duffy Jan 28 '19 at 14:03
  • 1
    (BTW, all-caps variable names are reserved, as described in http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html; better to use `usage` than `USAGE` -- all POSIX-compliant shells use only upper-case names for their own purposes, so you can use any lower-case name you like without worrying if the shell will later implement a builtin or setting that conflicts). – Charles Duffy Jan 28 '19 at 14:09
  • I need to read everything you shared thank you so much. By the way you forgot to put "done" in line 22 of your code sample – Anıl Aşık Jan 28 '19 at 14:34

0 Answers0