My first shot at variables

Okay, so im setting up a script to start my internet dependent scripts once I am connected to the net. It got complicated because of the different networks I frequent but it goes something like this:

n=1
iwconfig wlan0 > wireless.txt
m= grep -c MGHS /home/jake/Scripts/wireless.txt
o= grep -c NMU /home/jake/Scripts/wireless.txt
while [ $n -le 50 ]; do
    echo $n & sleep 3
    let n++
    if (ping -c 1 www.google.com)
        then sh /home/jake/Scripts/netdepsu.sh
    elif test "$m" -ne "0" 
        then sh /home/jake/Scripts/netdepsu.sh
    elif test $o -ne $0
        then sh /home/jake/Scripts/netdepsu.sh
    else echo "no internet"
    fi
done

Basically, if that txt file contains the name of one of the weird networks I use then I want to start up the other script (i cant ping at NMU or MGHS). Here are the two errors I just cant get rid of:

./looper.sh: line 13: test: : integer expression expected
./looper.sh: line 15: test: -ne: unary operator expected

if I dont have that space after those '='s up top then I get this error:

./looper.sh: line 6: -c: command not found
./looper.sh: line 7: -c: command not found

I know this has to be an easy fix if anyone could let me know I'd be ever so grateful....

I think i figured it out..
it was a problem with assigning the variables...
[CODE}#looper

n=1
iwconfig wlan0 > wireless.txt
grep -c MGHS /home/jake/Scripts/wireless.txt
m=$?
grep -c NMU /home/jake/Scripts/wireless.txt
o=$?
while [ $n -le 50 ]; do
echo $n & sleep 1
let n++
if (ping -c 1 www.google.com)
then sh /home/jake/Scripts/netdepsu.sh
elif [ "$m" = 0 ]
then sh /home/jake/Scripts/netdepsu.sh & echo "MGH" & break
elif test [ "$m" = 0 ]
then sh /home/jake/Scripts/netdepsu.sh & echo "NMU" & break
else echo "no internet"
fi
done
[/CODE]

yay for my most complicated script ever!!! it only took me 7 hours!

It's still not correct, the value of $? is 0 for match and 1 for no match after grep -c

The construct you are looking for is backticks.

m=`grep -c MGHS /home/jake/Scripts/wireless.txt`
o=`grep -c NMU /home/jake/Scripts/wireless.txt`

Those are grave accents (ASCII 96), not regular straight apostrophes.

In other news, the parentheses around the ping in the if are superfluous, and the & you use in a few places means "background this process"; you want "&&" which means "and if the previous command was successful, also run ..." or perhaps just semicolon, which is basically equivalent to newline.

Wow, thanks, that cleared up a bunch of things for me!

Oh, another thing:

foo=bar echo boink

This assigns "bar" to the variable "foo" for the duration of the execution of the command echo boink

To assign a variable value which contains spaces, you want

foo='bar echo boink'

There are differences between single and double quotes but in this context, both work equally well. I suspect it's better to save explaining the difference until another time.

foo='Thanks, I am reading up on quotation examples so I dont have this problem again.' echo foo

foo="Thanks, I am reading up on quotation examples so I dont have this problem again."; echo $foo

haha, Ill get there eventually!