Thursday, August 7, 2008

expr

expr is another command that is used to evaluate arithmetic expressions similar as ‘let’ command.
But expr can’t define variables.
expr command is working as following example.

num1=1
num2=2
expr $num1 + $num2


Important :
In expr command multiplication works different way. That means this is not working.
2 * 3

Have to type '\*' to get the multiplication
2 \* 3

echo -e

echo –e command allows interpreting backslash escapes

eg:- echo “Date and time is : \c”
Date

‘\c’ is used to print the output in the same line

eg:- echo “Have a nice day \n”
‘\n’ is used to make a new line after that.

Wednesday, August 6, 2008

Get the arguments from UNIX shell

This is a simple script program to calculate average of three numbers input as command line arguments.

Important --> UNIX is case sensitive

In scripting
# --> use for insert comments

This is the script avg.sh
#following command indicate the place to interpret the script
#!bin/bash

#$1 $2 and $3 get the command line arguments
echo “First three arguments are $1 $2 $3”
echo “This program is $0”

# $* can use to display all the command line arguments
echo “All arguments : $*”

# $# is used to get the number of arguments
echo “Number of arguments: $#”

# let command is used to execute arithmetic expression
# when declaring a variable that should not put $ sign at the beginning
let sum=$1+$2+$3
#whenever that variable is referring $ sign must put in front of the variable name
echo “sum is $sum”
let avg=$sum/$#
echo “Average is : $avg”

Start to do scripting

To start the UNIX scripting users have to familiar with one of UNIX editors. As a example vi, emacs or PICO.
Open the ‘vi’ editor just type vi and then press ‘i’ to go to the insert mode.
Then type following simple script.
Echo “Hello ”
whoami
Echo “Date and time is :”
Date


echo command dispays the output to the screen.

After that press esc button and type :w followed by the file name
:w example1.sh
Press shift and double z to exit from the vi editor.

After that permission must be given to this file to execute.
This can do using
chmod +x example1.sh
or chmod 755 example1.sh
After executing this command
owner has permission to write, read, and execute,
owner's group have permission to read and execute,
rest of the other people have permission to read and execute.

./example1.sh is used to run the script before set the class path to the current working directory.

To set the path into current working directory
PATH=. : $PATH
After that exmple1.sh can run by typing just example1.sh

man for help

man is the most important command in Unix. It gives complete description about every command.

UNIX

UNIX is developed by programmers for the programmers.