Linux Bourne Again Shell (BASh) Programming  VSRivera IBM Learning Services Worldwide Certified Manual
Unit 1. Basic Shell Concepts <ul><li>Objectives </li></ul><ul><ul><li>Describe the Linux Shell </li></ul></ul><ul><ul><li>...
Shells <ul><li>User interface to linux </li></ul><ul><li>Command interpreter </li></ul><ul><li>Programming Language </li><...
Shells <ul><li>Each shell has different features and syntax </li></ul><ul><li>GNU Bash adds features to the standard Syste...
Directories <ul><li>The file system comprises directories in a hierarchical structure </li></ul><ul><li>All Linux director...
<ul><li>No drive letters like A:, C:, ... </li></ul>
File <ul><li>Collection of data </li></ul><ul><li>A stream of characters or a byte stream </li></ul><ul><li>No structure i...
File Names <ul><li>Should be descriptive of the content </li></ul><ul><li>Should use only alphanumeric characters  </li></...
File Names <ul><li>Should not begin with + or - sign </li></ul><ul><li>Are case sensitive </li></ul><ul><li>Filenames star...
Shell Script <ul><li>A readable text file which can be edited with a text editor </li></ul><ul><li>Anything that you can d...
Invoking Scripts <ul><li>$ . prog  </li></ul><ul><ul><li>prog runs in current shell environment </li></ul></ul><ul><li>bas...
Metacharacters <ul><li>Characters that have special meaning </li></ul><ul><ul><li>Wildcard or filename expansion </li></ul...
Wildcard metacharacters <ul><li>Metacharacters that form patterns that are expanded into matching filenames from the curre...
<ul><li>Character equivalence classes can be used in place of range lists </li></ul><ul><ul><li>[[:upper:]] – range list o...
Expansion examples <ul><li>$ rm d*y </li></ul><ul><li>$ file script* </li></ul><ul><li>$ ls script[345] </li></ul><ul><li>...
Quoting metacharacters <ul><li>Stops normal shell metacharacter processing, including metacharacter expansion </li></ul><u...
Quoting metacharacters <ul><ul><li>To form literal strings </li></ul></ul><ul><ul><ul><li>‘ single quotes’ remove special ...
Brace expansion <ul><li>Bash allows an additional expansion facility </li></ul><ul><ul><li>Brace ({}) expansion </li></ul>...
<ul><li>Question? </li></ul>
Upcoming SlideShare
Loading in …5
×

COMELEC III - Bash unit 1

853 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
853
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
19
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

