[go: up one dir, main page]

0% found this document useful (0 votes)
11 views2 pages

Shell Scripting Simple Programs

The document provides examples of basic shell scripting, including commands to print the date and user information, perform arithmetic operations, and calculate the area of a circle. It explains the use of different quote characters in shell scripting and demonstrates how to use the 'expr' command and 'bc' for arithmetic calculations. Additionally, it highlights the importance of setting scale for decimal precision in calculations.

Uploaded by

Sreejith k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

Shell Scripting Simple Programs

The document provides examples of basic shell scripting, including commands to print the date and user information, perform arithmetic operations, and calculate the area of a circle. It explains the use of different quote characters in shell scripting and demonstrates how to use the 'expr' command and 'bc' for arithmetic calculations. Additionally, it highlights the importance of setting scale for decimal precision in calculations.

Uploaded by

Sreejith k
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Shell Scripting: Basic Programs

#Example 1
#! /bin/bash
echo date
#prints date
echo who
#prints who is logged into the system
#! /bin/bash
echo "User information for userid : $USER"
echo UID : $UID
echo HOME : $HOME
#arithmetic expression example
read x
read y
var3=`expr $x + $y`
# The expr command is used to evaluate a given expression and display its standard output.
echo The result is $var3
#! /bin/bash
var1=$[1+5]
echo $var1
v2=2
var3=`expr $var1 + $v2`

echo $var3
var3=`expr $var1 - $v2`
echo $var3

var3=`expr $var1 \* $v2`

echo $var3

var3=`expr $var1 / $v2`

echo $var3
Notes:

There are three different quote characters with different behaviour. These are:
“ double quote, weak quote. If a string is enclosed in “ ” the references to variables (i.e $variable )
are replaced by their values. Also back-quote and escape \ characters are treated specially.
‘ single quote, strong quote. Everything inside single quotes are taken literally, nothing is treated as
special.
` back quote. A string enclosed as such is treated as a command and the shell attempts to execute it. If
the execution is successful the primary output from the command replaces the string.

Example: echo “Today is:” `date`

#! /bin/bash

var1=100
var2=45
var3=$(echo "scale =4 ; $var1/$var2" | bc)
echo $var3

Notes:
This sets the scale to 4, meaning the result of 100/45 will be calculated with 4 decimal
places.
#! /bin/bash
#Area of a circle

echo Enter radius of circle

read radi

pivalue=3.14159

var1= $(echo "scale=4;$radi * $radi" | bc)

area= $(echo "scale=4;$var1 * $pivalue" | bc)

echo $area

You might also like