Unix And Shell Scripting

6,715 views
6,486 views

Published on

Understanding Unix And Shell Scripting.

Published in: Technology
4 Comments
29 Likes
Statistics
Notes
  • Hi Malik,



    Presetation was very good, Shall I get a copy of above presentation if possible?

    my mail id is 'srinig20@gmail.com'



    Regards,

    Srini
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Good Presentation! it's useful one for beginners of unix & shell
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Unix And Shell Scripting
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • best one
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total views
6,715
On SlideShare
0
From Embeds
0
Number of Embeds
250
Actions
Shares
0
Downloads
0
Comments
4
Likes
29
Embeds 0
No embeds

No notes for slide

Unix And Shell Scripting

  1. 1. Unix & Shell Scripting Presenter: Jaibeer Malik August 6, 2004
  2. 2. Agenda <ul><li>Objectives </li></ul><ul><li>UNIX </li></ul><ul><li>Shell Scripting </li></ul><ul><li>Questions </li></ul><ul><li>Feedback </li></ul>
  3. 3. Session Objectives <ul><li>Understand the basics of Unix Operating Environment </li></ul><ul><li>Understand the basics of Shell Scripting </li></ul>
  4. 4. Unix History <ul><li>History of the Unix Operating System: </li></ul><ul><li>- Beginning at AT&T Bell Laboratories </li></ul><ul><li>- Kenneth Thompson </li></ul><ul><li>- Dennis Ritchie </li></ul><ul><li>- Berkeley: The Second School </li></ul><ul><li>- BSD UNIX </li></ul><ul><li>- Stallman and Torvalds </li></ul><ul><li>- GNU and LINUX </li></ul>
  5. 5. Unix Why??? <ul><li> There are only two kind of people </li></ul><ul><li>who understand 0 1 and who don’t understand </li></ul><ul><li>01010101010101010101010101010101010101010101010101010 </li></ul><ul><li>The features that made UNIX a hit from the start are: </li></ul><ul><ul><li>Multitasking capability </li></ul></ul><ul><ul><li>Multi-user capability </li></ul></ul><ul><ul><li>Portability </li></ul></ul><ul><ul><li>UNIX programs </li></ul></ul><ul><ul><li>Library of application software </li></ul></ul>
  6. 6. ARCHITECTURE
  7. 7. ARCHITECHTURE <ul><li>The UNIX system is functionally organized at three levels: </li></ul><ul><ul><li>The kernel, which schedules tasks and manages storage; </li></ul></ul><ul><ul><li>The shell, which connects and interprets users' commands, calls programs from memory, and executes them; and </li></ul></ul><ul><ul><li>The tools and applications that offer additional functionality to the operating system </li></ul></ul>
  8. 8. Solaris File System Tree
  9. 10. Navigating The File System <ul><li>pwd: Checking your Current Directory </li></ul><ul><li>cd: Changing Directory </li></ul><ul><li>mkdir: Making Directories </li></ul><ul><li>rmdir: Removing Directories </li></ul><ul><li>ls: Listing Files </li></ul>
  10. 11. General-Purpose Utilities <ul><li>banner: Display a Blown-up Message </li></ul><ul><li>cal: The Calendar </li></ul><ul><li>date: Display the System Date </li></ul><ul><li>who: Login Details </li></ul><ul><li>tty: Knowing Your Terminal </li></ul><ul><li>man <command>: Displays help on <command> </li></ul><ul><li>apropos <keyword>:locates commands by keyword lookup </li></ul><ul><li>whatis <command>: Brief command description </li></ul>
  11. 13. Handling Files <ul><li>cat: Displaying and Creating Files </li></ul><ul><li>cp: Copying a File </li></ul><ul><li>rm: Deleting Files </li></ul><ul><li>mv: Renaming Files </li></ul><ul><li>more: Paging Output </li></ul><ul><li>lp: Printing a File </li></ul><ul><li>file: Know the file types </li></ul><ul><li>wc: Line and Word and character counting </li></ul><ul><li>split: Splitting a File into Multiple Files </li></ul><ul><li>cmp: Comparing two files </li></ul>
  12. 14. File Attributes <ul><li>ls –l: listing File Attributes </li></ul><ul><li>ls –d: Listing Directory Attributes </li></ul><ul><li>stat: Display File Permissions </li></ul><ul><li>chmod: Changing File Permissions </li></ul><ul><li>chown: Changing Ownership </li></ul><ul><li>chgrp: Changing Group </li></ul><ul><li>chsh: Changing Shell </li></ul><ul><li>ln: Links </li></ul><ul><li>umask: Define Defaults File Permissions for a User </li></ul>
  13. 15. The Environment <ul><li>env: Displays Environment Properties </li></ul><ul><li>.profile: The Script Executed During Login Time </li></ul><ul><li>PWD: Know your Current Directory </li></ul><ul><li>HISTSIZE: Define History Size </li></ul><ul><li>LOGNAME: Login Name </li></ul><ul><li>SHELL: Your Default Shell </li></ul><ul><li>HOME: Define Home Directory </li></ul><ul><li>PATH: Declare Path </li></ul><ul><li>MAIL: Declare Mail Directory Path </li></ul><ul><li>alias: Define short-hand names for commands </li></ul>
  14. 16. The Process <ul><li>ps: Process Status </li></ul><ul><li><command> &: To run a process in Background </li></ul><ul><li>bg: To run a process in Background </li></ul><ul><li>fg: To run a process in Foreground </li></ul><ul><li>kill: Premature Termination of a Process </li></ul><ul><li>nice: job execution with low Priority </li></ul><ul><li>at and batch: Execute Later </li></ul><ul><li>cron: Running jobs Periodically </li></ul>
  15. 17. Communication And Electronic Mail <ul><li>write: Two-way Communication </li></ul><ul><li>mesg: Your Willingness to Talk </li></ul><ul><li>talk: Splits the screen into two windows for chatting </li></ul><ul><li>mail: The mailer </li></ul><ul><li>news: To read news messages </li></ul><ul><li>elm: A screen-oriented mail handler </li></ul><ul><li>pine: Another mail program </li></ul><ul><li>finger: Details of Users </li></ul>
  16. 18. Networking <ul><li>telnet: Remote Login </li></ul><ul><li>ftp: File Transfer (protocol) </li></ul><ul><li>rlogin: Remote Login Without Password </li></ul><ul><li>rcp: Remote File Copying </li></ul>
  17. 19. System Administration <ul><li>useradd: Adding Users </li></ul><ul><li>/etc/passwd: User Information </li></ul><ul><li>/etc/shadow: Encrypted Passwords </li></ul><ul><li>userdel: Removing Users </li></ul><ul><li>fsck: File System Checking </li></ul><ul><li>df: Checking disk size </li></ul><ul><li>du: Checking file size </li></ul><ul><li>groupadd: Adding Group </li></ul><ul><li>groupdel: Deleting Group </li></ul><ul><li>groupmod: Modifying Group </li></ul>
  18. 20. The Shell <ul><li>The following table shows the default system prompt and superuser prompt for the C shell, Bourne shell, and Korn shell. </li></ul>
  19. 21. Shell Scripting <ul><li>A universal convention is that the extension .sh be used for shell scripts (or shell programs or shell procedures ). </li></ul><ul><li>The scripts can be executed in two ways: </li></ul><ul><li>$ chmod +x <filename.sh> </li></ul><ul><li>$ sh <filename.sh> </li></ul><ul><li>read: For taking input from the user </li></ul><ul><li>Example: $ cat example1.sh </li></ul><ul><li>#First Simple Example to print the standard input from the user to the standard output. </li></ul><ul><li>echo “Enter Your Name: “ </li></ul><ul><li>read name </li></ul><ul><li>echo “Your name is: $name” </li></ul>
  20. 22. Shell Scripting (Contd..) <ul><li>When arguments are specified with a shell procedure, they are assigned to certain special “variables” or rather positional parameters. </li></ul><ul><li>$1,$2,etc. The positional Parameters </li></ul><ul><li>$* Complete set of positional parameters as a single string </li></ul><ul><li>$# Number of arguments specified in command line </li></ul><ul><li>$0 Name of executed command </li></ul><ul><li>$? Exit status of the last command </li></ul><ul><li>$! PID of last command </li></ul>
  21. 23. Shell Scripting (Contd..) <ul><li>Example: $ cat example2.sh </li></ul><ul><li>#Example to print the standard input from the user to the standard output using positional parameters. </li></ul><ul><li>echo “Program: $0 </li></ul><ul><li>The Number of Arguments Specified is $# </li></ul><ul><li>The Arguments are $*” </li></ul><ul><li>echo “Your Name: $1 Your Extension Number: $2” </li></ul><ul><li>Example: $ example2.sh Jai 5630 </li></ul><ul><li>Program: example2.sh </li></ul><ul><li>The Number of Arguments Specified is 2 </li></ul><ul><li>The Arguments are Jai 5630 </li></ul><ul><li>Your Name: Jai </li></ul><ul><li>Your Extension Number: 5630 </li></ul>
  22. 24. Shell Programming (Contd..) <ul><li>The && operator delimits two commands; the second command is executed only when the first succeeds. </li></ul><ul><li>The || operator delimits two commands; the second command is executed only when the first fails. </li></ul><ul><li>The exit statement is used to prematurely terminate a program. </li></ul><ul><li>The if Conditional </li></ul><ul><li>if condition is true </li></ul><ul><li>then </li></ul><ul><li> execute commands </li></ul><ul><li>else </li></ul><ul><li> execute commands </li></ul><ul><li>fi </li></ul>
  23. 25. Shell Scripting (Contd..) <ul><li>Relational Operators </li></ul><ul><li>-eq Equal to </li></ul><ul><li>-neq Not equal to </li></ul><ul><li>-gt greater than </li></ul><ul><li>-ge greater than or equal to </li></ul><ul><li>-lt less than </li></ul><ul><li>-le less than or equal to </li></ul><ul><li>test uses certain operators to evaluate the condition on its right, and returns either a true or false </li></ul><ul><li>test $x –eq $y or [$x –eq $y] </li></ul><ul><li>String tests used by test </li></ul><ul><li>-n stg true if string stg is not a null string </li></ul><ul><li>-z stg true is string stg is a null string </li></ul><ul><li>s1 = s2 true if string s1=s2 </li></ul><ul><li>s1! = s2 true if string s1 is not equal to s2 </li></ul><ul><li>stg true if string stg is assigned and not null </li></ul>
  24. 26. Shell Scripting (Contd..) <ul><li>File-related tests with test </li></ul><ul><li>-e file true if file exists </li></ul><ul><li>-f file true if file exists and is a regular file </li></ul><ul><li>-r file true if file exists and is a redable </li></ul><ul><li>-w file true if file exists and is a writable </li></ul><ul><li>-x file true if file exists and is a executable </li></ul><ul><li>-d file true if file exists and is a directory </li></ul><ul><li>-s file true if file exists and has a size greater than zero </li></ul>
  25. 27. Shell Scripting (Contd..) <ul><li>The case conditional </li></ul><ul><li>case expression in </li></ul><ul><li>pattern1) execute commands;; </li></ul><ul><li>pattern2) execute commands;; </li></ul><ul><li>pattern3) execute commands;; </li></ul><ul><li>…… </li></ul><ul><li>esac </li></ul><ul><li>The while loop </li></ul><ul><li>while condition is true </li></ul><ul><li>do </li></ul><ul><li>execute commands </li></ul><ul><li>done </li></ul>
  26. 28. Shell Scripting (Contd..) <ul><li>The for loop </li></ul><ul><li>for variable in list </li></ul><ul><li>do </li></ul><ul><li>execute commands </li></ul><ul><li>done </li></ul><ul><li>Example: </li></ul><ul><li>$ for x in 1 2 4 5 #list has 4 strings </li></ul><ul><li> do </li></ul><ul><li>echo “ The value of x is $x” </li></ul><ul><li> done </li></ul><ul><li>The value of x is 1 </li></ul><ul><li>The value of x is 2 </li></ul><ul><li>The value of x is 4 </li></ul><ul><li>The value of x is 5 </li></ul>
  27. 29. Shell Scripting (Contd..) <ul><li>The here document symbol (<<) is used sometimes when we need to place the data inside the script. </li></ul><ul><li>The here document symbol (<<) is followed by the data and a delimiter. </li></ul><ul><li>shift transfers the contents of a positional parameter to its immediate lower numbered one. </li></ul><ul><li>When set –x is used inside a script, it echoes each statement on the terminal, preceded by a + as it is executed. </li></ul>
  28. 30. Example <ul><li>bash-2.03$ cat example3.sh </li></ul><ul><li>IFS=&quot;|&quot; </li></ul><ul><li>while echo &quot;Enter Department Code: &quot; </li></ul><ul><li>do read dcode </li></ul><ul><li>set -- `grep $dcode data` </li></ul><ul><li>case $# in </li></ul><ul><li>3) echo &quot;Department name : $2 Emp-id of head of dept : $3&quot; </li></ul><ul><li>shift 3 ;; </li></ul><ul><li>*) echo &quot;Invalid Code&quot; ; continue </li></ul><ul><li>esac </li></ul><ul><li>done </li></ul><ul><li>bash-2.03$ cat data </li></ul><ul><li>01|accounts|6213 </li></ul><ul><li>02|admin|5423 </li></ul><ul><li>03|marketing|6521 </li></ul><ul><li>04|personnel|2365 </li></ul><ul><li>05|production|9876 </li></ul><ul><li>06|sales|1006 </li></ul>
  29. 31. Example (Contd…) <ul><li>bash-2.03$ bash example4.sh </li></ul><ul><li>Enter Department Code: 99 </li></ul><ul><li>Invalid Code </li></ul><ul><li>Enter Department Code: 01 </li></ul><ul><li>Department name : accounts Emp-id of head of dept : 6213 </li></ul><ul><li>Enter Department Code: 06 </li></ul><ul><li>Department name : sales Emp-id of head of dept : 1006 Enter Department Code: </li></ul>
  30. 32. Questions <ul><li>? </li></ul>
  31. 33. ... Feedback…

×