Monday, July 26, 2010

Linux Shell Scripting part 2

Wild cards * and ?

The character * is a wildcard, and matches against one or more character(s) in a file (or directory) name.

Examples:

ls * will show all the files inside the current directory

ls *.s will show only the files which has .s extension

ls a* will show only the files which begin with a

ls ab* will show only the files which begin with ab

ls ab*.c will show only the files which begin with ab and have the .c extension

The character ? is also a wild cars which matches exactly one character in a file(or directory)name.

Examples:

ls ? will show only the files where their file name is one character long

ls bo? Will show only the files where their file name is 3 character long and starts with bo

Command line arguments

Consider the following command

cp ~/abc ~/Desktop

In this command there are two arguments. What if someone try to run this command without any of those arguments? the shell will probably give an error indicating arguments are missing.This point is crucial when someone uses commands which necessarily need arguments in their shell script. In that case they should run their shell script provided with those arguments.

Example :

Suppose our shell script name is comLineArgs.

Refresh memory on how to run it.

./comLineArgs

If someone needs to give the inputs to their shell scripts externally they can give them as command line arguments.

./comLine Args aaa bbb

The command line arguments which are used at a time can be also be accessed inside the sell script simply as variables.

Shell script name can be accessed as $0

Suppose there is n number of arguments. They can be accessed as $1, $2, $3……. $n

If someone needs to find the number of arguments $# can be used.

$* represents all the arguments.

Example Shell script describing how to access command line arguments.

echo "Total number of command line argument are $#"
echo "$0 is script name"
echo "$1 is first argument"
echo "$2 is second argument"
echo "All of them are :- $* or $@"

Input output redirection

Redirecting the output

> symbol is used to redirect the output of a command.

Example:

cat > file1

The above command creates a new file file1 or overwrites the existing file file1. Cat command reads the inputs given by the keyboard and redirects its output which is normally printed on the screen into the file1.

>> symbol is used to redirect the output of a command as well.

Example:

cat >> file1

Same thing as the > symbol but won’t overwrite the file adds more lines to the end of the file.

Redirecting the input

sort command sorts the contents of list1. Note that here inputs for the sort command are provided by the list1 file.

Pipes

Pipe is a temporary storage that output of a one command is stored and passes to another command as its input.

Example:

ls -l | wc -l

Output of ls command is given as input to wc command So that it will print number of files in current directory.

Saturday, July 24, 2010

Linux Shell Scripting part 1

Shell is a command line interpreter that executes commands read from the keyboard or from a file. It is not part of system kernel, but uses the system kernel to execute programs.

To find all available shells in your system type following command:

$ cat /etc/shells

To find your current shell type following command

$ echo $SHELL

Shell Scripting is series of command written in a plain text file and executing this text file without entering the commands one by one. Any text editor can be used for shell scripting.

Writing a simple shell script “first”

#
# My first shell script
#
clear
echo "My first shell script"

Explanation

1. # is used to represent a comment, which can be used to further explain what is happening in the script.

2. clear is clear command which clears the screen

3. echo is used to print on console

Setting execute permission for the script “first”

Syntax:
chmod permission script-name

Example:
$ chmod 755 first

Running the script “first”

$ ./first

Vriables in shell

1. System variables

Created and maintained by linux itself. Defined in capital letters.

Example: $HOME, $SHELL, $USERNAME

Using system variables

echo $HOME

2. User defined variables

Created and maintained by the user. Defined in simple letters.

Syntax: variable name=value

Example: name=abc

using user defined variables

name=abc

echo $name – this will print abc

echo name – this will print name

To access the user defined variables it is essential to write them followed by the $ sign.

Rules for Naming variable name

1. Variable name must begin with a letter or a number or an underscore, followed by one or more letters or numbers. Never use ?, * etc, for naming variable names.

2. When assigning value to variable never put spaces on either side of the equal sign
age=10 correct
age =10 incorrect
age= 10 incorrect
age = 10 incorrect

3. Variables are case-sensitive

Shell Arithmetic

Use to perform arithmetic operations.

Syntax:
expr operand1 operator operand2

Always put a space between the operand and the operator.

Examples:
expr 1 + 3
expr 2 - 1
expr 10 / 2
expr 20 % 3
expr 10 \* 3 multiplication use \* and not * since its wild card.
echo `expr 6 + 3` should be within back quotes(find back quote under ~) or it won’t print 9. If one uses single or double quotes it will print a + b instead of 9.

Header files and Libraries in c

C Header Files

A header file contains declarations, macro definitions that can be shared between several source files. one can include a header file in their program with the preprocessor tag #include.

System hearder files are predefined. One can include them in their program to provide the definitions and declarations need to invoke system calls and libraries.

#include

One can write their own header files which contains decarations shared between source files of their program.

#include "myheader.h"

C Libraries

GNU C standard library contains several header files which are used to implement common operations. For an example, the header file stdio.h declares facilities for performing input and output, and the header file string.h declares string processing utilities.

Library typically contains previously compiled codes. One can create their own library .Compilation process is shown below for creating a library.

Commands for creating the object file

cc -c mylibrary.c

Commands for creating the library

ar -q mylibrary.a mylibrary.o

using the created library with other source files

cc main.c mylibrary.a

note that main.c uses the definitions, declarations contain in mylibarary.

Thursday, July 22, 2010

Oracle Virtualbox tips

Hi all!

Thank you for viewing my first blog post. Before starting off I would like to thank my loving husband Prabath for encouraging me to start a blog. This would never be written without you.

Introduction to oracle virtualbox

Oracle Virtualbox allows users to run another operating system on top of their existing operating system. For me it's always been ubuntu on top of windows. The existing operating system is usually called as the 'host' and the operating system runs on host is called as 'guest'. Download Oracle virtualbox.



Issues

Getting the full screen

One of the common problems oracle virtualbox users face is the small display. Installing guest additions will give the solution.

1. From virtual machine's Device menu select 'Install Guest Additions'.

2. In the guest start a console and type the following command and run it to install guest additions.

sudo /media/cdrom/VBoxLinuxAdditions-x86.run

Creating a shared folder

Another problem is how to pass data between host and guest. Simple solution is to have a shared folder. Before moving on to this solution one should have installed guest additions or it will give an error unknown file system vboxsf .

1. From virtual machine's Device menu shared folder create a folder and give a name you want.

eg: share

2. Create a directory in the guest with a name you want.

mkdir vshare

3. Run the following command to mount

sudo mount -t vboxsf share ~/vshare

Make sure to use different names otherwise it will probably give a protocol error.

As the shared folder's access type is root, users may face troubles when moving or copying files and directories to the shared folder within the guest. Unless user is logged as the root, he/she will have to use sudo commands which allow to execute a command as the superuser. For an example if the user wants to move a file in the guest to the shared folder the command should be,

sudo mv [path of the file] [path of the destination]

Using USB devices inside the guest

1. Select the virtual machine you want and click on 'settings'(this should be done before starting up the virtual machine).

2. Click on 'USB', check 'enable usb controller' and 'enable usb 2.0 controller' and add usb device filters you want to have in guest.

3. Start the virtual machine, usb device's folder will appear on desktop.