LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash scripting to find CPU info and perform action (https://www.linuxquestions.org/questions/linux-newbie-8/bash-scripting-to-find-cpu-info-and-perform-action-4175453255/)

DavidDiepUSC 03-08-2013 07:20 AM

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!

TobiSGD 03-08-2013 07:34 AM

So what have you done so far and where are you hitting problems?

yowi 03-08-2013 07:37 AM

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

shivaa 03-08-2013 09:41 AM

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


DavidDiepUSC 03-08-2013 02:28 PM

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

DavidDiepUSC 03-08-2013 02:37 PM

#!/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?

DavidDiepUSC 03-08-2013 02:53 PM

#!/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"]

suicidaleggroll 03-08-2013 03:24 PM

Code:

if [$x = $ID]
should be
Code:

if [[ "$x" == "$ID" ]]
http://tldp.org/LDP/abs/html/

DavidDiepUSC 03-08-2013 04:35 PM

That did the trick... and thank you for the links. Good reference tools.

DavidDiepUSC 03-08-2013 06:05 PM

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

yowi 03-09-2013 04:55 AM

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


All times are GMT -5. The time now is 04:22 AM.