LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


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

Rep: Reputation: 17
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
 
Old 11-12-2009, 02:16 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
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.

Last edited by David the H.; 11-12-2009 at 02:17 PM.
 
Old 11-12-2009, 02:34 PM   #3
pixellany
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
karlochacon
Member
 
Registered: Dec 2006
Posts: 107

Original Poster
Rep: Reputation: 17
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

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

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
try:
if [ "$iftest" == "bond0:" ]

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

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
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)
 
Old 11-12-2009, 07:22 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

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

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


Reply


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


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

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