LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Basic Scripting (https://www.linuxquestions.org/questions/linux-newbie-8/basic-scripting-768704/)

karlochacon 11-12-2009 12:26 PM

Basic Scripting
 
hey guys

when I issue this command

Code:

ifconfig | grep bond*
I get
Code:

bond0    Link encap:Ethernet  HWaddr 00:0C:29:41:95:85
bond0:0  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
test='ifconfig | grep bond*'
test

but it does not not print the stuff above why?

the second thing is
based on the result from ifconfig | grep bond*
if I get
Code:

bond:
I would like to print a message saying

echo "This server is an active cluster."

thanks a lot

David the H. 11-12-2009 01:16 PM

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*'
...you're telling it to store that exact string.

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*`"
iftest="$(ifconfig | grep bond*)"

Note that in my examples I put double-quotes around the whole phrase. They work like single quotes, but allow "$" "`" and "\" to be expanded by the shell, so you can still use variables, commands, and escape sequences inside them.

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. ;)

pixellany 11-12-2009 01:34 PM

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.

karlochacon 11-12-2009 02:33 PM

sorry for bothering you again but

check
the normal command display this

Code:

ifconfig | grep bond

bond0 Link encap:Ethernet HWaddr 00:0C:29:C1:96:7A

now my script
now I added the if statement

Code:

#!/bin/bash
iftest="$(ifconfig | grep bond)"
echo $iftest
echo " "
if [ "$iftest"=="bond0:" ]; then
echo "NIC FOUND"
else
echo "NOT FOUND"
fi

but this is showing me NIC FOUND when there is no bond0: in that string

any idea? thanks

schneidz 11-12-2009 02:37 PM

try:
if [ "$iftest" == "bond0:" ]

need spaces between equal signs.

chrism01 11-12-2009 05:37 PM

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)

ghostdog74 11-12-2009 06:22 PM

you can use case/esac,

Code:

case "$iftest" in
  bond0* ) echo "NIC found";;
  * ) echo "NIC not found";;
esac



All times are GMT -5. The time now is 09:30 AM.