Tuesday, January 29, 2013

Sample Shell Scripts

Sample Shell Scripts


Given an integer argument i, output the successive integers 1 to
2i to stdout.


#!/bin/bash
echo -n "Enter input i : "
read i
i=`expr 2 \* $i`
for j in `seq 1 $i`
do
  echo "$j"
done
The first line indicates the path for the bash shell.Bash is a unix shell
which stands for Bourne-again shell.It is a command processor which pro-
cesses scripts. The second and third line takes the input from the user,echo
is a command which displays the message to stdout by default and read command reads the input from the user. Now to output the successive integers 1 to 2i to stdout,a for loop with seq command is used.seq command prints sequence of numbers.
--------------------------------------------------------------------------------------------------------------------------------------
List all executable files in the current directory.

#!/bin/bash
echo ‘ls -l | grep -- -..x‘

The first line includes the path for the bash shell.To list all the executables
in the current directory,grep command is used. Initially ls -l lists all the files
in the current directory.The output of ls is given as input to grep command
using pipe. Grep command filters out the files with executables.
--------------------------------------------------------------------------------------------------------------------------------------
To modify the file name with the current date.

#!/bin/bash
d=‘date +%Y%m%d‘
str1=‘echo $1 | awk -F"." ’{print $1}’‘
str2=‘echo $1 | awk -F"." ’{print $2}’‘
name=$str1-$d.$str2
mv $1 $name

The first line includes the path for the bash shell.The second line gets
the current date in the format specified.And awk command is used to split
the filename and its extension.And then it is concatenated in the specified
format and replaced with the new file name.
--------------------------------------------------------------------------------------------------------------------------------------
To replace a word with another word in the file.

#!/bin/bash
touch $1;
string="horse is a animal,horse runs very fast"
echo $string > $1
sed -i ’s/horse/elephant/g’ $1
touch -t 201301011200.00 $1
  
The first line includes the path for the bash shell.The touch command
is used to create a new file.The file name is passed as command line argu-
ment.Then some data is added to the file.Sed command is used to replace the
horse word with the elephant word.The touch -t command is used to modify
the last modification time.


Monday, January 28, 2013

How to use gdb debugger ? (Debugging with GDB)

Steps to use gdb :
1) Compile your source code (xyz.c file) using -g option as follows
$ gcc -g xyz.c

2) Execute your ./a.out file as follows
$ gdb ./a.out

3) you will get gdb command line environment and then to start debugging you need to run the program.So, before running ,you need to set the breakpoint to start debug.
gdb$ b xyz.c.15  
Here 15 indicates the line number in your source code file.

4) After setting the breakpoint ,run the program as follows
gdb$ r
If the program needs command line arguments,then enter
gdb$ r arg1 arg2......so on

5) Now ,it starts execution and stops at the given breakpoint.

6) Next,you can run the program line by line as follows
gdb$ n 
or
gdb$ s
Here ,option s also works similar to n(next line),but if you want to trace the program completely(including functions),then use "s" option.

7) Whenever you get segmentation fault,you can actually trace back from where this fault came by "bt"(backtrack) option.
gdb$ bt

8) And also ,you can print the value of any variable at any stage using "p" (print ) option as follows.
For example,if you have a variable like 'i'
gdb$ p i
or
gdb$ print i

There are lot more options which you can try similarly,for more information about options,you can refer to man page of gdb
$ man gdb

thank you for reading !!!
cheers !
sGk

Sunday, January 27, 2013

linux basic commands

Explore Linux commands

1) To get the full hardware configuration details and information about each component,just enter
$ sudo lshw
2) To get the usage of your RAM memory

$ free -m
3) To know the operating mode

$ uname -a
4) To know architecture of cpu

$ lscpu
5) To upack the tar.gz file

$ tar -xvf filename.tar.gz
6)To install the .deb (debian) file

$dpkg -i filename.deb

Tuesday, May 3, 2011

5 Interesting Facts About Android

Fact 1: Android runs Linux. But it’s not “the Linux.”

Android is not LinuxFor internal usage, Google already maintains its own flavor of Linux to power its systems. A similar kind of fork happened with Android where Google believed that certain features needed to be added to the Linux kernel to make it fit for Android. One such feature is “wake locks“, a mechanism introduced by Google in Linux code to handle power management—Wake locks allow Android applications to request kernel not to go into low-power state. Quite interestingly such changes were “rejected” in the staging area of Linux kernel—effectively requiring Google to remain aloof with its version of “Android Linux”—if we can call it that. More information on Android vs. Linux is available in the linked article (dated 9th Feb)

Fact 2: Android uses Java as a development platform. But it’s not Java ME.

Android is not Java MEUnlike Linux, it’s not a fork of Java ME either (if there could be such a thing). Android provides its own SDK which is based on Java. Except for AWT or Swing, quite a lot of Java SE is supported in this SDK. Limited JSRs (Java Specification Request), however, are implemented in addition to the core Java framework. Android’s support for Open GL, for example, is built similar to JSR 239.

Fact 3: Android runs a VM named Dalvik. But it’s not a Java Virtual Machine (JVM).

Dalvik is not a JVMGoogle built a VM from scratch for Android phone; it is optimized for Android. Instead of running Java byte code, Dalvik runs .dex files. Further, unlike JVMs which are stack-based, Dalvik is a registers-based VM. Trivia: Dalvik is named after a town in Iceland.
After Oracle acquired Sun Microsystems, Oracle sued Google over this VM. It’s widely believed that Oracle has shot itself in the foot by filing this lawsuit.

Fact 4: Android was not developed by Google!

Andy Rubin's HomepageAndroid Inc., the company behind Android, was a 22 month old startup when it was acquired by Google in mid 2005. Android was open sourced in October 2008 under Apache’s open source license. Google retained Andy Rubin, the co-founder of Android Inc, as VP Engineering.

Fact5: Android’s runtime includes a SQLite database!

But Android has SQLiteSQLite is a light weight relational database which is built into Android for data storage purpose. Android applications can also make us of this RDBMS.