0

I am trying real hard to figure out how to fix my for loop in my bash profile. So here is my problem, I have an alias command called "command" it works.

For the sake of brevity we will say alias command='teamviewer'

So I am trying to make the bash profile go through a for-loop to detect internet connectivity. I was able to awk my way to success to determine when the state is up and when the state is down. Not I am stuck on my for-loop and am having issues making it work. The for-loop is as follows

#For-loop begins below
# Variable Assignment and Alias Command for Network Connectivity
# ==============================================================

alias command='teamviewer'
alias inet_state="ip addr show | awk '{print $8,$9}' | awk 'NR >= 7 && NR <=7'"
inetstate-good='$(state UP)'
inetstate-bad='$(state DOWN)'

# Loop for Internet Connection & Start Broadsign
# ==============================================
echo "$(inet_state);
for inet_state in "$(inet_state);
do {
     if inet_state="$(inetstate-bad)";
       then 'sleep 9999999999';
     elif inet_state="$(inetstate-good)";
       command;      
     fi}
done

# ==============================================

Anyhelp would be greatly appreciated :D

R. Barrett
  • 685
  • 11
  • 34
  • Another issue the alias command doesn't work properly when I set it in the bash profile. But the command works to get the state as UP or DOWN. – R. Barrett Nov 27 '18 at 19:16
  • The alias command I am specifying is 'inet_state' – R. Barrett Nov 27 '18 at 19:16
  • Trace-back is awk: cmd. line:1: {print ,} awk: cmd. line:1: ^ syntax error awk: cmd. line:1: {print ,} awk: cmd. line:1: ^ syntax error awk: cmd. line:1: {print ,} awk: cmd. line:1: ^ unexpected newline or end of string – R. Barrett Nov 27 '18 at 19:17
  • 2
    Aliases aren't expanded in non-interactive scripts unless `expand_aliases` shell option is set. `{` after `do` isn't needed, and `fi}` is understood as one (unknown) word. – choroba Nov 27 '18 at 19:52
  • Missing closing double quote after `echo`, missing closing double quote after `in`. – choroba Nov 27 '18 at 19:52
  • 2
    (1) Don't use aliases when you could use functions instead. (2) The missing end quotes on the `echo` are important. (3) The quotes around the 'sleep 9999'` means you're looking for a command with the numbers and the space as part of its name. There are several different, unrelated problems here, making it not really a great SO question. A [mcve] would focus on *just one* bug, taking out any code that isn't specifically related to that issue. – Charles Duffy Nov 27 '18 at 19:52
  • 1
    The "don't use aliases" advice is particularly important because the alias use is making your `$8` and `$9` in the `awk` command be interpreted by bash before that code ever gets to awk. If you were running `inet_state() { ip addr show | awk 'NR >= 7 && NR <=7 {print $8,$9}'; }`, there would be no such issue. – Charles Duffy Nov 27 '18 at 19:54
  • Oh, and as *yet another* issue, dashes aren't legal in shell variable names. – Charles Duffy Nov 27 '18 at 19:55
  • thanks guy I really appreciate it, I will play around with it and will continue to update this post. – R. Barrett Nov 27 '18 at 22:36

2 Answers2

0

You can try this. Network test taken from Jesse here

# Test for network connection. Taken from Jesse in
# https://stackoverflow.com/questions/929368/how-to-test-an-internet-connection-with-bash

for interface in $(ls /sys/class/net/ | grep -v lo); do
    if [[ $(cat /sys/class/net/$interface/carrier) = 1 ]]; then OnLine=1; fi
done

# Then do the job
if [ $OnLine == 1 ]; then
    teamwiewer
else
    sleep 9999999999
fi
miimote
  • 373
  • 2
  • 12
0

I finally got it working. Sorry for the delayed response. A friend of mine suggested to use fping.

while true; do
    if [[ "${inet_state}" = "${inet_state_good}" ]];
            then sleep 10 && <insert_process_name && break
    elif [[ "${inet_state}" = "${inet_state_bad}" ]];
            then echo "Your Player has a technical issue, please call XXXXXXXXXXXXX ext. 150" && sleep 999999999 && sudo pkill <insert_process_name> 
    fi
done
R. Barrett
  • 685
  • 11
  • 34