Language:EN
Pages: 46
Rating : ⭐⭐⭐⭐⭐
Price: $10.99
Page 1 Preview
etc sum math myf cal findbs bigshell script nameno

Etc sum math myf cal findbs bigshell script nameno

Linux Shell Scripting Tutorial

G Introduction

H Kernel

H Why Process required

H Linux commands related with process

G Shell Programming

H Variables in Linux

H How to Run Shell Scripts

H Quotes in Shell Scripts

H Why Command Line arguments required

H Exit Status

H test command or [ expr ]

H Loop in shell scripts

H Local and Global Shell variable (export command)

H Conditional execution i.e. && and ||

H getopts command

H More examples of Shell Script (Exercise for You :-)

G Kernel

G Shell

G Process management

G Device management

http://www.freeos.com/guides/lsst/kershell.htm (1 of 5) [17/08/2001 17.42.02]

Shell Name

Developed by

Free Software
Foundation

Most common shell in Linux. It's Freeware
shell.

KSH (Korn SHell)

David Korn

Linux Common Commands
NOTE that following commands are for New users or for Beginners only. The purpose is if you use this command you will be more familiar with your shell and secondly, you need some of these command in your Shell script. If you want to get more information or help for this command try following commands For e.g. To see help or options related with date command try
$ date --help
or To see help or options related with ls command (Here you will screen by screen help, since help of ls command is quite big that can't fit on single screen )

http://www.freeos.com/guides/lsst/kershell.htm (2 of 5) [17/08/2001 17.42.02]

C:\> help time
C:\> help date
C:\> help

Linux Command

Linux Shell Script Tutorial

Remove all files in given directory/subdirectory. Use it very carefully.

rm -rf {dirname}

$ rm -rf oldfiles

$ mail ashish

To count lines, words and characters of given file

wc {file-name}

$wc myfile
To searches file for line that match a pattern.
To sort file in following order
-r Reverse normal order-n Sort in numeric order-nr Sort in reverse
numeric order

http://www.freeos.com/guides/lsst/kershell.htm (4 of 5) [17/08/2001 17.42.02]

Linux Shell Script Tutorial

What is Processes
Process is any kind of program or task carried out by your PC. For e.g. $ ls -lR , is command or a request to list files in a directory and all subdirectory in your current directory. It is a process. A process is program (command given by user) to perform some Job. In Linux when you start process, it gives a number (called PID or process-id), PID starts from 0 to 65535.

Example

To see currently running process

$ kill 1012

To get information about all running process

$ kill 0

For background processing (With &, use to put particular command and program in
background)

http://www.freeos.com/guides/lsst/process.htm [17/08/2001 17.42.03]

Linux Shell Script Tutorial

(3) < Redirector Symbol
Syntax: Linux-command < filename
To take input to Linux-command from file instead of key-board. For e.g. To take input for cat command give
$ cat < myfiles

Pips
A pipe is a way to connect the output of one program to the input of another program without any temporary file.

A pipe is nothing but a temporary storage place where the output of one command is stored and then passed as the input for second command. Pipes are used to run more than two commands ( Multiple commands) from same command line.

Syntax: command1 | command2

$ who | grep raju

Here output of who command is given as input to grep command So that it will print if particular user name if he is logon or nothing is printed ( To see for particular user logon)

Linux Shell Script Tutorial

Introduction to Shell Programming
Shell program is series of Linux commands. Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file. Shell script can take input from user, file and output them on screen. Useful to create our own commands that can save our lots of time and to automate some task of day today life.

BASH=/bin/bash Our shell name

BASH_VERSION=1.14.7(1) Our shell version name

OSTYPE=Linux Our o/s type : -)

PATH=/usr/bin:/sbin:/bin:/usr/sbin Our path settings

NOTE that Some of the above settings can be different in your PC. You can print any of the above variables contain as follows
$ echo $USERNAME
$ echo $HOME
Caution: Do not modify System variable this can some time create problems.

How to define User defined variables (UDV)
To define UDV use following syntax
Syntax: variablename=value
NOTE: Here 'value' is assigned to given 'variablename' and Value must be on right side = sign For e.g.

$ vech=Bus
To define variable called n having value 10
$ n=10

Rules for Naming variable name (Both UDV and System Variable) (1) Variable name must begin with Alphanumeric character or underscore character (_), followed by one or more Alphanumeric character. For e.g. Valid shell variable are as follows
HOME
SYSTEM_VERSION
vech
no

$ vech=
$ vech=""
Try to print it's value $ echo $vech , Here nothing will be shown because variable has no value i.e. NULL variable.

(5) Do not use ?,* etc, to name your variable names.

Q.2.How to Define variable xn with value Rani and print it on screen $ xn=Rani
$ echo $xn

Q.3.How to print sum of two numbers, let's say 6 and 3
$ echo 6 + 3
This will print 6 + 3, not the sum 9, To do sum or math operations in shell use expr, syntax is as follows Syntax: expr op1 operator op2
Where, op1 and op2 are any Integer Number (Number without decimal point) and operator can be + Addition
- Subtraction
/ Division
% Modular, to find remainder For e.g. 20 / 3 = 6 , to find remainder 20 % 3 = 2, (Remember its integer calculation)
\* Multiplication
$ expr 6 + 3
Now It will print sum as 9 , But
$ expr 6+3
will not work because space is required between number and operator (See Shell Arithmetic)

