Thursday, May 24, 2007

Shell Arithmetics, Date and the read statement

1. Shell Arithmetics is used to perform arithmetic operations.

Syntax:
expr op1 math-operator op2

Given below are some examples that you can try out.
$ expr 10 / 2 
$ expr 2 - 1
$ expr 2 - 10
$ expr 13 + 1
$ expr 20 \* 3
$ expr 20 %3

Note: There should be spaces between as shown in the Syntax listed above, if not it would be displayed as characters
e.g. $ expr 13+1 would be processed and displayed as 13+1 not 14

2. Printing current system date.

Use the following command to print the current system date.

$ echo "Today is `date`".  

Note: if you type it as shown below the system date will not be displayed only the text will be displayed. So include the word date withing single quotes.
$ echo "Today is date"  

3. read statement
Used to get input from keyboard and store into a variable.

Type the following in a shell script and test the output
$ echo "Your first name please:"
$ read fname
$ echo "Hello $fname, Welcome to BeyondM!"



No comments: