The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script which take a snap shot of Topas abhishek27 AIX 6 08-18-2008 08:59 PM
Delete all occurence of a word in one shot namishtiwari Shell Programming and Scripting 6 08-24-2007 09:41 AM
unix screen shot royal UNIX for Dummies Questions & Answers 7 07-14-2004 05:36 PM
Print Multipul Fiels as One shot geoquest UNIX for Dummies Questions & Answers 9 04-10-2002 09:57 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-06-2008
arcnsparc arcnsparc is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 9
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 (permalink)  
Old 09-06-2008
arcnsparc arcnsparc is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 9
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 (permalink)  
Old 09-07-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 09-07-2008
arcnsparc arcnsparc is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 9
Wow, thanks, that cleared up a bunch of things for me!
  #5 (permalink)  
Old 09-07-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
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 (permalink)  
Old 09-07-2008
arcnsparc arcnsparc is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 9
foo='Thanks, I am reading up on quotation examples so I dont have this problem again.' echo foo
  #7 (permalink)  
Old 09-07-2008
DeCoTwc DeCoTwc is offline
Registered User
  
 

Join Date: Mar 2008
Location: NYC
Posts: 76
Quote:
Originally Posted by arcnsparc View Post
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 05:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0