Advertisement
oquidave

recursion in bash

Jun 4th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.14 KB | None | 0 0
  1. #!/bin/bash
  2. ip_up(){
  3.         server_ip=$1
  4.         trials=$2
  5.         max_trials=2
  6.         status=0
  7.         echo "server ip is: $server_ip, trial $trials" >&2
  8.         if ping -i 1 -c 3 "$server_ip" &> /dev/null
  9.         then
  10.             status=1
  11.         else
  12.             status=0
  13.             while ((  "$trials" < "$max_trials" )); do
  14.                 echo -e "$server_ip is down: Trial $trials, checking again after 1 sec" >&2
  15.                 sleep 1
  16.                 ((trials++))
  17.                 ip_up "$server_ip" "$trials"
  18.             done
  19.         fi
  20.         echo "$status"
  21. }
  22.  
  23. status=$(ip_up "$ip" 1)
  24. echo -e "the returned status is: ====$status====\n"
  25.  
  26. <<'COMMENT'
  27. //results
  28.  
  29.  $ ./check_servers.sh
  30. checking box1(173.36.232.6)
  31. server ip is: 173.36.232.6, trial 1
  32. 173.36.232.6 is down: Trial 1, checking again after 1 sec
  33. server ip is: 173.36.232.6, trial 2
  34. the returned status is: ====0
  35. 0====
  36.  
  37. ./check_servers.sh: line 41: [: 0
  38. 0: integer expression expected
  39. Sat Jun  4 15:16:11 EAT 2016 box2 (173.36.232.7) is UP
  40. checking box2 (173.36.232.7)
  41. server ip is: 173.36.232.7, trial 1
  42. the returned status is: ====1====
  43.  
  44. COMMENT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement