linux - Passing IPC(Instructions/Cycles) continuously to other function or variable -
i trying read performance counters , ipc. need use ipc control few machine specific parameters. using shell script same. please see code below:
while true retval=./perf periodic -e instructions -e cycles -s 50 -d -m td & some_pid=$! kill some_pid if ["$retval" -gt "0.5"] ***something*** fi sleep 1 done
i getting following error:
algorithm.sh[27]: kill: some_pid: arguments must jobs or process ids algorithm.sh[27]: periodic: not found algorithm.sh[27]: [: missing ] algorithm.sh[27]: kill: some_pid: arguments must jobs or process ids algorithm.sh[27]: [: missing ] algorithm.sh[27]: periodic: not found algorithm.sh[27]: kill: some_pid: arguments must jobs or process ids algorithm.sh[27]: [: missing ]
can give me pointers on how get/return value perf instruction. tried using function , returning value, failed.
---------updated----------
now running following, , 1 of problem got solved , 1 remaining.
./perf periodic -e instructions -e cycles -s 50 -d -m td > result.txt &
and other 1
while true retval=$(tail -n 1 result.txt) echo $retval if ["$retval" -gt "0.5"] echo "hello mate" fi sleep 1 done
the echo giving value, if statement not getting executed. giving following:
algorithm.sh[30]: [: missing ] 0.302430 algorithm.sh[30]: [0.302430: not found 0.472716 algorithm.sh[30]: [0.472716: not found 0.475687 algorithm.sh[30]: [0.475687: not found
i looked if condition syntax , couldn't spot mistake. please help.
couple of shell syntax issues here.
first, retval=...
going set retval
variable equal first part of string on right side of '='. ampersand background whole thing, throwing value away. meant do:
retval=`./perf periodic -e instructions -e cycles -s 50 -d -m td`
which store output of perf
command retval
. however, won't work if put background '&'. you'll need either (a) run synchronously without '&' i've shown above, (b) redirect output file , recover after it's finished (you'll need use wait
determine when that's happened), or (c) use "coprocess" (too complicated explain here: see bash man page).
also, meant kill $some_pid
? without '$', string "some_pid"
passed literal argument kill
, not intended.
edit
following revisions... shell operates splitting command line individual tokens. spaces important. in case, initial token being identified shell combined value of ["$retval"
(after variable substitution , quote removal). last token 0.5]
after removal of quotes. in first invocation line then, first token '[' (presumably retval
empty first time through). there it's complaining last token not being matching ']'. in other iterations, first token '[' plus additional numeric text $retval
not providing valid command name.
once fix that, you'll discover -gt
operator evaluates integer comparisons. use bc(1)
command. example, command produce output of 1
if $retval
greater 0.5; otherwise 0
.
echo "$retval > 0.5" | bc
but note you'll need ensure retval
has valid numeric expression or you'll cause syntax error in bc
. need capture output , put conditional. should work:
if [ "$retval" ] x=$(echo "$retval > 0.5" | bc) if [ $x -eq 1 ] echo "hello mate" fi fi
(note $(...)
don't need additional spaces next parentheses. , in assignment statement x=foo
, must not have space on either side of =
.)
Comments
Post a Comment