Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-12-2009, 01:26 PM   #1
Registered: Dec 2006
Posts: 107

Rep: Reputation: 17
Basic Scripting

hey guys

when I issue this command

ifconfig | grep bond*
I get
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

test='ifconfig | grep bond*'
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
I would like to print a message saying

echo "This server is an active cluster."

thanks a lot
Old 11-12-2009, 02:16 PM   #2
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,834

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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
iftest='ifconfig | grep bond*''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 $().

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.

Last edited by David the H.; 11-12-2009 at 02:17 PM.
Old 11-12-2009, 02:34 PM   #3
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
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.
Old 11-12-2009, 03:33 PM   #4
Registered: Dec 2006
Posts: 107

Original Poster
Rep: Reputation: 17
sorry for bothering you again but

the normal command display this

ifconfig | grep bond

bond0 Link encap:Ethernet HWaddr 00:0C:29:C1:96:7A
now my script
now I added the if statement

iftest="$(ifconfig | grep bond)"
echo $iftest
echo " "
if [ "$iftest"=="bond0:" ]; then
echo "NIC FOUND"
echo "NOT FOUND"
but this is showing me NIC FOUND when there is no bond0: in that string

any idea? thanks

Last edited by karlochacon; 11-12-2009 at 03:35 PM.
Old 11-12-2009, 03:37 PM   #5
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,150

Rep: Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887Reputation: 887
if [ "$iftest" == "bond0:" ]

need spaces between equal signs.
Old 11-12-2009, 06:37 PM   #6
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,411

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
A couple of minor pts:

1. double brackets [[ ]] is preferred to [ ]

2. double quotes around $(cmd) is redundant (afaik)
Old 11-12-2009, 07:22 PM   #7
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
you can use case/esac,

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
foolish and basic questions about shell scripting a.toraby Solaris / OpenSolaris 8 11-09-2007 02:10 PM
Basic shell scripting help zipper777 Linux - Newbie 6 03-13-2006 01:58 PM
Basic bach scripting BruceC Linux - Newbie 4 02-25-2005 10:31 AM
very basic scripting question spatter9999 Programming 3 09-18-2003 02:36 AM
Basic BASH scripting question Dark_Helmet Programming 3 06-26-2003 06:45 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:59 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration