LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Need some shelling scripting help.... (https://www.linuxquestions.org/questions/programming-9/need-some-shelling-scripting-help-835610/)

ProtoformX 10-01-2010 07:02 AM

Need some shelling scripting help....
 
I'm trying to retrieve a single string or value from /proc/cpuinfo, but when I try to extract the model (not the model name) the model name comes with it, also it likes to print it twice...


Code:

model="`cat /proc/cpuinfo | grep model | cut -d: -f2  | cut -c1-3`"
echo $model

What am I doing wrong?

All i want this to do is spit out the model number once. (I need to pass it to another script)

vonbiber 10-01-2010 07:17 AM

Code:

model=$(cat /proc/cpuinfo | grep model | grep -v name | sed 's/^[^:]*: //')
should do the job

druuna 10-01-2010 07:17 AM

Hi,

model=$(awk '/model / { print $3}' /proc/cpuinfo)

That is not a space after /model but a tab (ctrl-v + tab).

Hope this helps.

GrapefruiTgirl 10-01-2010 07:20 AM

It is printing it twice, I suspect because you have a dual core CPU (or two CPU's by some means).

Maybe this will give you an idea (just tossed together quick!):
Code:

awk '/^model[[:space:]]+:/{print $NF; exit}' /proc/cpuinfo

kurumi 10-01-2010 07:48 AM

Code:

$ ruby -F":" -ane 'if $F[0][/model\s+(?!name)/] then puts $F[1];exit end' /proc/cpuinfo

grail 10-01-2010 11:03 AM

I like the awk and ruby, but figure sed should get a go:
Code:

sed -r -n '/model\t/{s/[^0-9]+//p;q}' /proc/cpuinfo

GrapefruiTgirl 10-01-2010 11:14 AM

Hey wait, where's a plain bash version?
Code:

while read one two three; do if [ "$one" = "model" ]; then echo $three && break; fi done < /proc/cpuinfo
EDIT:
Seeing the newer posts further down made me realize that this code will also fail if someone's /proc/cpuinfo file produces "model name" before "model", so here's a modification, with a safeguard to ensure that only the "model" line will be given:
Code:

while read one two three; do [ "$one" = "model" -a "$two" = ":" ] && echo $three && break; done < /proc/cpuinfo

ArfaSmif 10-01-2010 07:32 PM

grep -m 1 model

will give you one instance of the match

grail 10-02-2010 12:51 AM

@ArfaSmif - how does your example provide the number at the end of the line?
What happens if "model name" comes first?

ArfaSmif 10-02-2010 09:23 PM

Quote:

Originally Posted by grail (Post 4115395)
@ArfaSmif - how does your example provide the number at the end of the line?
What happens if "model name" comes first?

yes only true of model comes first which it does on my system and if it doesn't then :-

cat /proc/cpuinfo |grep -v "model name" | grep -m 1 model

etc

grail 10-03-2010 01:17 AM

But you still have not returned what the OP requested :(

ArfaSmif 10-03-2010 04:15 AM

Quote:

Originally Posted by grail (Post 4116117)
But you still have not returned what the OP requested :(

I only wanted to show them how to use grep.

So you can be happy, here goes :-

model=`cat /proc/cpuinfo |grep -v "model name" | grep -m 1 model | cut -d: -f2`

catkin 10-03-2010 05:00 AM

Quote:

Originally Posted by ArfaSmif (Post 4116176)
I only wanted to show them how to use grep.

So you can be happy, here goes :-

model=`cat /proc/cpuinfo |grep -v "model name" | grep -m 1 model | cut -d: -f2`

That gives " 107" on my system.

Here's a solution using bash parameter substitution
Code:

buf=$(cat /proc/cpuinfo); buf=${buf##*model name}; buf=${buf#*:}; echo ${buf%%$'\n'*}
It could be one step shorter if the number of spaces after "model name" and before ":" is taken as fixed or if extended pattern matching (extglob) is enabled.

EDIT: oops! I mis-read the thread and did not realise it was "model" not "model name" that is required.

EDIT2: second attempt:
Code:

shopt -s extglob
buf=$(cat /proc/cpuinfo); buf=${buf##*model+([$'\t' ]): }; echo "${buf%%$'\n'*}"


ghostdog74 10-03-2010 06:12 AM

Quote:

Originally Posted by catkin (Post 4116189)
EDIT2: second attempt:
Code:

shopt -s extglob
buf=$(cat /proc/cpuinfo); buf=${buf##*model+([$'\t' ]): }; echo "${buf%%$'\n'*}"


Code:

buf=$(</proc/cpuinfo)

ArfaSmif 10-03-2010 06:32 AM

@catkin, looks like /proc/cpuinfo produces very different output for different cpus etc. I have an AMD Athlon(tm) II X4 630 Processor and my bash script works ok for my cpu. My output is "5" which is what I thought ProtoformX was trying to get.


All times are GMT -5. The time now is 06:45 AM.