121-Linux Shell Scripting
121-Linux Shell Scripting
Session 01
You can use Linux as Server OS or as stand alone OS on your PC. (But
it is best suited for Server.) As a server OS it provides different
services/network resources to client. Server OS must be:
Stable
Robust
Secure
High Performance
Introduction to
Linux OS
Linux offers all of the above characteristics plus its Open Source and
Free OS. So Linux can be used as:
• Personal Work
• Web Server
• Software Development Workstation
• Workgroup Server
• In Data Center for various server activities such as FTP, Telnet,
SSH, Web, Mail, Proxy, Proxy Cache Appliance etc
Kernel
What Kernel Is?
Unix-style :
$ ps -ef
BSD-style
$ ps -aux
Shell
For example :
$ date
$ pwd
$ ls
$ uname –a
$ who
$w
Shell Script
Shell script can take input from user, file and output them on
screen.
Useful to create our own commands.
Save lots of time.
To automate some task of day today life.
System Administration part can be also automated.
Getting Started
The vi editor has three modes, command mode, insert mode and
command line mode.
Command mode: letters or sequence of letters interactively
command vi. Commands are case sensitive. The ESC key can end a
command.
Insert mode: Text is inserted. The ESC key ends insert mode and
returns you to command mode. One can enter insert mode with the
"i" (insert), "a" (insert after), "A" (insert at end of line), "o" (open
new line after current line) or "O" (Open line above current line)
commands.
Command line mode: One enters this mode by typing ":" which
puts the command line entry at the foot of the screen.
vi text editor
Example:
$ vi first
#My First Script
echo “Hello World”
If you want to run two commands together, you can enter them
on the same prompt line, separated with a semicolon:
$ date ; who
Thu Nov 1 11:58:14 IRST 2012
root pts/0 2012-11-01 11:44 (5.39.11.10)
v.shalchian pts/1 2012-11-01 11:51 (151.241.125.69)
Shell Scripting Basics
When creating a shell script file, you must specify the shell you
are using in the first line of the file. The format for this is:
#!/bin/bash
Using Variables
A shell script allows you to set and use your own variables
within the script. Setting variables allows you to temporarily store
data and use it throughout the script, making the shell script more
like a real computer program.
Assigning value
Here are a few examples of assigning values to user variables:
var1=10
var2=-57
var3=testing
var4="still more testing“
The shell script automatically determines the data type used for
the variable value.
Shell Scripting Basics
echo $var1
var2=$var1
Shell Scripting Basics
The backtick
TODAY=`date`
echo $TODAY
or
echo `date`
Shell Script
Exercise :
Write a shell script to
- Print a welcome message to the currently logged-in user
- Print the current date with proper message
- Print the number of currently logged-in users with proper
message
#!/bin/bash
echo “Welcome `whoami`”
echo “Today is `date`”
echo “Currently there are `who | wc -l` users logged-in to system”