Dec 30, 2013

Unix as IDE. History

Execute last command again:
sudo !!
echo !! > new_script.sh
!! | grep Linux

Execute last command started with ls:
!ls

Arguments:
ls !^  # first arg of prev cmd
ls !$  # last arg of prev cmd
ls !*  # all args of prev cmd
ls !ls:2  # second arg of prev ls cmd

Execute command with given history number:
!2039

Hide command from history (use space before cmd):
 ls ~/eagle

Dec 28, 2013

User-level threads....... with threads. - Paul Turner - Google

How to use git with gnome-keyring integration

sudo apt-get install libgnome-keyring-dev
cd /usr/share/doc/git/contrib/credential/gnome-keyring
sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/gnome-keyring/git-credential-gnome-keyring

Dec 19, 2013

kernel man 9

make mandocs
make installmandocs

Dec 11, 2013

pyopenssl

Use 
pip install pyopenssl==0.12.0

when you got such exception on Centos 5.9:

$ pip install pyopenssl
Downloading/unpacking pyopenssl
  Downloading pyOpenSSL-0.13.1.tar.gz (254kB): 254kB downloaded
  Running setup.py egg_info for package pyopenssl
    warning: no previously-included files matching '*.pyc' found anywhere in distribution
Installing collected packages: pyopenssl
  Running setup.py install for pyopenssl
    building 'OpenSSL.crypto' extension

...........

gcc -pthread -shared build/temp.linux-x86_64-2.6/OpenSSL/rand/rand.o build/temp.linux-x86_64-2.6/OpenSSL/util.o -L/usr/lib64 -lssl -lcrypto -lpython2.6 -o build/lib.linux-x86_64-2.6/OpenSSL/rand.so

building 'OpenSSL.SSL' extension

creating build/temp.linux-x86_64-2.6/OpenSSL/ssl

gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -I/usr/kerberos/include -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/python2.6 -c OpenSSL/ssl/connection.c -o build/temp.linux-x86_64-2.6/OpenSSL/ssl/connection.o

OpenSSL/ssl/connection.c: In function ‘ssl_Connection_set_context’:

OpenSSL/ssl/connection.c:289: warning: implicit declaration of function ‘SSL_set_SSL_CTX’

OpenSSL/ssl/connection.c: In function ‘ssl_Connection_get_servername’:

OpenSSL/ssl/connection.c:313: error: ‘TLSEXT_NAMETYPE_host_name’ undeclared (first use in this function)

OpenSSL/ssl/connection.c:313: error: (Each undeclared identifier is reported only once

OpenSSL/ssl/connection.c:313: error: for each function it appears in.)

OpenSSL/ssl/connection.c:320: warning: implicit declaration of function ‘SSL_get_servername’

OpenSSL/ssl/connection.c:320: warning: assignment makes pointer from integer without a cast

OpenSSL/ssl/connection.c: In function ‘ssl_Connection_set_tlsext_host_name’:

OpenSSL/ssl/connection.c:346: warning: implicit declaration of function ‘SSL_set_tlsext_host_name’

error: command 'gcc' failed with exit status 1

----------------------------------------
Cleaning up...
Command /home/.../app/bin/python2.6 -c "import setuptools;__file__='/home/.../app/build/pyopenssl/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-vBzwJt-record/install-record.txt --single-version-externally-managed --install-headers /home/.../app/include/site/python2.6 failed with error code 1 in /home/.../app/build/pyopenssl
Traceback (most recent call last):
  File "/home/.../app/bin/pip", line 8, in <module>
    load_entry_point('pip==1.4.1', 'console_scripts', 'pip')()
  File "/home/.../app/lib/python2.6/site-packages/pip/__init__.py", line 148, in main
    return command.main(args[1:], options)
  File "/home/.../app/lib/python2.6/site-packages/pip/basecommand.py", line 169, in main
    text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 42: ordinal not in range(128)

Dec 5, 2013

Linux I/O Scheduler

Selecting a Linux I/O Scheduler
How do I change I/O scheduler?

cat /sys/block/sda/queue/scheduler
echo noop > /sys/block/sda/queue/scheduler
In /etc/default/grub change
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
to
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash elevator=noop"
Then run
sudo update-grub2
and restart.

Nov 15, 2013

Введение в операционные системы - Столяров А.В.

http://www.stolyarov.info

Мультизадачность
  • Пакетный режим
  • Режим разделения времени
  • Планирование в режме реального времени
