240 likes | 404 Views
Shell Scripting in Linux. By Kartik N k.narayan@in.ibm.com. 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. Shells in Linux.
E N D
Shell Scripting in Linux By Kartik N k.narayan@in.ibm.com
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
Shells in Linux • Many shells available • Examples -sh, ksh, csh, bash • Bash is most popular http://linux-bangalore.org/2002
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
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
Starting Shell Scripting A Simple Shell Script - #!/bin/bash # catfile.sh # Uses the cat command to display # a file FILE=$1 echo "Displaying file $FILE" cat $FILE http://linux-bangalore.org/2002
Starting Shell Scripting Running A Shell Script - • Method1 $ chmod +x catfile.sh $ ./catfile.sh • Method 2 $ bash catfile.sh http://linux-bangalore.org/2002
BASH Features Loops • for for VAR in LIST do <something> done • while while <condition> do <something> done • until http://linux-bangalore.org/2002
BASH Features Decision operators • if if <condition> then <do something> else <do something else> fi • Can use the 'test' command for condition if test $a = $b then echo $a fi http://linux-bangalore.org/2002
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
BASH Features Arithmetic Evaluation • Methods of evaluation • a=`expr a + 5` • ((a=a+5)) • a=$((a+5)) • let a=a+5 • Many operators available +, -, *, /, %, +=, etc http://linux-bangalore.org/2002
BASH Features I/O Redirection • Operators - >, <, >>, >&, | • File Redirection command >file command <file • File Descriptor Redirection Opening - exec [n]<>file Using - command >&[n] Closing - [n]>&- • Output Redirection ls | sort http://linux-bangalore.org/2002
BASH Features Expansions Pathname expansion *, ?, [...] Tilde expansions ~abc or ~/abc Arithmetic expansion $((expr)) Parameter expansions ${...var...} Brace expansions {-,-,-} Command substitution $(command), `command` Process substitution <(command), >(command) http://linux-bangalore.org/2002
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
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
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
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
Regular Expressions • Pattern matching rules • Used in grep, sed, awk , (everywhere??) • Basic regexps • ^ $ . * ? + [^...] http://linux-bangalore.org/2002
Other Shell-scripting tools Sed • Search and Replace • Delete Awk • Search and process patterns • Process input streams http://linux-bangalore.org/2002
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
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
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
References • man bash(1) • Advanced Bash Scripting Guide [http://personal.riverusers.com/~thegrendel/abs-guide-0.5.tar.gz] • man pages - awk, sed, etc. http://linux-bangalore.org/2002 http://linux-bangalore.org/2002