Tuesday, February 7, 2017

Bc as CPU benchmark - revisited

Recently I found little script I wrote that may be helpful benchmarking machines with bc:

if [ -n "$1" ]; then
   PRECISION="$1"
else
   PRECISION=10000
fi

CPUINFO="/proc/cpuinfo"
MEMINFO="/proc/meminfo"

echo "Linux shell CPU benchmark"
echo "------------------------------------"
echo "Usage: $0 "
echo

BCCMD=$(which bc)
if [ -z "$BCCMD" ]; then
        echo "bc command not found... exiting now"
        exit
fi

echo -n "Getting CPU data.... "
#get cpu data
if [ -r $CPUINFO ]; then
        VENDOR=$(cat "$CPUINFO" | grep vendor_id | head -n1 | cut -f2 -d':'| cut -c2- )
        MODEL=$(cat "$CPUINFO" | grep "model name" | head -n1 | cut -f2 -d':' | cut -c2- | sed -e 's/  */ /g')
        SPEED=$(cat "$CPUINFO" | grep "cpu MHz" | head -n1 | cut -f2 -d':' | cut -c2- )
        CACHE=$(cat "$CPUINFO" | grep "cache size" | head -n1 | cut -f2 -d':' | cut -c2-)
        LMFLAG=$(cat "$CPUINFO" | grep flags | head -n1 | cut -f2 -d':' | tr ' ' '\n' | grep lm)

        if [ "$LMFLAG" = "lm" ]; then
                CPU64="1"
        else
                CPU64="0"
        fi
        echo "[OK]"
else
        CPU64=""
        echo "[NOT FOUND]"
fi


echo -n "Getting memory data..."
#get memory data
if [ -r "$MEMINFO" ]; then
        MEMTOTAL=$(cat "$MEMINFO" | grep MemTotal | cut -f2 -d':' | cut -c2- | sed -e 's/ *//g')
        echo "[OK]"
else
        MEMTOTAL=""
        echo "[NOT FOUND]"
fi
echo -n "Getting machine, OS and Kernel data..."
#get machine,os,kernel data
UNAME_CMD=$(which uname)
if [ -n "$UNAME_CMD" ]; then
        #MACHINE=$($UNAME_CMD -m)
        OS=$($UNAME_CMD -o)
        KERNEL=$($UNAME_CMD -r)
        echo "[OK]"
else
        OS=""
        KERNEL=""
        echo "[NOT FOUND]"
fi

#get distro name
LSBRELEASE=$(which lsb_release 2>/dev/null)
if [ -n "$LSBRELEASE" ]; then
    DISTRO=$($LSBRELEASE -d | cut -f2 -d':' | tr -d '\t')
elif [ $(ls /etc/*version 2>/dev/null | wc -l) -gt 0 ]; then
    DISTRO=$(cat $(ls /etc/*version | head -n1))
elif [ $(ls /etc/*release 2>/dev/null | wc -l) -gt 0 ]; then
    DISTRO=$(cat $(ls /etc/*release | head -n1))
elif [ -f /proc/version ]; then
    DISTRO=$(cat /proc/version)
else
    echo "[NOT FOUND]"
    DISTRO=""
fi


echo -n "Benchmarking (precision=$PRECISION)..."
#benchmarking
if [ -n "$BCCMD" ]; then
  SCORE=$((time echo "scale=$PRECISION;a(1)*4" | $BCCMD -l) 2>&1 | grep real | sed -e 's/  */ /g' | tr '\t' ' ' | cut -f2 -d' ')
  #echo "SCORE#$SCORE"
  echo "[OK]"
else
  SCORE=""
  echo "[ERROR]"
fi

#print values
echo
echo "--- cut here ---"
echo "DISTRO#$DISTRO"
echo "OS#$OS"
echo "KERNEL#$KERNEL"
echo "VENDOR#$VENDOR"
echo "MODEL#$MODEL"
echo "SPEED#$SPEED"
echo "CACHE#$CACHE"
echo "CPU64#$CPU64"
echo "MEMTOTAL#$MEMTOTAL"
echo "PRECISION#$PRECISION"
echo "SCORE#$SCORE"
echo "--- cut here ---"

Thursday, November 17, 2016

Summing numbers in a column

To sum several values in column from file we can use paste command and bash arithmetic expressions

$ echo $(($(paste -s -d'+' nums)))

I've always used tr to glue the values into one line, but the remaining '+' sign must be removed:

$ echo $(($(cat nums | tr '\n' '+' |sed 's/.$//')))

Or with awk:

$ awk '{s+=$1}; END { print s}' nums




Saturday, July 16, 2016

wget and line delimiters CR LF

If you download file with wget and the filename comes from windows file, the output filename will always have %0D at the end. To get rid of this, instead of:

wget $filename

use

wget $(echo "$filename" | tr -d '\r')

This will do the trick

Sunday, March 6, 2016

Raspberry Pi - booting into console mode

By default Raspbian boots into GUI mode. I don't need that. I wanted to change it to boot into command line. In Slackware or RedHat distros I always did it by editing runlevel in /etc/inittab.
Raspbian is different. I did it with raspi-config utility:

$ sudo raspi-config

And then I chose "Boot options"=>"B2 Console Autologin Text console, automatically logged in as 'pi' user"

Sunday, February 21, 2016

InsecureMag - downloading all issues

I bumped into InsecureMag (https://www.helpnetsecurity.com/insecuremag-archive/) - the free magazine on security. As I'm interested in it I decided to check it out. It's totally free. But being lazy I wanted to have all the issues on my harddisk. As usual bash comes with help:

#!/bin/bash
for i in {1..49}; do
  link="https://www.helpnetsecurity.com/dl/insecure/INSECURE-Mag-$i.pdf"
  wget -c "$link"
done


Have a nice reading :)