My first shot at variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting My first shot at variables
# 1  
Old 09-06-2008
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:

Code:
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....
# 2  
Old 09-06-2008
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!
# 3  
Old 09-07-2008
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.

Code:
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.
# 4  
Old 09-07-2008
Wow, thanks, that cleared up a bunch of things for me!
# 5  
Old 09-07-2008
Oh, another thing:

Code:
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

Code:
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.
# 6  
Old 09-07-2008
foo='Thanks, I am reading up on quotation examples so I dont have this problem again.' echo foo
# 7  
Old 09-07-2008
Quote:
Originally Posted by arcnsparc
foo='Thanks, I am reading up on quotation examples so I dont have this problem again.' echo foo
Code:
foo="Thanks, I am reading up on quotation examples so I dont have this problem again."; echo $foo

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute multiple commands in one shot?

for example: I'm greping the process where i can get the location of the file $ ps -ef | grep LLAWP | awk {'print $9'} | tail -1 /Hostname/ihs/INSTANCE2/conf/WebAgent.conf then I need to display second line of WebAgent.conf file: $ cat /Hostname/ihs/INSTANCE1/conf/WebAgent.conf | head... (2 Replies)
Discussion started by: raghur77
2 Replies

2. Shell Programming and Scripting

How to pass different number of arguments in a single shot

I have one configuration file. The number of lines in the file will vary. I need to pass each line as a parameter to a shell script in a single shot. Ex: Suppose file contains: ou=x,o=z o=y Suppose the shell script name is sample.sh. Then the script should be called like sample.sh ou=x.o=z... (6 Replies)
Discussion started by: saurabhkoar
6 Replies

3. Shell Programming and Scripting

To change the ownership at one shot

i have a directory in which i have Multiple files: Following are they==== -rw-r--r-- 1 root root 886 Jan 21 16:38 trunkn.xsd -rw-r--r-- 1 root root 244 Jan 21 16:38 trunknameCache.xml -rw-r--r-- 1 root root 1240 Jan 21 16:38 subscribercache.xsd -rw-r--r-- 1 root ... (1 Reply)
Discussion started by: Aditya.Gurgaon
1 Replies

4. OS X (Apple)

Please look at this screen shot: How do I remove this lock icon?!

RESOLVED in the screen shot I'd like to remove the lock sandwiched between the spaces indicator and the monitor icon, how do I get rid of it? It locks the screen and I cannot remember how it came to be http://guptaxpn.com/tmp/lock_icon_screenshot.png Thanks for your help! (3 Replies)
Discussion started by: guptaxpn
3 Replies

5. Shell Programming and Scripting

remove a 350 files in one shot

i have a dir called logs. In it i have some 350 files how to move all those files from directory logs to a new dir called archive I want to do it in single shot Below the command i m trying but not getting find . -name "CurrentCollector*" -exec mv {} archive \;... (2 Replies)
Discussion started by: ali560045
2 Replies

6. AIX

script which take a snap shot of Topas

can any one give me a script which take a snap shot of Topas at a fix interval of 15min and save it in a perticular file!!!!!!!! (6 Replies)
Discussion started by: abhishek27
6 Replies

7. Shell Programming and Scripting

Delete all occurence of a word in one shot

i have a file called file1 cat file1 i am namish namish lives in India India and namish both are good. I want to delete all the occurences of namish in one shot,if i do it with sed i guess all the lines will be deleted containing the pattern.Suggest me any idea without AWK. Thanks... (6 Replies)
Discussion started by: namishtiwari
6 Replies

8. UNIX for Dummies Questions & Answers

unix screen shot

i would like to see an unix screen shot if anyone could take a screen shot of unix (7 Replies)
Discussion started by: royal
7 Replies

9. UNIX for Dummies Questions & Answers

Print Multipul Fiels as One shot

Hi there This is my first post. I just want to know the best way to print N number of files as one shot. Let's simulate the case ... > ls file1.txt file2.txt file3.txt file4.txt file5.txt What is the best way to print all this files.. what I'm doing now is / > print... (9 Replies)
Discussion started by: geoquest
9 Replies
Login or Register to Ask a Question