Difference between revisions of "Shell Scripting - Part 3"

From CDOT Wiki
Jump to: navigation, search
(Created page with '== Purpose of Shell Scripting - Part 3 == == Online Scripting Resources == If you require <u>'''additional practice'''</u> in creating shell scripts and using the vi text ed…')
 
Line 1: Line 1:
 
== Purpose of Shell Scripting - Part 3 ==
 
== Purpose of Shell Scripting - Part 3 ==
  
 
+
Bash Shell Scripting Tips:|<br><ul><li>'''The Here Document'''<br><br>A neat little trick involving a special type of redirection of stdin ( '''&lt;&lt;''' ) that allows input to be redirected to a command from within the command. '''The name relates to where the stdin is contained: not in a file, but "here in the command itself"'''. A character (like '''+''') is used to mark the boundary of stdin. It is important that the ending boundary only contains a line with that matching character (eg '''+'''); otherwise the stdin will continue to be read! This command is a convenience way to display multiple lines on that screen, but this command can be used with any Linux command that accept stdin.<br><br><u>'''Examples''' (try at the shell prompt)</u><br><br>''cat &lt;&lt;+<br>This is a test message<br>This is the second line<br>+''<br><br>''mail -s "test message" youremailaddr &lt;&lt;+<br>This is a test message<br>I hope you like it.''<br>+<br><br>''tr [a-z] [A-Z] &lt;&lt;+<br>i like ops235<br>i love scripting.<br>+''<br><br></li><li>'''Using sed to Manipulate Text'''<br><br>The Linux command '''sed''' stands for <u>'''S'''</u>treaming <u>'''Ed'''</u>itor which is an effective way to manipulate a text file, output sent from a command, or from within a "here document". This command can manipulate matching text on a variety of criteria (such as line number, regular expression match, etc). Commands can then be used for manipulation such as omitting, printing, substituting, adding, inserting, etc. The sed option -n suppresses display of text so the print (p) command can be used; otherwise, the text will be displayed (with edits via the sed command instructions). Results of text manipulation with sed can be stored in a variable using command substitution, or redirected to a file. NEVER redirect the stdout from a sed command to the same input file (or the input file will be destroyed)! <br><br></li><li>'''<u>Examples''' (try at the shell prompt)</u><br><br>''sed 's/&#124;/ /g' &lt;&lt;+<br>I&#124;like&#124;weekends!<br>+''<br><br>''sed 's/$/\n/g' &lt;&lt;+<br>This text<br>should be<br>double-spaced!''<br>+<br><br></li></ul>
  
  

Revision as of 09:10, 13 October 2015

Purpose of Shell Scripting - Part 3

Bash Shell Scripting Tips:|
  • The Here Document

    A neat little trick involving a special type of redirection of stdin ( << ) that allows input to be redirected to a command from within the command. The name relates to where the stdin is contained: not in a file, but "here in the command itself". A character (like +) is used to mark the boundary of stdin. It is important that the ending boundary only contains a line with that matching character (eg +); otherwise the stdin will continue to be read! This command is a convenience way to display multiple lines on that screen, but this command can be used with any Linux command that accept stdin.

    Examples (try at the shell prompt)

    cat <<+
    This is a test message
    This is the second line
    +


    mail -s "test message" youremailaddr <<+
    This is a test message
    I hope you like it.

    +

    tr [a-z] [A-Z] <<+
    i like ops235
    i love scripting.
    +


  • Using sed to Manipulate Text

    The Linux command sed stands for Streaming Editor which is an effective way to manipulate a text file, output sent from a command, or from within a "here document". This command can manipulate matching text on a variety of criteria (such as line number, regular expression match, etc). Commands can then be used for manipulation such as omitting, printing, substituting, adding, inserting, etc. The sed option -n suppresses display of text so the print (p) command can be used; otherwise, the text will be displayed (with edits via the sed command instructions). Results of text manipulation with sed can be stored in a variable using command substitution, or redirected to a file. NEVER redirect the stdout from a sed command to the same input file (or the input file will be destroyed)!

  • Examples (try at the shell prompt)

    sed 's/|/ /g' <<+
    I|like|weekends!
    +


    sed 's/$/\n/g' <<+
    This text
    should be
    double-spaced!

    +


Online Scripting Resources

If you require additional practice in creating shell scripts and using the vi text editor, run the commands in your Matrix account:


Scripting Content