LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2010, 07:02 AM   #1
ProtoformX
Member
 
Registered: Feb 2004
Location: Canada
Distribution: LFS SVN
Posts: 334

Rep: Reputation: 34
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)
 
Old 10-01-2010, 07:17 AM   #2
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
Code:
model=$(cat /proc/cpuinfo | grep model | grep -v name | sed 's/^[^:]*: //')
should do the job
 
Old 10-01-2010, 07:17 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

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

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

Hope this helps.
 
Old 10-01-2010, 07:20 AM   #4
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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
 
Old 10-01-2010, 07:48 AM   #5
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Code:
$ ruby -F":" -ane 'if $F[0][/model\s+(?!name)/] then puts $F[1];exit end' /proc/cpuinfo
 
Old 10-01-2010, 11:03 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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
 
Old 10-01-2010, 11:14 AM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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

Last edited by GrapefruiTgirl; 10-03-2010 at 07:19 AM.
 
1 members found this post helpful.
Old 10-01-2010, 07:32 PM   #8
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
grep -m 1 model

will give you one instance of the match
 
Old 10-02-2010, 12:51 AM   #9
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
@ArfaSmif - how does your example provide the number at the end of the line?
What happens if "model name" comes first?
 
Old 10-02-2010, 09:23 PM   #10
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
Quote:
Originally Posted by grail View Post
@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

Last edited by ArfaSmif; 10-02-2010 at 09:30 PM. Reason: more info added for clarity
 
Old 10-03-2010, 01:17 AM   #11
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
But you still have not returned what the OP requested
 
Old 10-03-2010, 04:15 AM   #12
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

Rep: Reputation: 70
Wink

Quote:
Originally Posted by grail View Post
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`
 
Old 10-03-2010, 05:00 AM   #13
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by ArfaSmif View Post
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'*}"

Last edited by catkin; 10-03-2010 at 05:22 AM. Reason: Added extglob
 
Old 10-03-2010, 06:12 AM   #14
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by catkin View Post
EDIT2: second attempt:
Code:
shopt -s extglob
buf=$(cat /proc/cpuinfo); buf=${buf##*model+([$'\t' ]): }; echo "${buf%%$'\n'*}"
Code:
buf=$(</proc/cpuinfo)
 
1 members found this post helpful.
Old 10-03-2010, 06:32 AM   #15
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora, Centos, Manjaro
Posts: 317

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


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
Firefox Scripting Add-on (Scripting HTML / Javascript inside Firefox) linuxbeatswindows Programming 1 09-18-2009 10:09 PM
Scripting in C - need help hifivoyager Linux - Newbie 9 02-06-2009 08:47 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
scripting help Abe_the_Man Linux - General 1 11-03-2004 05:30 PM
Shelling df dsheller Programming 4 06-20-2004 06:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:21 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