Требования к аппаратуре
  • Аппарат прерываний
  • Защита памяти
  • Привилегированный и ограниченный режимы
  • Таймер

Прерывания
  • Аппаратные (внешние)
  • Внутренние (в процессоре на исключительные ситуации, например деление на ноль)
  • Программные (системные вызовы)
Эмуляция физического компьютера  возможна за счет эмитации прерываний.

Проблемы решаемые менеджером памяти:
  • защита процессов др. от др. и от ОС. управление аппаратной защитой памяти
  • откачка при недостатке объема опертивной памяти
  • дублирование данных при, например, запуске нескольких копий программы
  • перемещение кода
  • фрагментация
Виртуальная память
  • база и предел
  • сегментная организация
  • страничная
  • сегментно-страничная

Драйверы (в ядре, при запуске, динамич. загружаемые), контроллеры как средства абстрагирования, буфера ввода-вывода (асинхронный режим)
Ввод-вывод



Процессы




Файловая система
Директория (имена файлов и их номера)
Файл. Индексный дескрптор (i-node или index node) - вся информация (права, дата...)
Ссылка (счетчик ссылок на i-node)

Семафоры и мьютексы
Мьютексы lock() unlock()
Семафоры up() увелич. некот знач. на 1; down()
Должны быть реализоваы атомарно

Графический интурфейс
Х-сервер

Nov 13, 2013

Программирование на языке ассемблера NASM для ОС Unix - Столяров А.М.

http://www.stolyarov.info/books/asm_unix

Кольца защиты процессора

nasm -f elf hello.asm
ld -m elf_i386 hello.o -o hello
./hello
%include "stud_io.inc"
global _start


section .data
string resb 20
count  resw 256
x      resd 1
fibon  dw 1, 1, 2, 3, 5, 8, 13, 21
msg    db "Hello World"

section .bss
set512 resd 16

section .text
_start: mov  eax, 0
        mov  eax, ebx
        mov  eax, [count]  ; значение count в eax
        mov  eax, count    ; адрес count в eax
        mov  eax, [ebx]    ; из ячейки памяти с адресом ebx

; Арифметика
        add edx, 12
        sub [x], ecx
        inc eax
        dec eax

; Безусловные переходы
        jmp some_label
        jmp eax
        jmp [eax]
        jmp short some_label

; Условные переходы        
some_label: cmp eax, 5
            je equal_label
            jl less_label
            jng not_greater_label
            jnz some_label

; Циклы
        mov ecx, 100
        mov esi, array
        mov eax, 0
lp:     add eax, [esi]
        add esi, 4
        loop lp  

; Побитовые xor, or, and
        xor eax, eax

; Операции сдвига shr (shift right), shl (shift left)
        shr edx, 5
Стек используется при вызовах подпрограмм для хранения адресов возврата, для передачи фактических параметров в подпрограммы и для хранения локальных переменных
        push eax
        pop eax

        pushad ; push all doublewords
        popad  ; для всех регистров: EAX, ECX, EDX, EBX, ESP, EBP, ESI, EDI

        pushfd ; для регистра флагов EFLAGS
        popfd
Подпрограммы
; fill memory (edi=address, ecx=length, al=value)
fill_memory:
        jecxz fm_q
fm_lp:  mov   [edi], al
        inc edi
        loop fm_lp
fm_q:   ret

; Использование
        mov edi, my_array
        mov ecx, 256
        mov al, '@'
        call fill_memory

        push ebp
        mov ebp, esp
        mov esp, 16 ; 16 - объем памяти под локальные переменные

; после подпрограммы
        mov esp, ebp
        pop ebp
        ret

Макросы
%macro pcall1 2 ; 2 кол-во параметров
        push %2
        call %1
        add esp, 4
%endmacro

; вызов
pcall1 proc, eax


%define arg1 ebp+8

; вызов
        mov eax, [arg1]


%ifdef DEBUG_PRINT
        PRINT "Entering"
%endif
Макросимвол можно определить как ключ коммандной строки
nasm -f elf -dDEBUG_PRINT hello.asm
Операционная система
Аппаратная поддержка мультизадачности:
  • аппарат прерываний
  • защита памяти
  • привилегированный и ограниченный режим работы центрального процессора
  • таймер

        int 80h ; Прерывание
