[Gloucs] RE: [Northants] scripting

Swietochowska, Alina gloucs at mailman.lug.org.uk
Sat May 31 10:16:01 2003


Hi everybody, 
This is to comment on all the replies in one go. I sent it earlier today,
but it appears I hit the wrong 'reply' button and it went to one person only
(thanks Dave for alerting me to it). Anyway, here it is:

Command 
typeset -i num 
initialises variable num as an integer. It's the same as saying: 
integer num 
>From now on a string assignement is not possible. 

Simplest way of testing for a card would be: 

#!/bin/bash 
if /sbin/ifconfig eth1 >dev/null 2>&1 
then 
    echo "interface is fine" 
else 
    echo "no eth1 interface" 
fi 
The if statement checks the exit status of the last command. The '0' or '1'
are generated automatically, and 'if' will get them. 

If you wanted to use the num as below, the assignement should have a 'result
of a command' syntax, as in: 

num=$(ifconfig | grep eth1 | wc -l) 

This is the same as the old syntax: 
num=`ifconfig | grep eth1 | wc -l` 

However, both of the lines above are a massive overkill for what you need...


If you want to test for a value, don't use single brackets. This is, again,
old syntax. Better way is: 

if [[ $num == 1 ]] 
... 

However, since you declared the num as an integer, you could use an 
arithmetic comparison (intead of string one whi square brackets do): 

if (( $num == 1 )) 

In this instance it doesn't matter, but imaging the following: 

x=100 
y=20 

Spot the difference between the two tests: 

if [[ $x > $Y ]] 

and 

if (( $x > $y )) 

The first of these will give wrong result!!! 

Anyway, I'm going too far  off the track... Come to the scripting session to
learn more ;-) 

cheers, 
alina
 

-----Original Message-----
From: Richard Mellersh
To: Oxfordshire lug; Northants lug; Gloucester lug
Sent: 29/05/03 22:58
Subject: [Northants] scripting

Can anyone help?

I'm still trying to sort out some old stuff that never worked on older 
systems.  There is something wrong with line 4 inside the [brackets] - 
expects a "unary operator".

#! /bin/bash
typeset -i num
num=ifconfig | grep eth1 | wc -l
if [$num -eq 0] ; then
	ifconfig eth1 192.168.1.200 up
	route delete -net 192.168.1.0/24 eth1
	route add -host 192.168.1.201 eth1
fi

Any ideas?

Cheers
RM


_______________________________________________
Northants Linux Users Group:
Disclaimer: http://www.northants.lug.org.uk/disclaim.htm
List Help: http://www.northants.lug.org.uk/maillist.htm

Northants Linux Help Pages:
http://www.northants.lug.org.uk/cgi-bin/nlugwiki/wiki.pl?LinuxHelpPages

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

________________________________________________________________________
This e-mail has been scanned for all viruses by Star Internet. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________