понедельник, 14 октября 2013 г.

скриптуем математику в bash

юзается так:
$ seq 0 99 | ./test_seq.sh is_prime 

Сам скрипт выглядит так... Для проверки простых чисел используется калькулятор для командной строки o.0
#!/bin/bash

even_counter=$(( 2-$1%2  ))

function is_prime {
    number=
    if(( $1 < 0 )); then
        number=$(($1*-1))
    else
        number=$1
    fi
    if(( $number == 1 || $number == 0)); then
        echo $1
        return
    fi
    # а это строчка проверки числа, простое ли оно...
    result=`seq -s "*" 1 $(($number-1)) | 
sed -r "s/([0-9,\*]+)/(\1+1)%$number/" | bc`
    # перенос строчки чисто чтобы в блог нормально влезло

    #echo "[[$result]] for $number"
    if(( $result == 0 )); then
        echo $1
    fi
}

function is_even {
    if(( $even_counter == 2 )); then
        echo $1
        even_counter=1
    else
        even_counter=$(($even_counter + 1))
    fi
}

function is_not_even {
    if(( $even_counter == 1 )); then
        echo $1
        even_counter=$(($even_counter + 1))
    else
        even_counter=1
    fi
}

echo "filtering by: $1"
for item in $(cat)
do
    # echo "Item: $item"
    cmd="$1 $item"
    eval "$cmd"
    # eval $1
done

1 комментарий:

  1. And our personal previous research on illegal playing utilized a readable transformation method . It presents completely different coupons and bonuses for the users and that increases the chances of wins 온라인카지노 as well. Are you trying ahead to some dependable assets to play Casino Korea on? If you are be} about to say sure, for these questions, we shall be happy to help you|that will help you|that can assist you} out in the identical. We will let you know right here the easiest way to earn cash on-line by enjoying in} on line casino Korea on one of the trusted platforms and that's 우리카지노.

    ОтветитьУдалить