Thursday, 29 December 2011

advanced top command tutorial

Yesterday i finished reading the book "High performance Mysql- Optimization, backups, replication and Load balancing" by jermy D. Zawodny & Derek J. Balling. It was a old book. Edition says as June 2004. That time clustering was not so mature hence it is lacking in that field. Still it was presented in a simple and easy to understand manner. I learnt quite a lot of things from that book. I was a novice mysql user. Now i am confident of taking up bigger challenges in mysql, aleast it put me in path where i can make good judgement of what decision i have to take , where to look in case of error and understand what the experts are saying and apply them here. I am on path to becoming mysql dba expert. Not just because of this book, it is also because i have been going through standford university db classes. And also because i am going to take up mysql dba certification. I am still reading.

I always wanted to know more about what are the options available for top and ps , rather than simple typing top and interpreting the result. In ps i use to do just ps -ef. But found that -ef for ps is a linux specific thing and doesnt work in freebsd. Where i used the older one of ps i used to, ie. ps aux. Today i learnt about options to top. I am putting up the options that interest me and not all i learnt.

Some of Top's interactive command list

d or s => change the display refresh rate.
W => Write current settings to file.
M => Sort by %Mem
N =>  Sort by %PID
P =>  Sort by %CPU (defualt)
T =>  Sort by Time+
O =>  Sort by  any other field selected from list presented upon pressing O.
R =>  Reverse the order sort of the selected field (above).
c => show commands with absolute path  and its arguments.

Some of Top's command line options

-n 5 => iterate for 5 times of display refresh and then exit.
-b -n 5 > top-out.txt  => run in batch mode and iterate for 5 times and output to top-out.txt. Note that just '-n 5 > top-out.txt' will create binary data in the top-out.txt. It is necessary to use -b to output in plain text.

Sunday, 4 December 2011

bash arrays


Courtesy:http://mywiki.wooledge.org/BashGuide/Parameters
Courtesy:http://www.thegeekstuff.com/2010/06/bash-array-tutorial/

Associative array: declare -A variable: The variable is an associative array of strings (bash 4.0 or higher).
Read Only: declare -r variable: The variable can no longer be modified or unset.
Export: declare -x variable: The variable is marked for export which means it will be inherited by any subshell or child process.
${arr[0]} = refers to first element of array.
${#host[*]} = no. of elements in a array.
${arr[*]} = refers to all elements of array.
${#arrayname[n]} should give the length of the nth element in an array.
${arr[@]:3:2} = return the all elements of the array starting from 4th element (3 index) to 6th element (2 elements). First number is the position from which to extract and the next number is the number of elemets from that position to extract.
${arr[2]:0:4} = extracts first 4 characters of 3rd element of the array. For example if the arr[2]=ubuntu, then the above will print ubun
Search and replace a array element:
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Unix=("${Unix[@]/Ubuntu/SCO Unix}")

Add an element to an existing Bash Array:
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Unix=("${Unix[@]}" "AIX" "HP-UX")
echo ${Unix[7]}
This can also be doen via
Unix+=("AIX" "HP-UX")


Remove an Element from an Array
unset Unix[3]
The above will assign null value at position 3. To completely remove 3 positon valu. reassign the araray removing positon 3 vlaue like this.
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
pos=3
Unix=(${Unix[@]:0:$pos} ${Unix[@]:$(($pos + 1))})

Remove Bash Array Elements using Patterns
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora');
patter=( ${Unix[@]/Red*/} )
echo ${patter[@]}

Deleting an Entire Array:
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
unset Unix

Load Content of a File into an Array:
mapfile -t filecontent < "$logfile"
printf "%s\n" "${filecontent[@]}"
you cannot use echo since it will convert new line into single space.

download youtube video in ubuntu and convert them to mp3 with ffmpeg

I have compiled ffmepg following this article in Ubuntu forums http://ubuntuforums.org/showthread.php?t=786095 I should say this is one awesome guide, i have seen. This reply clarifies a bit more on the instructions on the  link above http://ubuntuforums.org/showpost.php?p=11243462&postcount=1839 . I gathered this shell script from ubuntu forum which automates the converstion of mp4 or flv to mp3 format. Let me explain a bit more about this youtube download in ubuntu. First install youtube-dl with
sudo apt-get install youtube-dl
The update it to latest version by
sudo youtube-dl -U
The download your favourite songs from youtube via
youtube-dl -o "%(stitle)s.%(ext)s" http://some-youtube-url/

This -o option tells youtube-dl to save the downloaded video in alphanumberic title (aka simplified title) with the extension it is downloaded as . Such as flv or mp4, etc.

Once it is downloaded you can use the following one liner bash script to convert flv or mp4 to mp3

for f in *.flv; do /home/ashwin/ffmpeg/ffmpeg -i "$f" -acodec libmp3lame -ab 128k "${f%.flv}.mp3"; done
 and
for f in *.mp4; do /home/ashwin/ffmpeg/ffmpeg -i "$f" -acodec libmp3lame -ab 128k "${f%.mp4}.mp3"; done

replace the path for ffmpeg to the right path as you compiled in your PC. I noticed that previously in natty the codec was mp3 and now in oneirc it is libmp3lame. Why the *&^%$$#$ do they change this.