i have shell script , need make efficient. using temp files store , read data, need read data in memory.
it collects metrics postgres database using command , fetches metrics. current script fetches metrics temp file, reads it.
i want stop using temp files , use memory instead. script works, need automate more , rid of reading data temp files.
list item
input=`mktemp` #/usr/pgsql-9.5/bin/pgbench -c1 -j1 -t 1000 -s man > $input testtime=15 #seconds echo "waiting $testtime seconds..." /usr/pgsql-9.5/bin/pgbench -c1 -j1 -t $testtime -r man > $input oldifs=$ifs ifs=" " [ ! -f $input ] && { echo "$input file not found"; exit 99; } tps=`cat $input |awk '/^tps/ {print $3}' |awk -f'.' '{print $1}' |head -n1` update_l=`cat $input |awk '/update/ {print $1}' |tail -n1` select_l=`cat $input |awk '/select/ {print $1}' |tail -n1` insert_l=`cat $input |awk '/insert/ {print $1}' |tail -n1` echo ${plotter_prefix}.tps $tps kv echo ${plotter_prefix}.update_latency $update_l kv echo ${plotter_prefix}.select_latency $select_l kv echo ${plotter_prefix}.insert_latency $insert_l kv #{ while read line; # # statsite_builddata ${plotter_prefix}.latency average ${latency average} kv # echo ${plotter_prefix}.${line} kv # done } < $input statsite_senddata #echo $test ifs=$oldifs rm -f $input
you can capture output of command variable, so:
output=$(/usr/pgsql-9.5/bin/pgbench -c1 -j1 -t $testtime -r man)
then use echo
instead of cat
, substitute $input
variable name.
tps=`echo "$output" | awk '/^tps/ {print $3}' | awk -f'.' '{print $1}' |head -n1` update_l=`echo "$output" | awk '/update/ {print $1}' | tail -n1` ...
i suggest using $()
instead of surrounding commands backticks. above become:
tps=$(echo "$output" | awk '/^tps/ {print $3}' | awk -f'.' '{print $1}' |head -n1) update_l=$(echo "$output" | awk '/update/ {print $1}' | tail -n1) ...