Basic Scripting
hey guys
when I issue this command Code:
ifconfig | grep bond* Code:
bond0 Link encap:Ethernet HWaddr 00:0C:29:41:95:85 I want to do this in a script but it's not working Code:
#!/bin/bash the second thing is based on the result from ifconfig | grep bond* if I get Code:
bond: echo "This server is an active cluster." thanks a lot |
First of all, test is a reserved command name, so you'll have to change it.
Next, single-quotes in bash make everything inside them literal. So when you do Code:
iftest='ifconfig | grep bond*' And to get the value of a variable, put $ in front of it. So the output of "echo $iftest" here is going to be "ifconfig | grep bond*". What you want to do is embed a command. The old form was to use ` backticks (note, these are not quotation marks). The newer, and preferred way is to use $(). Code:
iftest="`ifconfig | grep bond*`" For the second part, a simple "if" statement to test the contents of the variable should be all you need. You'll find lots of info on if structures in the various online tutorials. ;) |
In most (all?) situations, the use of a wildcard (*) with grep is redundant--ie "grep w" is the same as "grep w*". In some cases, it could cause really weird results.
|
sorry for bothering you again but
check the normal command display this Code:
ifconfig | grep bond now I added the if statement Code:
#!/bin/bash any idea? thanks |
try:
if [ "$iftest" == "bond0:" ] need spaces between equal signs. |
A couple of minor pts:
1. double brackets [[ ]] is preferred to [ ] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS 2. double quotes around $(cmd) is redundant (afaik) |
you can use case/esac,
Code:
case "$iftest" in |
All times are GMT -5. The time now is 09:30 AM. |