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`" All i want this to do is spit out the model number once. (I need to pass it to another script) |
Code:
model=$(cat /proc/cpuinfo | grep model | grep -v name | sed 's/^[^:]*: //') |
Hi,
model=$(awk '/model / { print $3}' /proc/cpuinfo) That is not a space after /model but a tab (ctrl-v + tab). Hope this helps. |
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 |
Code:
$ ruby -F":" -ane 'if $F[0][/model\s+(?!name)/] then puts $F[1];exit end' /proc/cpuinfo |
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 |
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 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 |
grep -m 1 model
will give you one instance of the match |
@ArfaSmif - how does your example provide the number at the end of the line?
What happens if "model name" comes first? |
Quote:
cat /proc/cpuinfo |grep -v "model name" | grep -m 1 model etc |
But you still have not returned what the OP requested :(
|
Quote:
So you can be happy, here goes :- model=`cat /proc/cpuinfo |grep -v "model name" | grep -m 1 model | cut -d: -f2` |
Quote:
Here's a solution using bash parameter substitution Code:
buf=$(cat /proc/cpuinfo); buf=${buf##*model name}; buf=${buf#*:}; echo ${buf%%$'\n'*} 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 |
Quote:
Code:
buf=$(</proc/cpuinfo) |
@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. |