Page 1 of 1

[Solved ] - echo command to file ?

Posted: Tue 29 Mar 2011, 18:16
by stu90
Hello,

I am trying to echo a command to a file, the command is:

echo $(awk 'NR==7 { print $4,$2 }' /proc/cpuinfo)

What i would like to do is echo the actual command its self to the file and not the command output.

using this command:

echo 'echo $(awk 'NR==7 { print $4,$2 }' /proc/cpuinfo)' > /root/file

results in the /root/file entry

echo $(awk NR==7 { print , } /proc/cpuinfo)

which has several missing parts, any ideas ?

thanks.

echo command to file

Posted: Tue 29 Mar 2011, 19:36
by L18L
tried

Code: Select all

# echo 'echo $(awk "NR==7 { print $4,$2 }" /proc/cpuinfo)' > /root/file
results in:

echo $(awk "NR==7 { print $4,$2 }" /proc/cpuinfo)

Hope that helps

Posted: Wed 30 Mar 2011, 01:56
by stu90
Hi L18L

I just tried your suggested command but when pasted into terminal the command doesn't work. :(

i tried this slight variant which i think is what you probably meant - this does indeed copy to the file however it seems by using the quote marks in the command it alters the output.

echo $(awk "NR==7 { print $4 $2 }" /proc/cpuinfo)

which resulted in the whole line being printed and not the desired $4 and $2

whole line.
cpu MHz : 800.000
desired result.
800.000MHz

:?

Posted: Wed 30 Mar 2011, 09:31
by CatDude
Hello stu90

How about this ?

Code: Select all

echo "echo \$(awk 'NR==7 { print \$4,\$2 }' /proc/cpuinfo)" > /root/file
that results in this in the /root/file

Code: Select all

echo $(awk 'NR==7 { print $4,$2 }' /proc/cpuinfo)
which i believe is what you wanted.

CatDude
.

proc

Posted: Wed 30 Mar 2011, 09:36
by L18L
Hi stu90,

as I have no experience with awk I have done it by using grep and cut on my system:

Code: Select all

# echo `cat /proc/cpuinfo | grep 'cpu MHz' | cut -d ':' -f 2 ` MHz
1514.903 MHz
#
Note, it is not depending on "number 7" which might be changed in future versions.
Best regards

Posted: Wed 30 Mar 2011, 11:08
by potong
stu90:

For scripts that write scripts, I've found the here-document to be your friend.

Code: Select all

# cat /proc/cpuinfo 
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 7
model name      : Pentium III (Katmai)
stepping        : 3
cpu MHz         : 598.584
cache size      : 512 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 2
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 sep mtrr pge mca cmov pse36 mmx fxsr sse up
bogomips        : 1197.16
clflush size    : 32
power management:

# awk 'NR==7{print $4$2}' /proc/cpuinfo
598.584MHz
# cat <<\EOF >/tmp/file                 # N.B. '\' before EOF!
> awk 'NR==7{print $4$2}' /proc/cpuinfo
> EOF
# . /tmp/file
598.584MHz
# #
# # for a bash only solution try using arrays
# #
# O=$IFS IFS=$'\n' cpuinfo=($(</proc/cpuinfo )) IFS=$O
# cpuspeed=(${cpuinfo[6]})
# echo ${cpuspeed[3]}${cpuspeed[1]}
598.584MHz
# 
HTH

Potong

Posted: Wed 30 Mar 2011, 13:56
by stu90
Great stuff - thanks guys, these commands worked a treat.

I will mark this one as solved - cheers. 8)

Posted: Fri 01 Apr 2011, 16:42
by Bruce B
</proc/cpuinfo grep "cpu MHz"|awk '{print $4$2}'

outputs for me 2129.971MHz

Is that what is wanted?

The idea is to run as few commands as possible. Also, I'm going to setup a
small ramdisk for commonly used utilities and put it first in the path.


It takes a fraction of a second to have rc.local setup a ram disk

You make /var/bin as an example, then

mount -t tmpfs none /var/bin -o size=10m

For a 10mb dedicated RAM disk.

Put /var/bin as first in path statement in /etc/profile

Unzip your utilities package to /var/bin, adjust size as needed.

~

~