Shell Scripting in Linux - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Shell Scripting in Linux

Description:

Shell Scripting. Automating command execution. Batch processing of ... Usage of Shell-scripts. Where Not to use shell-scripting. Resource-intensive tasks ... – PowerPoint PPT presentation

Number of Views:2258
Avg rating:3.0/5.0
Slides: 24
Provided by: foss
Category:
Tags: linux | scripting | shell

less

Transcript and Presenter's Notes

Title: Shell Scripting in Linux


1
Shell Scripting in Linux
By Kartik N k.narayan_at_in.ibm.com
2
Introduction
  • Shell
  • Interface to the user
  • Command interpreter
  • Programming features
  • Shell Scripting
  • Automating command execution
  • Batch processing of commands
  • Repetitive tasks

http//linux-bangalore.org/2002
3
Shells in Linux
  • Many shells available
  • Examples -sh, ksh, csh, bash
  • Bash is most popular

http//linux-bangalore.org/2002
4
The Bourne Again Shell
  • Abbreviated bash
  • Default in most Linux distributions
  • Most widely used on all UNIX platforms
  • Derives features from ksh, csh, sh, etc.

http//linux-bangalore.org/2002
5
BASH Programming features
  • Support for many programming features
  • - variables, arrays, loops, decision
    operators, functions, positional parameters
  • Pipes, I/O re-direction
  • Misc. features - expansions, job control
  • Built in Commands - read, echo, source, alias

http//linux-bangalore.org/2002
6
Starting Shell Scripting
A Simple Shell Script -
  • !/bin/bash
  • catfile.sh
  • Uses the cat command to display
  • a file
  • FILE1
  • echo "Displaying file FILE"
  • cat FILE

http//linux-bangalore.org/2002
7
Starting Shell Scripting
Running A Shell Script -
  • Method1
  • chmod x catfile.sh
  • ./catfile.sh
  • Method 2
  • bash catfile.sh

http//linux-bangalore.org/2002
8
BASH Features
  • Loops
  • for
  • for VAR in LIST
  • do ltsomethinggt
  • done
  • while
  • while ltconditiongt
  • do ltsomethinggt
  • done
  • until

http//linux-bangalore.org/2002
9
BASH Features
  • Decision operators
  • if
  • if ltconditiongt
  • then
  • ltdo somethinggt
  • else
  • ltdo something elsegt
  • fi
  • Can use the 'test' command for condition
  • if test a b
  • then
  • echo a
  • fi

http//linux-bangalore.org/2002
10
BASH Features
  • Functions
  • Creating
  • - function foo ...
  • - foo () ...
  • Calling
  • - foo 5 6
  • Using parameters in the function
  • - foo () echo "1 2"

http//linux-bangalore.org/2002
11
BASH Features
  • Arithmetic Evaluation
  • Methods of evaluation
  • aexpr a 5
  • ((aa5))
  • a((a5))
  • let aa5
  • Many operators available
  • , -, , /, , , etc

http//linux-bangalore.org/2002
12
BASH Features
  • I/O Redirection
  • Operators - gt, lt, gtgt, gt,
  • File Redirection
  • command gtfile
  • command ltfile
  • File Descriptor Redirection
  • Opening - exec nltgtfile
  • Using - command gtn
  • Closing - ngt-
  • Output Redirection
  • ls sort

http//linux-bangalore.org/2002
13
BASH Features
Expansions Pathname expansion , ?,
... Tilde expansions abc or
/abc Arithmetic expansion ((expr)) Paramet
er expansions ...var... Brace
expansions -,-,- Command
substitution (command), command Process
substitution lt(command), gt(command)
http//linux-bangalore.org/2002
14
BASH Features
  • Environment
  • env variables - HOME, PWD, LOGNAME
  • Aliases - alias cp'cp -i'
  • Use /.bashrc to set env variables and aliases
  • Job Control
  • Foreground and Background jobs ()
  • Ctrl-z, bg, fg, jobs
  • kill job

http//linux-bangalore.org/2002
15
BASH Features
  • Quoting
  • Escape character (\)
  • echo \hello
  • hello
  • Partial quoting ("...")
  • Escape special characters using \
  • Prevents word-splitting
  • Full quoting ('...')
  • Cannot use escape sequences
  • Cannot embed single quotes

http//linux-bangalore.org/2002
16
BASH Programmable Completion
  • Available in BASH 2.05 and above
  • Allows user-defined completions
  • Uses the BASH built-ins 'complete' and 'compgen'
  • Uses in-built arrays COMPREPLY and COMP_WORDS

http//linux-bangalore.org/2002
17
Other Shell-scripting tools
  • Basic commands
  • ls, cat ,cp, mv, find, xargs, expr
  • date, time, etc
  • Filters
  • grep, sort, uniq, diff, cut, paste, etc.
  • Regular Expressions
  • Other programs
  • sed, awk, lex, yacc
  • tty, stty, tset

http//linux-bangalore.org/2002
18
Regular Expressions
  • Pattern matching rules
  • Used in grep, sed, awk , (everywhere??)
  • Basic regexps
  • . ? ...

http//linux-bangalore.org/2002
19
Other Shell-scripting tools
  • Sed
  • Search and Replace
  • Delete
  • Awk
  • Search and process patterns
  • Process input streams

http//linux-bangalore.org/2002
20
Usage of Shell-scripts
  • Where to use shell-scripting
  • System Administration
  • -Automate tasks
  • -Repeated tasks
  • Development
  • -Allows testing a limited sub-set of
    functionality
  • -Testing tools
  • Daily usage
  • Simple scripts
  • - reminders, diary, mail, etc.

http//linux-bangalore.org/2002
21
Usage of Shell-scripts
  • Where Not to use shell-scripting
  • Resource-intensive tasks
  • Cross-platform portability (Use C, JAVA,etc.)
  • Complex and mission-critical applications
  • Where security is important
  • Proprietary, closed-source applications

http//linux-bangalore.org/2002
22
Optimising scripts
  • Speed improvement
  • Use/write programs for slow operations
  • e.g use grep to speeden searches and awk for
    mathematical ops
  • Optimise loops
  • Minimise I/O BASH is slow with files
  • Use awk, Perl, etc. where speed is reqd.
  • Keeping scripts small
  • Modularised scripting

http//linux-bangalore.org/2002
23
References
  • man bash(1)
  • Advanced Bash Scripting Guide
  • http//personal.riverusers.com/thegrendel/abs-gu
    ide-0.5.tar.gz
  • man pages - awk, sed, etc.

http//linux-bangalore.org/2002
http//linux-bangalore.org/2002
Write a Comment
User Comments (0)
About PowerShow.com