Title: Shell Scripting in Linux
1Shell Scripting in Linux
By Kartik N k.narayan_at_in.ibm.com
2Introduction
- 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
3Shells in Linux
- Many shells available
- Examples -sh, ksh, csh, bash
- Bash is most popular
http//linux-bangalore.org/2002
4The 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
5BASH 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
6Starting 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
7Starting Shell Scripting
Running A Shell Script -
- Method1
- chmod x catfile.sh
- ./catfile.sh
- Method 2
- bash catfile.sh
http//linux-bangalore.org/2002
8BASH Features
- Loops
- for
- for VAR in LIST
- do ltsomethinggt
- done
- while
- while ltconditiongt
- do ltsomethinggt
- done
- until
http//linux-bangalore.org/2002
9BASH 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
10BASH Features
- Functions
- Creating
- - function foo ...
- - foo () ...
- Calling
- - foo 5 6
- Using parameters in the function
- - foo () echo "1 2"
http//linux-bangalore.org/2002
11BASH Features
- Arithmetic Evaluation
- Methods of evaluation
- aexpr a 5
- ((aa5))
- a((a5))
- let aa5
- Many operators available
- , -, , /, , , etc
http//linux-bangalore.org/2002
12BASH 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
13BASH 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
14BASH 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
15BASH 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
16BASH 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
17Other 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
18Regular Expressions
- Pattern matching rules
- Used in grep, sed, awk , (everywhere??)
- Basic regexps
- . ? ...
-
http//linux-bangalore.org/2002
19Other Shell-scripting tools
- Sed
- Search and Replace
- Delete
-
- Awk
- Search and process patterns
- Process input streams
http//linux-bangalore.org/2002
20Usage 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
21Usage 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
22Optimising 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
23References
- 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