Последовательность:
  • Запрос прерывания
  • Процессор доводит выполнение текущ программы до точки в кот можно прервать. На шине выставляет подтверждене прерывания. Др. прерывания блокируются
  • Устройство передает число идентифицирующие данное устройство - номер прерывания
  • Процессор сохраняет в стеке активной задачи счетчик комманд и регистр флагов
  • Устанавливается привилег. режим центр. процессора. Управление передается на точку входа обработчика прерываний (процедуры операционной системы). Адрес процедуры в спец. области памяти

Nov 12, 2013

Nasm "Hello world"

PC Assembly Language is resource on assembly programming. It's modern, free, and covers a variety of topics. It uses NASM and the examples are available for Linux (among several other systems).

hello-world-nasm-linux
64-bit-hello-world-in-linux-assembly-nasm

Docs

http://www.nasm.us/docs.php

x64
Gentle Introduction to x86-64 Assembly
64-bit Linux system call numbers
Intel® 64 and IA-32 Architectures Developer's Manual: Vol. 2B

RabbitMQ different verisons on Centos 5.9 in Virtualbox

Download Erlang source: https://www.erlang-solutions.com/downloads/download-erlang-otp

./configure && make && su -c "make install"

Download RabbitMQ required server version(s): http://www.rabbitmq.com/releases/rabbitmq-server/
Configure each instance
cd <RABBIT_PATH>/sbin

rabbitmq-plugins enable rabbitmq_management

rabbitmqctl add_user <RABBIT_USER> <RABBIT_PASS>
rabbitmqctl add_vhost /<RABBIT_VHOST>
rabbitmqctl set_permissions -p /<RABBIT_VHOST> <RABBIT_USER> ".*" ".*" ".*"
rabbitmqctl set_user_tags <RABBIT_USER> administrator

./rabbitmq-server

Configure port forwarding in VirtualBox

Oct 17, 2013

Cocaine

Cocaine — это PaaS (Platform-as-a-Service) система с открытым исходным кодом, позволяющая создавать аналоги Google AppEngine или Heroku.
https://github.com/cocaine/cocaine-docs-ru/wiki

Oct 12, 2013

Find out current working directory of a running process?

pwdx <PID>
lsof -p <PID> | grep cwd
readlink -e /proc/<PID>/cwd

Sep 23, 2013

Linux and Python timezones

$ TZ=KST-9 date
# Thu Oct  3 15:51:21 KST 2013
$ date
# Thu Oct  3 09:51:23 FET 2013
$ ls /usr/share/zoneinfo
Africa      Asia       Canada
.....
$ ls /usr/share/zoneinfo/Europe
Amsterdam  Belgrade    Bucharest  Copenhagen  Helsinki
.....
$ cat /etc/timezone
Europe/Minsk
python -c "import pytz;print pytz.all_timezones"
['Africa/Abidjan', 'Africa/Accra', 'Africa/Addis_Ababa', 'Africa/Algiers',
.....
import datetime, pytz
korea_tz = pytz.timezone('Asia/Seoul')
korea_time = korea_tz.localize(datetime.datetime(2013, 10, 2, 14, 0))
# datetime.datetime(2013, 10, 2, 14, 0, tzinfo=<DstTzInfo 'Asia/Seoul' KST+9:00:00 STD>)
local_time = korea_time.astimezone(pytz.timezone('Europe/Minsk'))
# datetime.datetime(2013, 10, 2, 8, 0, tzinfo=<DstTzInfo 'Europe/Minsk' FET+3:00:00 STD>)
List of time zone abbreviations
List of time zones by country

Sep 19, 2013

Dead snakes

sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python2.4 python2.4-dev

Sep 10, 2013

Python API

http://www.pythonapi.com/

Ранжирование языков программирования по их выразительности (Programming languages ranked by expressiveness)

http://dev.by/blogs/main/ranzhirovanie-yazykov-programmirovaniya-po-ih-vyrazitelnosti
http://redmonk.com/dberkholz/2013/03/25/programming-languages-ranked-by-expressiveness/

Если для вашего проекта требуется выразительный язык, по которому сравнительно несложно найти специалистов, обратите пристальное внимание на Python. ;)

Sep 3, 2013

Pythonbrew

This project is no longer under active development.
You are encouraged to try out pyenv instead.

Aug 30, 2013

Explain Shell

http://explainshell.com/

This site contains 29761 parsed manpages from sections 1 and 8 found in Ubuntu's manpage repository. A lot of heuristics were used to extract the arguments of each program, and there are errors here and there, especially in manpages that have a non-standard layout.

