1 / 23

Shell Scripting in Linux

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.

ranee
Download Presentation

Shell Scripting in Linux

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Shell Scripting in Linux By Kartik N k.narayan@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 FILE=$1 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 <something> done • while while <condition> do <something> done • until http://linux-bangalore.org/2002

  9. 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

  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 • a=`expr a + 5` • ((a=a+5)) • a=$((a+5)) • let a=a+5 • Many operators available +, -, *, /, %, +=, etc http://linux-bangalore.org/2002

  12. 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

  13. 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

  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-guide-0.5.tar.gz] • man pages - awk, sed, etc. http://linux-bangalore.org/2002 http://linux-bangalore.org/2002

More Related