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 03-08-2013, 07:20 AM   #1
DavidDiepUSC
Member
 
Registered: Jan 2008
Posts: 58

Rep: Reputation: 0
Bash scripting to find CPU info and perform action


Hi

I want to create a bash script that would take the CPU ID (either by looking in /proc/cpuinfo or by issuing a vmcp command) and then issuing a command (i.e. ifconfig ethx up).

I want to put this somewhere during the boot process.

Thanks!
 
Old 03-08-2013, 07:34 AM   #2
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,133
Blog Entries: 2

Rep: Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836Reputation: 4836
So what have you done so far and where are you hitting problems?
 
1 members found this post helpful.
Old 03-08-2013, 07:37 AM   #3
yowi
Member
 
Registered: Dec 2002
Location: Au
Distribution: Debian
Posts: 209

Rep: Reputation: 54
Go on then...

Post it here when you get stuck and we'll help you get it fixed.

And I'm going to link ESR here:
http://www.catb.org/esr/faqs/smart-questions.html
 
Old 03-08-2013, 09:41 AM   #4
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Please do not expect a readymade script, as it will give you nothing to learn. But do some hit & trials, search over google, follow any guides... and then if you stuck with any command or syntax or symbols... then we're here to help you.

A shell script is nothing, but a list of commands, but in correct order.

In the meantime, I will give you some hints:
Code:
#!/bin/bash                           # Script interpreter
ID=$(grep 'some-id' /proc/cpuinfo)    # Strong some-id's value in a variable named ID
ifconfig ethx up                      # Some operation
 
Old 03-08-2013, 02:28 PM   #5
DavidDiepUSC
Member
 
Registered: Jan 2008
Posts: 58

Original Poster
Rep: Reputation: 0
Thanks guys... though I've coded everything from Cobol to C++, I'm not too sure about shell scripting.

With that said, here is my embarrassing attempt:

#!/bin/bash
x = FF265
ID = (Not sure how to extract CPU ID from /proc/cpuinfo)

if (x = ID)
ifconfig eth0 up

else
ifconfig eth1 up
 
Old 03-08-2013, 02:37 PM   #6
DavidDiepUSC
Member
 
Registered: Jan 2008
Posts: 58

Original Poster
Rep: Reputation: 0
#!/bin/bash
x = FF265
ID = grep -w "FF265" /proc/cpuinfo

if (x = ID)
ifconfig eth0 up

else
ifconfig eth1 up


Now... any ideas on how I can test this?
 
Old 03-08-2013, 02:53 PM   #7
DavidDiepUSC
Member
 
Registered: Jan 2008
Posts: 58

Original Poster
Rep: Reputation: 0
#!/bin/bash
x=02FAC6
ID=$(grep -w "02FAC6" /proc/cpuinfo)
if [$x = $ID]; then
echo "02FAC6"
else
echo "not 02FAC6"
fi


Its not liking the comparison on line 4?

./HANetwork.sh: line 4: [02FAC6: command not found[COLOR="Silver"]

Last edited by DavidDiepUSC; 03-08-2013 at 02:55 PM.
 
Old 03-08-2013, 03:24 PM   #8
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,463

Rep: Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066Reputation: 2066
Code:
if [$x = $ID]
should be
Code:
if [[ "$x" == "$ID" ]]
http://tldp.org/LDP/abs/html/
 
Old 03-08-2013, 04:35 PM   #9
DavidDiepUSC
Member
 
Registered: Jan 2008
Posts: 58

Original Poster
Rep: Reputation: 0
That did the trick... and thank you for the links. Good reference tools.
 
Old 03-08-2013, 06:05 PM   #10
DavidDiepUSC
Member
 
Registered: Jan 2008
Posts: 58

Original Poster
Rep: Reputation: 0
This is what I ended up with and it is exactly what I'm looking for. Thanks again for all your help... sorry if the initial question was bad.

#!/bin/bash
#
# Used to extract CPU ID from /proc/cpuinfo
# Compares with x and based on that starts appropriate
# Network Interface
#
x='02FAC6'
ID=$(grep -i -r -o -m 1 '02FAC6' /proc/cpuinfo)
if [[ "$x" = "$ID" ]]; then
ifconfig eth0 up
else
ifconfig eth1 up
fi
 
Old 03-09-2013, 04:55 AM   #11
yowi
Member
 
Registered: Dec 2002
Location: Au
Distribution: Debian
Posts: 209

Rep: Reputation: 54
Code:
x='02FAC6'  
if grep -iq "$x" /proc/cpuinfo; then 
  ifup eth0
else
  ifup eth1
fi
slightly cleaner and ifup rather than ifconfig.

Shell is a subtle beast, there's lots of nuances and tricks that are easiest to pick up from a good book.
This is the one on my bookshelf:
http://www.amazon.com/Unix-Shell-Pro.../dp/0672324903

Last edited by yowi; 03-09-2013 at 05:08 AM.
 
  


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
[SOLVED] Perform an action on a list/array of variables from user input in bash eamesj Programming 1 12-30-2012 10:22 PM
BASH Scripting: Cutting specific info from df command mcgregor Programming 4 04-19-2011 11:46 AM
You are not authorized to perform this action RockyRX Linux - Newbie 9 07-22-2010 09:50 PM
how to find out about platform, cpu info etc. nasridine Linux - Newbie 3 01-21-2010 12:51 AM
Perform action based on email txt plisken Linux - General 1 03-06-2007 09:49 PM


All times are GMT -5. The time now is 08:15 AM.

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