It is written in Python and uses a bit of NLTK (to find the interesting parts of the manpage), a little d3.js (for the connecting lines graphic) and Flask. It is served with uwsgi and nginx. I will open source the code in the near future, when I have time to clean it up a bit.


Jul 28, 2013

smiley – Python Application Tracer

Smiley is a tool for spying on your Python programs and recording their activities. It can be used for post-mortem debugging, performance analysis, or simply understanding what parts of a complex program are actually used in different code paths.
Docs Code

Jul 19, 2013

Web Framework Benchmarks

http://www.techempower.com/benchmarks/#section=data-r6&hw=ec2&test=query

My gnome extensions


Terminator

  • Arrange terminals in a grid 
  • Tabs 
  • Drag and drop re-ordering of terminals 
  • Lots of keyboard shortcuts 
  • Save multiple layouts and profiles via GUI preferences editor 
  • Simultaneous typing to arbitrary groups of terminals
sudo apt-get install terminator
gsettings set org.gnome.desktop.default-applications.terminal exec terminator

Jul 4, 2013

Django model mixins and utilities.

https://bitbucket.org/carljm/django-model-utils

I want to know when a running process will terminate. How can I watch it?

http://askubuntu.com/questions/313138/i-want-to-know-when-a-running-process-will-terminate-how-can-i-watch-it
while [ -d /proc/ ]; do sleep 1; done; echo "Process finished at $(date +"%F %T")."
Or you can make a bash script:
#!/bin/bash

if [ ! -d /proc/$1 ]; then
    echo "Process $1 doesn't exist."
    exit 1
fi

while [ -d /proc/$1 ]; do
    sleep 1
done

echo "Process $1 finished at $(date +"%F %T")."

Jun 19, 2013

Colored SVN diff

svn di --git --no-diff-deleted -x -p | pygmentize -l diff -f html -O full,style=emacs > /tmp/patch.html

Jun 10, 2013

PostgreSQL

http://www.postgresql.org/docs/9.4/static/app-psql.html

May 24, 2013

Levenshtein distance (a string metric for measuring the difference between two sequences)

FuzzyWuzzy: Fuzzy String Matching in Python
# SIMPLE RATIO
fuzz.ratio("this is a test", "this is a test!")
# 96

# PARTIAL RATIO
fuzz.partial_ratio("this is a test", "this is a test!")
# 100

# TOKEN SORT RATIO
fuzz.ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")
# 90

fuzz.token_sort_ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")
# 100

# TOKEN SET RATIO
fuzz.token_sort_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear")
# 84
fuzz.token_set_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear")
# 100

# PROCESS
choices = ["Atlanta Falcons", "New York Jets", "New York Giants", "Dallas Cowboys"]
process.extract("new york jets", choices, limit=2)
# [('New York Jets', 100), ('New York Giants', 78)]
process.extractOne("cowboys", choices)
# ("Dallas Cowboys", 90)

May 23, 2013

Rabbitmq Admin

sudo rabbitmq-plugins enable rabbitmq_management
sudo rabbitmq-plugins list
sudo /etc/init.d/rabbitmq-server restart
guest/guest
http://127.0.0.1:15672

May 6, 2013

SVN stash equivalent

svn diff > patch_name.patch; svn revert -R .
patch -p0 < patch_name.patch

Apr 2, 2013

Mar 29, 2013

Functional Programming in Python

https://github.com/Suor/funcy - A fancy and practical functional tools Article
https://github.com/Suor/patterns - Pattern matching for python
https://github.com/kachayev/fn.py - Functional programming in Python: implementation of missing features to enjoy FP

Mar 18, 2013

Mar 9, 2013

How can one run multiple programs in the background with single command?

The bash manpage section titled Compound Commands has two options that would work, list and group commands.

A group command is a series of commands enclosed in curly braces {}. A list is the same, enclosed in parentheses (). Both can be used to background multiple commands within, and finally to background the entire collection as a set. The list construct executes commands in a subshell, so variable assignments are not preserved.

To execute a group of commands:
{ command1 & command2 & } &

You can also execute your commands in a list (subshell):
( command1 & command2 ) &

Note that the list () syntax executes the enclosed commands in a subshell, so variables are not preserved within.

Mar 4, 2013

Linux From Scratch Book

Linux From Scratch (LFS) is a project that provides you with step-by-step instructions for building your own customized Linux system entirely from source.

http://www.linuxfromscratch.org/lfs/
http://www.linuxfromscratch.org/lfs/downloads/stable/