Press Ctrl + D to save. Now our script is ready. To execute it type command
$ ./first
This will give error since we have not set Execute permission for our script first; to do this type command
$ chmod +x first
$ ./first
First screen will be clear, then Knowledge is Power is printed on screen. To print message of variables contains we user echo command, general form of echo command is as follows

echo "Message"

$ ./first
Here '.'(dot) is command, and used in conjunction with shell script. The dot(.) indicates to current shell that the command following the dot(.) has to be executed in the same shell i.e. without the loading of another shell in memory. Or you can also try following syntax to run Shell Script Syntax: bash &nbsh;&nbsh; your-shell-program-name
OR /bin/sh &nbsh;&nbsh; your-shell-program-name
For e.g.

$ bash first
$ /bin/sh first
Note that to run script, you need to have in same directory where you created your script, if you are in different directory your script will not run (because of path settings), For eg. Your home directory is ( use $ pwd to see current working directory) /home/vivek. Then you created one script called 'first', after creation of this script you moved to some other directory lets say
/home/vivek/Letters/Personal, Now if you try to execute your script it will not run, since script 'first' is in /home/vivek directory, to Overcome this problem there are two ways First, specify complete path of your script when ever you want to run it from other directories like giving following command
$ /bin/sh /home/vivek/first

Linux Shell Script Tutorial

this purpose create bin directory in your home directory and then copy your tested version of shell script to this bin directory. After this you can run you script as executable file without using $ ./shell script-name syntax, Following are steps
$ cd
$ mkdir bin
$ cp first ~/bin
$ first
Each of above command Explanation

Options
-n Do not output the trailing new line.

-e Enable interpretation of the following backslash escaped characters in the strings: \a alert (bell)
\b backspace
\c suppress trailing new line
\n new line
\r carriage return
\t horizontal tab
\\ backslash
For eg. $ echo -e "An apple a day keeps away \a\t\tdoctor\n"

$ echo "Today is date"
Can't print message with today's date.

$ echo "Today is `date`".

Linux Shell Script Tutorial

Use to perform arithmetic operations For e.g.

$ tail +10 myf
Here the name of command is tail, and the arguments are +10 and myf.

Now try to determine command and arguments from following commands: $ ls foo
$ cp y y.bak
$ mv y.bak y.okay
$ tail -10 myf
$ mail raj
$ sort -r -n myf
$ date
$ clear

Linux Shell Script Tutorial

First command line argument passed to myshell i.e. foo

Second command line argument passed to myshell i.e. bar

Here $# will be 2 (Since foo and bar only two Arguments), Please note At a time such 9 arguments can be used from $0..$9, You can also refer all of them by using $* (which expand to
`$0,$1,$2...$9`) Now try to write following for commands, Shell Script Name ($0), No. of Arguments (i.e. $#), And actual argument (i.e. $1,$2 etc)
$ sum 11 20
$ math 4 - 7
$ d
$ bp -5 myf +20
$ ls *
$ cal
$ findBS 4 8 24 BIG

Shell Script Name

$#

$0 $1 $2 $3

sum

11
4 -

7

d

For e.g. now will write script to print command ling argument and we will see how to access them $ cat > demo
#!/bin/sh
#
# Script that demos, command line args
#
echo "Total number of command line argument are $#"
echo "$0 is script name"
echo "$1 is first argument"
echo $2 is second argument"
echo "All of them are :- $*"

Save the above script by pressing ctrl+d, now make it executable
$ chmod +x demo
$ ./demo Hello World
$ cp demo ~/bin
$ demo
Note: After this, For any script you have to used above command, in sequence, I am not going to show you all of the above.

$ echo Welcome
$ echo $?

$ wildwest canwork?
$ echo $?

Linux Shell Script Tutorial

$ echo $?

Zero Value (0)

Yes/True

0

NON-ZERO Value (> 0)

Now will see, if condition which is used for decision making in shell script, If given condition is true then command1 is executed.

of condition is 0 (zero)

...

Following are all examples of expression:
5 > 2
3 + 6
3 * 65
a < b
c > 5
c > 5 + 30 -1
Type following command (assumes you have file called foo) $ cat foo
$ echo $?

The cat command return zero(0) on successful, this can be used in if condition as follows, Write shell script as
$ cat > showfile
#!/bin/sh
#
#Script to print file
#
if cat $1
then
echo -e "\n\nFile $1, found and successfully echoed"
fi

http://www.freeos.com/guides/lsst/shellprog.htm (10 of 19) [17/08/2001 17.42.21]

You are viewing 1/3rd of the document.Purchase the document to get full access instantly

Immediately available after payment
Both online and downloadable
No strings attached
How It Works
Login account
Login Your Account
Place in cart
Add to Cart
send in the money
Make payment
Document download
Download File
img

Uploaded by : Lauren Diaz

PageId: DOC74DA28D