COMELEC III - Bash unit 1

  1. 1. Linux Bourne Again Shell (BASh) Programming VSRivera IBM Learning Services Worldwide Certified Manual
  2. 2. Unit 1. Basic Shell Concepts <ul><li>Objectives </li></ul><ul><ul><li>Describe the Linux Shell </li></ul></ul><ul><ul><li>Use the Linux file system </li></ul></ul><ul><ul><li>Create a shell script </li></ul></ul><ul><ul><li>Use metacharacters and brace expansion </li></ul></ul><ul><ul><li>Use I/O redirection </li></ul></ul><ul><ul><li>Use pipes and tees </li></ul></ul><ul><ul><li>Group commands </li></ul></ul><ul><ul><li>Use Bash shell job control </li></ul></ul><ul><ul><li>Use command line recall and editing </li></ul></ul>
  3. 3. Shells <ul><li>User interface to linux </li></ul><ul><li>Command interpreter </li></ul><ul><li>Programming Language </li></ul><ul><li>Linux Shells </li></ul><ul><ul><li>Bash V1 - bash </li></ul></ul><ul><ul><li>Public Domain Korn - ksh </li></ul></ul><ul><ul><li>Bourne/System V - ash </li></ul></ul><ul><ul><li>C Compatible - tcsh </li></ul></ul>
  4. 4. Shells <ul><li>Each shell has different features and syntax </li></ul><ul><li>GNU Bash adds features to the standard System V or Bourne Shell. </li></ul><ul><li>Bash was written by Free Software Foundation (FSF) as free software. </li></ul><ul><li>Bourne shell is the oldest shell written by Steven Bourne. </li></ul><ul><li>David G. Korn wrote the Korn Shell at AT&T’s Bell Labs (Lucent). It is more recent than other shells . </li></ul>
  5. 5. Directories <ul><li>The file system comprises directories in a hierarchical structure </li></ul><ul><li>All Linux directories are contained in one, virtual, &quot;unified filesystem“ </li></ul><ul><li>Physical devices are mounted on mount points </li></ul><ul><ul><li>Floppy disks </li></ul></ul><ul><ul><li>Hard disk partitions </li></ul></ul><ul><ul><li>CD-ROM drives </li></ul></ul>
  6. 6. <ul><li>No drive letters like A:, C:, ... </li></ul>
  7. 7. File <ul><li>Collection of data </li></ul><ul><li>A stream of characters or a byte stream </li></ul><ul><li>No structure is imposed on a file by the operating system </li></ul><ul><li>Directories and devices are known as “special files” – the operating system controls their use. </li></ul><ul><ul><li>/dev/null </li></ul></ul><ul><ul><li>/home </li></ul></ul>
  8. 8. File Names <ul><li>Should be descriptive of the content </li></ul><ul><li>Should use only alphanumeric characters </li></ul><ul><ul><li>UPPERCASE, lowercase, number, </li></ul></ul><ul><ul><li>. , @, - , _ </li></ul></ul><ul><li>Should not include embedded blanks </li></ul><ul><li>Should not contain shell metacharacters: * </li></ul><ul><ul><li>? > < / ; & ! [ ] | ' &quot; ( ) { } </li></ul></ul>
  9. 9. File Names <ul><li>Should not begin with + or - sign </li></ul><ul><li>Are case sensitive </li></ul><ul><li>Filenames starting with a . are hidden </li></ul><ul><li>The maximum number of characters for a filename is 255 </li></ul><ul><li>Pathname length of 4095 characters </li></ul>
  10. 10. Shell Script <ul><li>A readable text file which can be edited with a text editor </li></ul><ul><li>Anything that you can do from the shell prompt </li></ul><ul><li>A program containing: </li></ul><ul><ul><li>System commands </li></ul></ul><ul><ul><li>Variable assignments </li></ul></ul><ul><ul><li>Flow control syntax </li></ul></ul><ul><ul><li>Shell commands </li></ul></ul><ul><ul><li>comments </li></ul></ul>
  11. 11. Invoking Scripts <ul><li>$ . prog </li></ul><ul><ul><li>prog runs in current shell environment </li></ul></ul><ul><li>bash prog </li></ul><ul><ul><li>Run prog in a new shell </li></ul></ul><ul><li>prog </li></ul><ul><ul><li>Run in a new shell if prog is executable </li></ul></ul><ul><li>exec prog </li></ul><ul><ul><li>Run in a new shell to replace a current one </li></ul></ul>
  12. 12. Metacharacters <ul><li>Characters that have special meaning </li></ul><ul><ul><li>Wildcard or filename expansion </li></ul></ul><ul><ul><li>Quoting </li></ul></ul><ul><li>Characters that do not represent themselves </li></ul><ul><li>Shell processes metacharacters before executing a command </li></ul>
  13. 13. Wildcard metacharacters <ul><li>Metacharacters that form patterns that are expanded into matching filenames from the current directory </li></ul><ul><ul><li>* – match any number of any characters </li></ul></ul><ul><ul><li>? – match any single character </li></ul></ul><ul><ul><li>[abc] – match a single character from a bracketed list </li></ul></ul><ul><ul><li>[!az] – match any single character except those listed </li></ul></ul><ul><ul><li>[a-z] – inclusive range for a list </li></ul></ul>
  14. 14. <ul><li>Character equivalence classes can be used in place of range lists </li></ul><ul><ul><li>[[:upper:]] – range list of uppercase classes </li></ul></ul><ul><ul><li>[[:lower:]] – all lower case letters </li></ul></ul><ul><ul><li>[[:digit:]] – digits </li></ul></ul><ul><ul><li>[[:space:]] – spacing characters: tab, space, etc.. </li></ul></ul>
  15. 15. Expansion examples <ul><li>$ rm d*y </li></ul><ul><li>$ file script* </li></ul><ul><li>$ ls script[345] </li></ul><ul><li>$ touch ?a* </li></ul><ul><li>$ ls [st][ah]* </li></ul><ul><li>$ touch [^ab]* </li></ul><ul><li>$ touch [a-z]*t[0-9] </li></ul>
  16. 16. Quoting metacharacters <ul><li>Stops normal shell metacharacter processing, including metacharacter expansion </li></ul><ul><ul><li>To form strings </li></ul></ul><ul><ul><ul><li>“ double quotes” group character into a string, and allow variable and command substitution. Everything between &quot; and &quot; is taken literally except $, `, “ </li></ul></ul></ul>
  17. 17. Quoting metacharacters <ul><ul><li>To form literal strings </li></ul></ul><ul><ul><ul><li>‘ single quotes’ remove special meaning for the character within them. Everything between ' and ' is taken literally, except for another ‘. </li></ul></ul></ul><ul><ul><li>For a literal character </li></ul></ul><ul><ul><ul><li>character removes the special meaning of the character following the . </li></ul></ul></ul><ul><ul><ul><li>Where is nested inside double quotes, it only removes the special meaning of characters: , ‘, “, $. </li></ul></ul></ul>
  18. 18. Brace expansion <ul><li>Bash allows an additional expansion facility </li></ul><ul><ul><li>Brace ({}) expansion </li></ul></ul><ul><ul><li>$ ls *{1,2} </li></ul></ul><ul><ul><li>$ mkdir release_{src,doc} </li></ul></ul><ul><ul><li>$ cat {file1,file2,file3} > combined_file </li></ul></ul><ul><ul><li>$ cp file22.{txt,backup} </li></ul></ul><ul><ul><li>$ echo {a..z} </li></ul></ul><ul><ul><li>$ echo {0..3} </li></ul></ul><ul><ul><li>$ echo hi{DDD,BBB,CCC,AAA}there </li></ul></ul><ul><ul><li>$ ls {ch,app}? </li></ul></ul>
  19. 19. <ul><li>Question? </li></ul>

×