[Solved ] - echo command to file ?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
stu90

[Solved ] - echo command to file ?

#1 Post 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.
Last edited by stu90 on Wed 30 Mar 2011, 16:49, edited 2 times in total.

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

echo command to file

#2 Post 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

stu90

#3 Post 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

:?

User avatar
CatDude
Posts: 1563
Joined: Wed 03 Jan 2007, 17:49
Location: UK

#4 Post 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
.
[img]http://www.smokey01.com/CatDude/.temp/sigs/acer-futile.gif[/img]

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

proc

#5 Post 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

potong
Posts: 88
Joined: Fri 06 Mar 2009, 04:01

#6 Post 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

stu90

#7 Post by stu90 »

Great stuff - thanks guys, these commands worked a treat.

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

Bruce B

#8 Post 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.

~

~

Post Reply