Files
Programming_Tips/bash.md

4.5 KiB
Raw Blame History

BASH

FLV to AVI conversion :

You will need ffmpeg

$ sudo apt-get install ffmpeg
$ ffmpeg -o /your/flv/file.flv -vcodec mpeg1-video \
 -acodec copy -ar 44100 -s 320x240 -y /your/avi/file.avi

OGG to AVI conversion :

You will need mencoder

$ sudo apt-get install mencoder
$ mencoder out.ogv -ovc xvid -oac mp3lame -xvidencopts pass=1 -o Winner.avi

Get the size of a file :

Here are two simple ways:

$ ls -s file
$ stat file-c %s

##Get the processing time of a function/script:

Simply run the same command prepended with time

$ time previous_command

Get the number of arguments in command line:

Use the variable $# to get the number of arguments of previous command.

WARNING: The name of the command stands for one argument!

Get the list of dependencies of a binary:

Simply run the same command prepended with ldd :

$ ldd previous_command

Demonize a process :

The command to search for is start-stop-daemon, with start option.

To avoid any output in stdout, use redirection:

$ command &> /dev/null&

Launch a process at startup:

A link must be placed in /etc/init.d, using ln -s

$ mv /etc/init.d
$ ln -s /path/to/file link

WARNING: A printf in a startup process will lead to a crash as it blocks the remaining processes.

INFO: In embedded Linux, startup processes will have to be named using a Sxx name, where xx is a digit between 1 and 99.

The processes will be run in ascending order, so be careful not to block the system (samba after network, . . . )

Block interruptions (CTRL+C) :

Simply add the line

trap " 2 3

where you want to trap the command.

End catching interruptions by adding where you want to stop

trap 2 3

Use floats in a script, and convert them back to integers:

The operations can be performed with bc :

$ VAR=$(echo "2.2/ 10.65" | bc -l)

To switch back to the int value :

$ (ceil) INT=${VAR/.*}

Search for pattern in all files of a folder:

$ "find ./ -name .svn -prune -o -type f -print | xargs grep -Hi" //pattern//

The .svn part is used to avoid searching in the subversion folder of my projects.

$ grep -R //pattern//@@ is also a solution.

It is even possible to create an alias with it (see Add a one line command in terminal).

##Add a one line command in terminal:

You may use some one-line commands lots of times a day in terminal.

You can define aliases to gain productivity:

$ vim ~/.bash_aliases

Simply add a new line at the end of the file :

alias_name = "my_one_line_command"

Restart your terminal, youre done!

Seconds since epoch:

$ date -d "$FIRST_TIME" +"%s"

Directly get the correct shebang for you script:

$ which //language// > my_script

Example :

$ which bash > my_script.sh

Get the encoding of a file:

$ file -i //unkown_file_type//

Remove all .svn folders from a repo:

$ cd //my_folder///
$ find -name ".svn" -exec rm -rf {} \;

INFO: Can be performed for any folder type, changing the .svn part

Get information about your Linux version:

Description file:

$ cat /etc/lsb-release

Kernel version:

$ uname -a

More information about Kernel (compiler and so on):

$ cat /proc/version

Get information about your processor(s):

$ cat /proc/cpuinfo

Change default browser in Linux:

$ update-alternatives &&config x-www-browser

You will have to choose between all installed browsers in you distro.

Add a user to the sudoers:

$ visudo

Search for the line

@@ root ALL=(ALL) ALL@@

Add this new line just below :

user_name ALL=(ALL) ALL

Get information about you graphical card:

$ lspci | grep VGA

You may try this if first is not working :

$ lspci | grep video

Add tab completion for sudo commands:

Add

complete -cf sudo

in your .bashrc. It can be done simply in running the following command once:

$ echo "complete -cf sudo" > ~/.bashrc

Read information in elf files:

$ readelf -h $1 | grep 'Class\|File\|Machine'

Sniff on serial interface to read output of a device :

$ sudo apt-get install jpnevulator
$ jpnevulator --read --ascii --tty /dev/ttyUSB0

Remove trailing / in path in a script:

#!/bin/bash
FOLDER=$1
echo ${FOLDER%/}