Ч.Петцольд - Код. Тайный язык информатики
















Feb 25, 2013

ssh to vagrant host

cat >> ~/.ssh/config << "EOF"
Host vagrant
  HostName localhost
  User vagrant
  StrictHostKeyChecking no
  UserKnownHostsFile=/dev/null
  IdentityFile ~/.vagrant.d/insecure_private_key
  Port 2222
EOF

ssh vagrant

Feb 21, 2013

Vim ctags

Navigation
<C-LeftMouse>
<C-RightMouse>
:tags list the current tag stack
Ctrl-W } open the tag in the preview window

How do I list all the tags matching a search pattern?
:tag /<pattern>
:stag /<pattern>

Under your Python distribution
Tools/scripts/ptags.py

http://vim.wikia.com/wiki/Browsing_programs_with_tags


Configure ctags.
Add to ~/.ctags the following, one option per line:
--python-kinds=-i
optional: --exclude=<partial names of bad files/directories>. e.g. --exclude=*/migrations/* to exclude all files inside 'migrations/' directories

Feb 18, 2013

Managing Projects with GNU Make - Robert Mecklenburg - 2009


# Standard phony targets
Target    | Function
----------|--------------------------------------------------------------------
all       | Perform all tasks to build the application
install   | Create an installation of the application from the compiled binaries
clean     | Delete the binary files generated from sources
distclean | Delete all the generated files that were not in the original source distribution
TAGS      | Create a tags table for use by editors
info      | Create GNU info files from their Texinfo sources
check     | Run any tests associated with this application

# Automatic Variables
$@ | The filename representing the target.
$% | The filename element of an archive member specification.
$< | The filename of the first prerequisite.
$? | The names of all prerequisites that are newer than the target, separated by spaces.
$^ | The filenames of all the prerequisites, separated by spaces. This list has duplicate
   | filenames removed since for most uses, such as compiling, copying, etc., duplicates are not wanted.
$+ | Similar to $^, this is the names of all the prerequisites separated by spaces, except
   | that $+ includes duplicates. This variable was created for specific situations such
   | as arguments to linkers where duplicate values have meaning.
$* | The stem of the target filename. A stem is typically a filename without its suffix.
   | (We’ll discuss how stems are computed later in the section “Pattern Rules.”) Its use
   | outside of pattern rules is discouraged.

VPATH = src
vpath pattern directory-list
vpath %.c src

# Special Targets
.INTERMEDIATE
Prerequisites of this special target are treated as intermediate files. If make creates
the file while updating another target, the file will be deleted automatically when
make exits. If the file already exists when make considers updating the file, the file
will not be deleted.
This can be very useful when building custom rule chains. For instance, most
Java tools accept Windows-like file lists. Creating rules to build the file lists and
marking their output files as intermediate allows make to clean up many temporary files.
.SECONDARY
Prerequisites of this special target are treated as intermediate files but are never
automatically deleted. The most common use of .SECONDARY is to mark object
files stored in libraries. Normally these object files will be deleted as soon as they
are added to an archive. Sometimes it is more convenient during development to
keep these object files, but still use the make support for updating archives.
.PRECIOUS
When make is interrupted during execution, it may delete the target file it is
updating if the file was modified since make started. This is so make doesn't leave
a partially constructed (possibly corrupt) file laying around in the build tree.
There are times when you don't want this behavior, particularly if the file is large
and computationally expensive to create. If you mark the file as precious, make
will never delete the file if interrupted.
Use of .PRECIOUS is relatively rare, but when it is needed it is often a life saver.
Note that make will not perform an automatic delete if the commands of a rule
generate an error. It does so only when interrupted by a signal.
.DELETE_ON_ERROR
This is sort of the opposite of .PRECIOUS. Marking a target as .DELETE_ON_ERROR
says that make should delete the target if any of the commands associated with
the rule generates an error. make normally only deletes the target if it is interrupted by a signal.

target...: variable = value
target...: variable := value
target...: variable += value
target...: variable ?= value

--include-dir (or -I)
-include i-may-not-exist.mk

@echo "$(MAKE_VERSION)"
--directory (-C)
@echo "$(CURDIR)"
@echo "$(MAKECMDGOALS)"
@echo "$(MAKEFILE_LIST)"
@echo "$(.VARIABLES)"

# String Functions
$(filter pattern... ,text)
$(filter-out pattern... ,text)
$(findstring string,text)
$(subst search-string,replace-string,text)
$(patsubst search-pattern,replace-pattern,text)
$(words text)
$(word n,text)
$(firstword text)
$(wordlist start,end,text)

# Important Miscellaneous Functions
$(sort list)
$(shell command)

# Filename Functions
$(wildcard pattern...)
$(dir list...)
$(notdir name...)
$(suffix name...)
$(basename name...)
$(addsuffix suffix,name...)
$(addprefix prefix,name...)
$(join prefix-list,suffix-list)

# Flow Control
$(if condition,then-part,else-part)
$(error text)
$(foreach variable,list,body)

# Less Important Miscellaneous Functions
$(strip text)
$(origin variable)
$(warning text)


@ | Do not echo the command. For historical compatibility, you can make your tar-
  | get a prerequisite of the special target .SILENT if you want all of its commands to
  | be hidden. Using @ is preferred, however, because it can be applied to individual
  | commands within a command script. If you want to apply this modifier to all
  | targets (although it is hard to imagine why), you can use the --silent (or -s)
  | option.
  |
- | The dash prefix indicates that errors in the command should be ignored by make.
  | By default, when make executes a command, it examines the exit status of the
  | program or pipeline, and if a nonzero (failure) exit status is returned, make termi-
  | nates execution of the remainder of the command script and exits. This modi-
  | fier directs make to ignore the exit status of the modified line and continue as if
  | no error occurred.
  |
+ | The plus modifier tells make to execute the command even if the --just-print (or
  | -n) command-line option is given to make. It is used when writing recursive
  | makefiles.


# Debugging
--just-print -p
--print-data-base -n
--warn-undefined-variables


Linux Journal Readers' Choice Awards

Best Monitoring Application
Best Linux-Friendly Web Hosting Company
Best Open-Source Configuration Management Tool
Best Linux Book
Best Linux Journal Column

Feb 10, 2013

Feb 8, 2013

Feb 4, 2013

recordmydesktop

sudo apt-get  install gtk-recordmydesktop
recordmydesktop --no-sound -o new.ogg

The Linux Command Line: A Complete Introduction, William E. Shotts Jr. - 2012

& |    && ||    (cd interior && pwd) && pwd    autocd    C+A+F7
> file1    2>&1    &>    cat>file2    tee    cat <<- EOF
apropos    alias
kill -1 apache2   pstree    vmstat 5
dpkg -s solr-common    dpkg -S solr 
lftp    scp    sftp    ftp <<- EOF
cat -A foo.txt   sort -k 3.7nbr -k 3.1nbr -k 3.4nbr distros.txt
cut    paste    join
nl    fold    pr
{ ls -l; cat f.txt; } > o.txt
read < <(echo "foo")    trap    async-child &    mkfifo pipe1

Jan 29, 2013

USB

lsusb
usb-devices

Jan 28, 2013

SSD

cat >> sysctl.conf << "EOF"
vm.swappiness=1
vm.vfs_cache_pressure=50
EOF

sudo cp sysctl.conf /etc/
And in /etc/fstab:
noatime,discard
https://wiki.archlinux.org/index.php/Solid_State_Drives
http://help.ubuntu.ru/wiki/ssd

Jan 17, 2013

C code auto formatting

indent -st na.c > na2.c

Jan 15, 2013

How to determine OS of the remote computer

Scan of a local network
nmap -O -sV 192.168.10.*
#...
#Nmap scan report for 192.168.10.100
#Host is up (0.074s latency).
#Nmap scan report for 192.168.10.102
#Host is up (0.0015s latency).
#Nmap scan report for 192.168.10.104
#Host is up (0.11s latency).
#...
Identify OS on remote host
sudo nmap -O -sV 192.168.10.20
#Starting Nmap 6.00 ( http://nmap.org ) at 2013-01-15 10:29 FET
#Nmap scan report for 192.168.10.20
#Host is up (0.00032s latency).
#Not shown: 980 closed ports
#PORT     STATE SERVICE        VERSION
#80/tcp   open  http           Microsoft HTTPAPI httpd 2.0 (SSDP/UPnP)
#89/tcp   open  http           Microsoft HTTPAPI httpd 2.0 (SSDP/UPnP)
#135/tcp  open  msrpc          Microsoft Windows RPC
#139/tcp  open  netbios-ssn
#443/tcp  open  skype2         Skype
#445/tcp  open  netbios-ssn
#912/tcp  open  vmware-auth    VMware Authentication Daemon 1.0 (Uses VNC, SOAP)
#1025/tcp open  msrpc          Microsoft Windows RPC
#1026/tcp open  msrpc          Microsoft Windows RPC
#1027/tcp open  msrpc          Microsoft Windows RPC
#1028/tcp open  msrpc          Microsoft Windows RPC
#1062/tcp open  msrpc          Microsoft Windows RPC
#1104/tcp open  memcache       memcached
#1192/tcp open  msrpc          Microsoft Windows RPC
#1248/tcp open  msrpc          Microsoft Windows RPC
#1433/tcp open  ms-sql-s       Microsoft SQL Server 2008 R2 10.50.2500; SP1
#2003/tcp open  http           Microsoft HTTPAPI httpd 2.0 (SSDP/UPnP)
#3389/tcp open  ms-wbt-server?
#8009/tcp open  ajp13          Apache Jserv (Protocol v1.3)
#8181/tcp open  http           Apache Tomcat/Coyote JSP engine 1.1
#MAC Address: 1C:6F:65:8C:34:B3 (Giga-byte Technology Co.)
#Device type: general purpose
#Running: Microsoft Windows 7|2008
#OS CPE: cpe:/o:microsoft:windows_7 cpe:/o:microsoft:windows_server_2008::sp1
#OS details: Microsoft Windows 7 or Windows Server 2008 SP1
#Network Distance: 1 hop
#Service Info: OS: Windows; CPE: cpe:/o:microsoft:windows
#
#OS and Service detection performed. Please report any incorrect results at #http://nmap.org/submit/ .
#Nmap done: 1 IP address (1 host up) scanned in 101.33 seconds
The same technique can be also used for all over the WAN remote hosts. Scanning for OS version on a remote host can be quite handy to you as an administrator. On the other hand, this technique can also be abused by hackers. They can target any host with their exploitation attack based on quite accurate information of a running OS and its patch level. Let this be just a quick reminder for all of us to keep all our systems up to date. http://how-to.linuxcareer.com/how-to-determine-os-of-the-remote-computer

Common Lisp links from Alexander Artemenko

http://dev.svetlyak.ru/link-roundup-25/

Jan 11, 2013

The Manual

man printf
# search:
man -k printf
apropos printf
# library functions section 3:
man 3 printf
Vi users can put the cursor on a word and use K to open that word’s manpage

Compiling C

-O3 indicates optimization level three, which tries every trick known to build faster code. If, when you run the debugger, you find that too many variables have been optimized out for you to follow what’s going on, then change this to -O0.
-Wall adds compiler warnings. This works for gcc, Clang, and icc. For icc, you might prefer -w1, which displays the compiler’s warnings, but not its remarks.
-Werror your compiler will treat warnings as errors.
-I adds the given path to the include search path, which the compiler searches for header files you #included in your code.
-L adds to the library search path. Order matters:
gcc -I/usr/local/include use_useful.c -o use_useful -L/usr/local/lib -luseful
If you have a file named specific.o that depends on the Libbroad library, and Libbroad depends on Libgeneral, then you will need: gcc specific.o -lbroad -lgeneral
-g adds symbols for debugging.
-std=gnu11 is gcc-specific, and specifies that gcc should allow code conforming to the C11 and POSIX standards

pkg-config
pkg-config --libs gsl libxml-2.0
# -lgsl -lgslcblas -lm -lxml2

pkg-config --cflags gsl libxml-2.0
# -I/usr/include/libxml2

gcc `pkg-config --cflags --libs gsl libxml-2.0` -o specific specific.c
# gcc -I/usr/include/libxml2 -lgsl -lgslcblas -lm -lxml2 -o specific specific.c
Runtime linking
export LD_LIBRARY_PATH=libpath:$LD_LIBRARY_PATH
# or 
LDADD=-Llibpath -Wl,-Rlibpath

Makefile

Rules:
$@
The full target filename. By target, I mean the file that needs to be built, such as a .o file being compiled from a .c file or a program made by linking .o files.
$*
The target file with the suffix cut off. So if the target is prog.o, $* is prog, and $*.c would become prog.c.
$<
The name of the file that caused this target to get triggered and made. If we are making prog.o, it is probably because prog.c has recently been modified, so $< is prog.c.

The full list of default rules and variables:
make -p > default_rules
# POSIX-standard make has a specific recipe for compiling a .o object file from a .c source code file:
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $*.c
Make template:
P=program_name
OBJECTS=
CFLAGS = -g -Wall -O3
LDLIBS=
CC=c99
$(P): $(OBJECTS)
Passing enviroment variable:
html:
    latex -interaction batchmode $(f)
    latex2html $(f).tex
...and execute:
f=tip make

Jan 10, 2013

Compiling C from stdin

allheads.h:
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <gsl/gsl_rng.h>
Paste this onto your command line, or add it to your .bashrc, .zshrc, or wherever applicable:
go_libs="-lm"
go_flags="-g -Wall -include allheads.h -O3"
alias go_c="c99 -xc '-' $go_libs $go_flags"
And now we can use a here document to paste short C programs onto the command line:
go_c << '---'
int main(){printf("Hello from the command line.\n");}
---
# and then run:
./a.out

Python shell command

Execute code:
python -c "print 'hi.'"
# or
echo "print 'hi.'" | python '-'
# equal to
echo "print 'hi.'" | python -- -
Execute multiline code:
python '-' <<"EOF"
lines=2
print "\nThis script is %i lines long.\n" %(lines,)
EOF
Using json.tool to validate and pretty-print:
curl https://app01.nutshell.com/api/v1/json | python -m json.tool

Jan 8, 2013

Python to Javascript converters

https://github.com/buchuki/pyjaco

http://pyjs.org/
pyjamas/bin/pyjscompile -o output.js source.py
--strict: this preserves maximum Python semantic compatibility (up to the point of including a copy of the source file to provide tracebacks exactly like Python does);
-O: this tries to balance Python compatibility with speed: turns off function argument checking, attribute checking, bound methods, and using classes for numbers.
--stupid-mode: produces the minimal possible code, at the cost of switching Python semantics with JS semantics.

Jan 7, 2013

Head First C - David Griffiths

Exit status of last program
echo $?

&x - location in memory of x variable

You can also call sizeof for a data type, such as sizeof(int).

You can declare a char pointer as const char * to prevent the code from using it to modify a string.

man strstr
info coreutils 'printf'

Q: But what if I want to pass negative numbers as command-line arguments like set_temperature -c -4? Won’t it think that the 4 is an option, not an argument?
A: In order to avoid ambiguity, you can split your main arguments from the options using --. So you would write set_temperature -c -- -4. getopt() will stop reading options when it sees the --, so the rest of the line will be read as simple arguments.

(./bermuda | ./geo2json) < spooky.csv > output.json

If you genuinely want to share variables, you should declare them in your header file and prefix them with the keyword extern: 
extern int passcode;

make takes away a lot of the pain of compiling files. But if you find that even it is not automatic enough, take a look at a tool called autoconf

The make tool can do far, far more than we have space to discuss here. To find out more about make and what it can do for you, visit the GNU Make Manual

Remember: when you're assigning struct variables, you are telling the computer to copy data.

If you use the typedef command, you can normally skip giving the struct a proper name. But in a recursive structure, you need to include a pointer to the same type. C syntax won't let you use the typedef alias, so you need to give the struct a proper name. That's why the struct here is called struct island.

The nm command lists the names that are stored inside the archive.
nm /usr/lib/libruby1.8-static.a

When you bind a socket to a port, the operating system will prevent anything else from rebinding to it for the next 30 seconds or so, and that includes the program that bound the port in the first place. Use reuse option (p. 477) 

MUT-EX = MUTually EXclusive.

The make tool knows quite a lot about C compilation, and it can use implicit rules to build files without you telling it exactly how.  For example, if you have a file called fred.c, you can compile it without a makefile by typing:
> make fred
cc fred.c -o fred

Purchase at Amazon

https://bitbucket.org/st1tch/head-first-c

Author repository: https://github.com/dogriffiths/HeadFirstC

Generate safe bug reports

…
from django.views.decorators.debug import sensitive_post_parameters

@sensitive_post_parameters("password")
def login_view(request):
    …
POST:<QueryDict: {u'csrfmiddlewaretoken': [u'F3d71EHWECfavaeK4H7nUTzLwgY07AHT'],
                  u'password': [u'********************'],
                  u'email': [u'aruseni.magiku@gmail.com']}>
…
from django.views.decorators.debug import sensitive_variables

@sensitive_variables("payment_card_id")
def process_payment(request):
    …
http://habrahabr.ru/post/164935/

Jan 3, 2013

JavaScript profiling, Console

console.dir(obj);

console.time("t1")
....
console.timeEnd("t1")

console.profile("p1")
...
console.profileEnd("p1")

console.trace()

console.count("my_fun_count")