Showing posts with label bc. Show all posts
Showing posts with label bc. Show all posts

Saturday, May 1, 2010

Computations in linux command line

The bc command is useful to perform some simple arithemetical calculations in console. It may be used in scripts too.

For example, percent values from my previous post have been obtained as

$> echo '265594/292891' | bc -l
$> echo '262938/292891' | bc -l

Argument of echo contains an arithmetic express, and echo pipes it to the bc with standard math library attached (-l).

Do you want to know the Euler's number e (the base of the natural logarithm)?

$> echo 'e(1)' | bc -l

Let's print first 80 digits of π (pi) using arctangent function (a(1.0)):

$> echo 'scale=80; 4.0*a(1.0)' | bc -l

The bc has interactive mode as well; just type

$> bc

In order to quit from interactive mode, type quit or exit.

Sunday, November 8, 2009

How to fix some sort of mp3-files

#!/bin/bash

# 
# I had some mp3-files that were repeated twice inside themselfes.
# It looked like someone copied them one day with append command.
# The script splits such files into to equal parts and checks if
# those parts are coinside.
#

for f in *.mp3 ; do
    # Calculate size of the file
    size=`du -b "${f}" | cut -f 1`
    half_size=`echo ${size}/2 | bc`

    # Split file into two parts
    f1="$f".1
    f2="$f".2
    head -c $half_size "$f" > "${f1}"
    tail -c $half_size "$f" > "${f2}"

    # Calculate checksums and check if they coinside
    md1=`md5sum "${f1}" | cut -f 1 -d " "`
    md2=`md5sum "${f2}" | cut -f 1 -d " "`
    if [ ${md1} != ${md2} ] ; then
        echo "Problems with $f"
    fi
done