Intro To Shell Scripts
Intro To Shell Scripts
Output:
Hello world
$touch try.sh
or
$nano try.sh
//nano editor opened//
//Write the script in this editor//
echo “hello Linux”
echo “please enter name:”
read name
echo “entered Name is: $name”
echo “thats it”
date
Save = CTRL+X, Y
Execute on terminal:
$./try.sh
Output:
Hello Linux
Please Enter name:
Enterd name is: jims
Thats it
Thurs Apr 16 03:55:17
Now run the write the following in your script with echo
echo $ BASH VERSION
echo $ HOME
echo$ PWD
read name
Go to terminal-
Enter name:
Aparna
$ ./file.sh
Enter Names:
What is comment?
We can write # (hash) when we want to write a comment in a
script. Line starting with # would not execute.
Example for showing the system variables and user defined variables:
Use of if...
Keywords:
if, else, then, elif, fi
Vi editor
If [ $s != $b ]; also can be used
Output:
root
you are logged in with root
Output:
root
you are not root
//comparing age using if-else//
Loops
Keywords: for, while, do, done, in
Syntax of for loop
Example.1
Example.2
Example.3
Note- ..Increment option can be used in above version 4.0
To Bash version – echo ${BASH_VERSION}
#While (( $n <= 10 )) - for these parenthesis you can use (<=) rather (–le)
#(( n++ )) - For increament it also can be used
Output:
1
2
3
4
5
6
Output:
1
Output:
1
2
No=10
V=$no
While [ $a –lt $no ]
Do
Temp= ‘expr $temp \* $no’
No= ‘expr $no ‘ 1
Done
Echo “Factorial of $v is $temp”
Run:
$ sh whileloop.sh
Output:
0
1
2
3
4
5
6
7
8
9
10
Output
Output:
//write a shell script to find sum of Digits//
Output:
Output:
//write a shell script to find numbers are equals and to find the largest
number//
Output:
11
11
11
$ ./demo.sh
12
13
14