unix shell scripting

Download Unix Shell Scripting

Post on 25-May-2015

1.208 views

Category:

Documents

3 download

Embed Size (px)

TRANSCRIPT

  • 1. Unix Shell Scripting

2. InstructorMustafa QasimCertified Ethical Hacker (CEH)Oracle Certified Expert Solaris Security AdministratorOracle Certified Professional Solaris System Administrator Oracle Certified Associate Solaris 10 Operating System Oracle Certified Associate MySQL 5.5/5Cisco Certified Network Associate (CCNA) 3. Catch MeLinkedIn pk.linkedin.com/in/mustafaqasimGoogle+gplus.to/mustuTwittertwitter.com/mustafaqasim 4. IntroductionNameJob DescriptionProfessional ExperienceExpectations from Unix Shell Scripting course 5. Ideal ParticipantsHardworking? 6. Ideal Participants?Hardworking 7. Ideal Participants LaaazzZZzzyyy.... 8. Lets Start... 9. UnixAT&T Employees at Bell Labs in 1969Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy,Michael Lesk and Joe OssannaUnix Family OS University of California, Berkeleys BSD Solaris HP-UX AIX Sequent and as well as Darwin 10. GNUGNU Not UnixRichard Stallman, 1983GNU General Public License (GPL)GNU Compiler Collection (GCC), the GNU C library (glibc), andGNU Core Utilities (coreutils),[1] but also the GNU Debugger(GDB), GNU Binary Utilities (binutils),[18] and the bash shell.GNU Hurd KernelFree Software Foundation (FSF) 11. GNU/LinuxIn April 1991, Linus Torvalds, a 21-year-oldstudent at the University of Helsinki, Finlandstarted working on some simple ideas for anoperating systemKernel vs OS Linus Torvalds 12. To DoMake yourself familiar with Unix, Unix Derivatives, GNU, Linux andsoftware license GPL and Free & Open Source Software (FOSS).http://en.wikipedia.org/wiki/Unixhttp://www.gnu.org/philosophy/philosophy.htmlhttp://en.wikipedia.org/wiki/GNUhttp://www.gnu.org/gnu/why-gnu-linux.htmlhttp://en.wikipedia.org/wiki/Linux 13. To DoMake yourself familiar with Unix, Unix Derivatives, GNU, Linux andsoftware license GPL and Free & Open Source Software (FOSS).http://en.wikipedia.org/wiki/Unixhttp://www.gnu.org/philosophy/philosophy.htmlhttp://en.wikipedia.org/wiki/GNUhttp://www.gnu.org/gnu/why-gnu-linux.htmlhttp://en.wikipedia.org/wiki/Linux 14. So, Where comes the Shell? 15. Shell? 16. Shell? 17. Compiler vs InterpretedComplete Program > Compiler > ExecutableProgram Fragment > Interpreter > Instant Output 18. Compiler vs Interpreted (Contd.)Compiled Languages C/C++ Pascal FORTRANInterpreted Languages Unix Shell Perl Tcl/Tk 19. Unix ShellFirst shell for Version 6 Unix was written by Ken Thompson(Bell Labs) in 1971It was only Interactive Shell 20. Bourne ShellCreated by Stephen Bourne at AT&T Bell Labs for V7 UNIXGoals Interpreted Language ScriptingNew Features Control Flows, Loops, VariablesLacked Functions 21. Bourne Shell (Contd.)Bourne Shell Derivatives Korn Shell (ksh) Almquist shell (ash) Bourne Again Shell (Bash) 22. C Shell (csh)Bill Joy in 1978 for BSDCreate a scripting language similar to CProminent Feature Command HistoryTenex Enhancements (tcsh) Command completion Command line editing 23. Korn Shell (ksh)By David KornBackward compatibility with Bourne Shell (sh)Derivative of Bourne Shell (sh) 24. Bourne Again Shell (Bash)GNU ProjectSuperset of sh and ksh 25. POSIXInstitute of Electrical and Electronics Engineering (IEEE)International Organization for Standardization (ISO)POSIX ComplianceBash, gawk 26. Responsibilities of Shell1. Reading input and parsing the command line2. Evaluating special characters, such as wildcards and the history character3. Setting up pipes, redirection, and background processing4. Handling signals5. Setting up programs for execution 27. To DoPerform Login and Logout on a Unix shellPerform and understand the following commands ls, dir, touch, mkdir, cd, rm, mv, cat, less, more, which, pwd, man,infoUse vi editor to create and edit filesUnderstand Unix File System hierarchy especially thefollowing directories /bin, /sbin, /home, /root, /var 28. Unix StartupOS is booted and very first process is born Init, PID 1OS authenticates the user and provides access to shell /bin/loginData Streams are set Stdout, stderr, stdinA process is forked to run Graphical Desktop 29. Parsing CommandsLexical Analysis The process of breaking the line up into tokens is called lexicalanalysis.TokensCommand? Built-in command => Execute Internally Executable command => Fork a Child ShellForking Child Shel 30. Types of Commands1. Aliases2. Keywords3. Functions4. Built-in commands5. Executable programs 31. Shell ProcessesPIDpsLinux: pstreeSolaris: ptree 32. System Calls}forkexit Kernel exec wait 33. fork System CallParent/ChildInheritance of environment, open files, real and useridentifications, umask, current working directory, andsignals. 34. fork System Call 35. wait System CallWaits for Childs PID & Exit CodeZombie ProcessOrphan Zombie Process 36. exec System Callexec call for executableExecutable becomes the child 37. exit System Callsigchild signal to parentExit code 0 - 255 38. To DoPerform and Understand the following commands top, ps, pgrep, pstree/ptree, killLearn to hunt a specific process and kill itLearn to hunt a specific instance of a binary running multipletimes simultaneous 39. Environment and InheritanceUID, GIDOwnershipPermissionVariablesWorking Directory 40. User & Group IDs$ iduid=502(mustu) gid=502(mustu) 41. File Creation MaskUmask 777 (Directory) 666 (File) 022 (umask value)022 (umask value)----------------755 644 drwxr-xr-x-rw-r--r-- 42. Set Ownership & Permissionchownchmod 43. VariablesLocal VariablesEnvironment Variables $env 44. File Descriptor0 stdin1 stdout2 stderr 45. I/O Redirection 46. I/O Redirection 47. I/O Redirection 48. Pipe 49. Signals 50. To DoUnderstand & Practice the following Bash Environmental variables Check your current environment variables (hint: $env) Check your users UID & GID Check a files permission set for owner, group and others Change a file/folder permission using both numerical and character format Change a file/folder owner and group Redirect stdout and stderr into a file Pipe or Redirect output of programs 51. Lets write a Bash script ;-) 52. Writing Bash ScriptShebang #!/bin/bashComments #Getting input for value of ageMake script executable Chmod +x myscript.shExecute Script ./myscript.sh 53. Simple Bash Script#!/bin/bash# This is a commentecho Khamoshi Ka Boycot 54. Use Variable#!/bin/bashname=Telenorecho $name Khamoshi Ka Boycot 55. Pass Arguments./myscript ali 15 Lahore#!/bin/bashecho Mr. $1 is $2 years old and belongs to $3 56. Get Input#!/bin/bashecho "What is your name?"read unameecho Welcome $uname 57. Arithmetic Operators == Equal to != Not equal to >Greater then >= Greater then, equal to /scripting/g1,$First Line to Last LinesSubstitute Beginning and End of Word[Pp] Match one of the wordProgramming OR programminggGlobally 70. Regular Expression MetacharactersMeta Character^FunctionBeginning of line anchorExample/^lahore/What It MatchedMatches all lines beginning with lahore 71. Regular Expression MetacharactersMeta Character$FunctionEnd of line anchorExample/lahore$/What It MatchedMatches all lines ending with lahore 72. Regular Expression MetacharactersMeta Character.FunctionMatches one characterExample/la...e/What It MatchedMatches all starting with la, followed by three characters, followed by e 73. Regular Expression MetacharactersMeta Character*FunctionMatches zero or more of the preceding charactersExample/ *lahore/What It MatchedMatches lines with zero or more spaces, followed by the pattern lahore 74. Regular Expression MetacharactersMeta Character[]FunctionMatches one in the setExample/[Ll]ahore/What It MatchedMatches lines containing Lahore or lahore 75. Regular Expression MetacharactersMeta Character[x-y]FunctionMatches one character within a range in the setExample/[A-Z]ahore/What It MatchedMatches letters from A through Z followed by ahore 76. Regular Expression MetacharactersMeta Character[^]FunctionMatches one character not in the setExample/[^A-Z]ahore/What It MatchedMatches any character not in the range between A and Z 77. Regular Expression MetacharactersMeta CharacterFunctionUsed to escape a metacharacterExample/lahore./What It MatchedMatches lines containing lahore, followed by a literal period; Normally the period matches one of anycharacter 78. Regular Expression MetacharactersMeta CharacterFunctionEnding-of-word anchorExample/lahore>/What It MatchedMatches lines containing a word that ends with lahore (supported by vi and grep) 80. Regular Expression Metacharacters AdditionalMeta Character(..)FunctionTags match characters to be used laterExample/(love)able 1er/What It MatchedMay use up to nine tags, starting with the first tag at the leftmost part of the pattern. For example, the pattern loveis saved as tag 1, to be referenced later as 1. In this example, the search pattern consists of lovable followed by lover(supported by sed, vi, and grep) 81. Regular Expression Metacharacters AdditionalMeta Character(..)FunctionTags match characters to be used laterExample/(love)able 1er/What It MatchedMay use up to nine tags, starting with the first tag at the leftmost part of the pattern. For example, the pattern loveis saved as tag 1, to be referenced later as 1. In this example, the search pattern consists of lovable followed by lover(supported by sed, vi, and grep) 82. Regular Expression Metacharacters AdditionalMeta Characterx{m} or x{m,} or x{m,n}FunctionRepetition of character x, m times, at least m times, at least m and not more than n timesExampleo{5,10}What It MatchedMatches if line contains between 5 and 10 consecutive occurrences of the letter o (supported by vi and grep) 83. SedStreamlined, non-interactive editor 84. How Sed Worksabc abc abcText FileSed Buffer Sed Output 85. AwkUNIX programming language used for manipulating data andgenerating reports 86. How A

Recommended

View more >