LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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, 12:26 PM   #1
karlochacon
Member
 
Registered: Dec 2006
Posts: 109

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, 01:16 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 01:17 PM.
 
Old 11-12-2009, 01:34 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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, 02:33 PM   #4
karlochacon
Member
 
Registered: Dec 2006
Posts: 109

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 02:35 PM.
 
Old 11-12-2009, 02:37 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

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

need spaces between equal signs.
 
Old 11-12-2009, 05:37 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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, 06: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



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 01:10 PM
Basic shell scripting help zipper777 Linux - Newbie 6 03-13-2006 12:58 PM
Basic bach scripting BruceC Linux - Newbie 4 02-25-2005 09:31 AM
very basic scripting question spatter9999 Programming 3 09-18-2003 01:36 AM
Basic BASH scripting question Dark_Helmet Programming 3 06-26-2003 05:45 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:23 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
Open Source Consulting | Domain Registration